Open Bug 2093579 Opened 3 days ago Updated 3 days ago

Make OffscreenCanvas cached fonts threadsafe

Categories

(Core :: Graphics, defect)

defect

Tracking

()

ASSIGNED

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.

  1. UnscaledFontDWrite::GetFontDescriptor asserts NS_IsMainThread(), because it lazily caches the font file name. A worker canvas drawing text on Windows hits this assertion.

  2. A recorded ScaledFont reports its destruction through a user data destroy callback (RecordingFontUserDataDestroyFunc) on whichever thread releases it. Off the main thread it records RecordedScaledFontDestruction into 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 UnscaledFontDWrite with a mutex instead of asserting the main thread.
  • Stop using font user data in recordings. As WebRenderBridgeChild does for font keys, track recorded fonts in a map keyed by ThreadSafeWeakPtr<ScaledFont>, and on the recorder's owning thread record the destruction of the dead entries whenever ScaledFont::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.

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.

Assignee: nobody → aosmond

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.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: