Make OffscreenCanvas cached fonts threadsafe
Categories
(Core :: Graphics, defect)
Tracking
()
People
(Reporter: aosmond, Assigned: aosmond)
References
(Blocks 1 open bug)
Details
Attachments
(3 external files)
Canvas 2D recordings (DrawTargetRecording with CanvasDrawEventRecorder) assume that fonts are recorded and destroyed on the main thread. That stops holding once a canvas is recorded on a DOM worker (accelerated OffscreenCanvas 2D), and is not guaranteed even for main-thread canvases, because ScaledFonts come from the process-wide font cache and their last reference can be released on any thread.
-
UnscaledFontDWrite::GetFontDescriptorassertsNS_IsMainThread(), because it lazily caches the font file name. A worker canvas drawing text on Windows hits this assertion. -
A recorded
ScaledFontreports its destruction through a user data destroy callback (RecordingFontUserDataDestroyFunc) on whichever thread releases it. Off the main thread it recordsRecordedScaledFontDestructioninto the recorder from that thread, racing the recorder's owning thread, and the recorder's raw-pointer set of stored fonts is left with a dangling entry. For worker canvases the font is usually released on the main thread, when the font cache expires it, so the wrong-thread case is the common one there.
The translator keys fonts by their content-process address, so destructions must also be recorded before the creation of any new ScaledFont or UnscaledFont that reuses an address (see bug 1607630 for the wrong glyphs that happen otherwise). Note that ScaledFont releases its UnscaledFont before its user data is destroyed, so any destroy-callback scheme leaves a small window for UnscaledFont address reuse.
Proposed fix:
- Guard the cached font file name in
UnscaledFontDWritewith a mutex instead of asserting the main thread. - Stop using font user data in recordings. As
WebRenderBridgeChilddoes for font keys, track recorded fonts in a map keyed byThreadSafeWeakPtr<ScaledFont>, and on the recorder's owning thread record the destruction of the dead entries wheneverScaledFont::DeletionCounter()changes: before recording any new font, at the end of each canvas transaction, and when the recorder detaches. This never touches the recorder from another thread, needs no cross-thread dispatch, and closes the address reuse window.
| Assignee | ||
Comment 1•3 days ago
|
||
Canvas recordings on DOM workers need font descriptors too, so guard the
cached font file name with a mutex instead of asserting the main thread.
Updated•3 days ago
|
| Assignee | ||
Comment 2•3 days ago
|
||
Fonts are shared between threads, so a recorded ScaledFont may be destroyed
on another thread, which would record its destruction into the recorder from
that thread. Instead, as WebRender does for font keys, track recorded fonts
with weak pointers and record the destruction of dead ones on the owning
thread whenever ScaledFont::DeletionCounter changes. This is checked before
recording a font, which may reuse the address of a destroyed one, and at the
end of each canvas transaction, so that the translator can free them.
DetachResources records the destruction of the remaining fonts, as removing
their user data used to. External fonts are now tracked separately, since
they are kept alive until ClearResources.
| Assignee | ||
Comment 3•3 days ago
|
||
Description
•