Open Bug 2083548 Opened 1 month ago Updated 1 month ago

Drawing a WebGL canvas into a 2D canvas produces transparent black on Windows without D3D11 fence support

Categories

(Core :: Graphics: Canvas2D, defect)

Desktop
Windows
defect

Tracking

()

ASSIGNED

People

(Reporter: aosmond, Assigned: aosmond)

References

Details

Attachments

(2 external files)

On Windows, when D3D11 fences are not available, drawing a WebGL canvas into an accelerated 2D canvas (drawImage(webglCanvas), and readbacks such as getImageData or toDataURL on the result) produces transparent black instead of the WebGL content. Each draw also waits about 100 ms. This happens with remote WebGL and accelerated Canvas2D in the GPU process.

Without fence support, SharedSurface_ANGLEShareHandle::Create synchronizes the WebGL snapshot texture with a keyed mutex instead of a fence. In the GPU process:

  1. WebGLContext::GetBackBufferSnapshotSharedSurface writes the snapshot and releases the keyed mutex.
  2. CanvasTranslator::ResolveExternalSnapshot calls BeginRead() on the snapshot, which acquires the keyed mutex again for the producer and holds it until EndRead().
  3. While it is held, the snapshot is imported into the 2D canvas. GLBlitHelper::BlitDescriptor(const SurfaceDescriptorD3D10&) opens the texture and BindAnglePlanes calls AcquireSync(0, 100), which times out.
  4. WAIT_TIMEOUT is not a failure code, so BindAnglePlanes only checks FAILED(hr) and blits from a texture it does not own, which reads as zeros. Its destructor then releases a mutex it never acquired.

With fences, BeginRead() takes no lock and the import waits on the write fence, so the default path on fence-capable hardware works.

Steps to reproduce:

  1. Set gfx.direct3d11.fences-enabled=false (added in bug 2083416; it makes FenceD3D11::IsSupported return false, as on hardware without fence support).
  2. Run dom/canvas/test/webgl-conf/generated/test_conformance__canvas__webgl-to-2d-canvas.html or test_conformance__canvas__draw-webgl-to-canvas-test.html (WebGL 1 or 2).

Actual: 38 of 45 checks fail with drawImage: Should be [...], reading transparent black.
Expected: all checks pass, as they do with fences enabled.

This was found on a try push for bug 2083416, which also fails draw-static-webgl-to-multiple-canvas-test and context-attribute-preserve-drawing-buffer.

Proposed fix, verified locally (all of the tests above pass with fences off, and still pass with fences on):

  • ResolveExternalSnapshot: do not call BeginRead()/EndRead() for SurfaceDescriptorD3D10 snapshots with hasKeyedMutex, since the import acquires the mutex itself and the producer already released it.
  • BindAnglePlanes: treat any AcquireSync result other than S_OK as failure, and release exactly the mutexes that were acquired, so this cannot silently read an unowned texture again.

Without D3D11 fences, WebGL snapshots are synchronized with a keyed mutex.
ResolveExternalSnapshot acquired it for the producer with BeginRead while
importing the snapshot, but the import acquires the same mutex to read the
texture, so it timed out and read transparent black. The producer already
released the mutex after writing the snapshot, so let the import acquire it.

AcquireSync returns WAIT_TIMEOUT and WAIT_ABANDONED as success codes, so
BindAnglePlanes went on to read textures it did not own and then released
mutexes it never acquired. Only S_OK now counts, and only the mutexes that were
acquired are released, including when a later plane fails.

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

Attachment

General

Created:
Updated:
Size: