- Revision
- 288076
- Author
- [email protected]
- Date
- 2022-01-16 14:10:02 -0800 (Sun, 16 Jan 2022)
Log Message
Ensure in flight layer transactions don't accumulate further canvas drawing
https://bugs.webkit.org/show_bug.cgi?id=231163
<rdar://problem/83863292>
Reviewed by Simon Fraser.
Source/WebCore:
When UI side compositing is enabled (as it is on iOS), we build a
layer tree transaction in RemoteLayerTreeDrawingArea::updateRendering to
send off to the UI process. At a high level, updateRendering does this:
1. Run any requestAnimationFrame callbacks
2. Iterate over the composited layers to draw their contents into the
layer backing stores
3. Dispatch a task to a different thread to flush the contexts of the
layer backing stores and then send the transaction to the UI
process
Step 3 is done off a separate task as an optimization, to get the
drawing work queued up by step 2 happening in parallel to any work the
page may day once updateRendering is finished. This can be a problem
when:
- GPU process canvas rendering is enabled (but DOM rendering is
disabled)
- we have accelerated ImageBuffers for both layer backing stores and
canvas backing stores
- the page does canvas drawing between steps 2 and 3, which affects
the contents of a canvas backing store IOSurface that was drawn into
a layer backing store IOSurface while building the transaction
The way we draw the canvas contents into the layer is by creating a
CGImage from the canvas backing store ImageBuffer's IOSurface.
Normally, if we draw into an IOSurface that has been wrapped by a
CGImage, this will cause the CGImage to obtain a unique copy of the
IOSurface's pixels, rather than continue holding on to the IOSurface
itself.
When the drawing of this CGImage on to the layer backing store
IOSurface happens, the "draw image" command is queued up to be
processed later. It's only when the flush happens in step 3 above that
the drawing occurs. This means that the CGImage wrapping the IOSurface
exists until that flush.
But while the CGImage does exist, it's in the Web process, and the
canvas drawing on to the same IOSurface happens in the GPU process.
Since QuartzCore doesn't know of its existence, it doesn't cause the
copy-on-write to occur. The effect of this is that subsequent page
drawing on to the canvas can make its way into the layer transaction
unexpectedly.
This patch induces the copy-on-write in the Web process when needed,
by tracking whether a canvas has been drawn into a layer backing
store and the layer transaction flush hasn't happened yet. Just before
we do any more drawing on the canvas, if we are in this state, we
perform a no-op drawing command on the IOSurface, then flush, to make
the CGImage copy the IOSurface data out.
* html/HTMLCanvasElement.cpp:
(WebCore::imageDrawingRequiresGuardAgainstUseByPendingLayerTransaction):
We only need to induce the CGImage copy-on-write behavior if both the
layer and canvas backing stores are accelerated, the canvas
ImageBuffer is remote, and the layer ImageBuffer is not remote.
(WebCore::HTMLCanvasElement::paint):
(WebCore::HTMLCanvasElement::drawingContext const):
Use drawingContext as a convenient single place to check whether we
are performing the first drawing command before the layer tree
transaction flush has happened.
* html/HTMLCanvasElement.h:
* page/Page.h:
(WebCore::Page::setIsAwaitingLayerTreeTransactionFlush):
(WebCore::Page::isAwaitingLayerTreeTransactionFlush const):
* platform/graphics/ImageBufferBackend.h:
(WebCore::ImageBufferBackend::ensureNativeImagesHaveCopiedBackingStore):
* platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp:
(WebCore::ImageBufferIOSurfaceBackend::ensureNativeImagesHaveCopiedBackingStore):
The flush is needed since the CGImage wrapping the IOSurface will only
copy the pixel data out once the no-op drawing command is processed.
* platform/graphics/cg/ImageBufferIOSurfaceBackend.h:
Source/WebKit:
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::willCommitLayerTree):
(WebKit::WebPage::didFlushLayerTreeAtTime):
Inform the WebCore::Page about the state of the layer tree transaction
building.
* WebProcess/WebPage/WebPage.h:
Modified Paths
Diff
Modified: trunk/Source/WebCore/ChangeLog (288075 => 288076)
--- trunk/Source/WebCore/ChangeLog 2022-01-16 21:44:55 UTC (rev 288075)
+++ trunk/Source/WebCore/ChangeLog 2022-01-16 22:10:02 UTC (rev 288076)
@@ -1,3 +1,84 @@
+2022-01-16 Cameron McCormack <[email protected]>
+
+ Ensure in flight layer transactions don't accumulate further canvas drawing
+ https://bugs.webkit.org/show_bug.cgi?id=231163
+ <rdar://problem/83863292>
+
+ Reviewed by Simon Fraser.
+
+ When UI side compositing is enabled (as it is on iOS), we build a
+ layer tree transaction in RemoteLayerTreeDrawingArea::updateRendering to
+ send off to the UI process. At a high level, updateRendering does this:
+
+ 1. Run any requestAnimationFrame callbacks
+ 2. Iterate over the composited layers to draw their contents into the
+ layer backing stores
+ 3. Dispatch a task to a different thread to flush the contexts of the
+ layer backing stores and then send the transaction to the UI
+ process
+
+ Step 3 is done off a separate task as an optimization, to get the
+ drawing work queued up by step 2 happening in parallel to any work the
+ page may day once updateRendering is finished. This can be a problem
+ when:
+
+ - GPU process canvas rendering is enabled (but DOM rendering is
+ disabled)
+ - we have accelerated ImageBuffers for both layer backing stores and
+ canvas backing stores
+ - the page does canvas drawing between steps 2 and 3, which affects
+ the contents of a canvas backing store IOSurface that was drawn into
+ a layer backing store IOSurface while building the transaction
+
+ The way we draw the canvas contents into the layer is by creating a
+ CGImage from the canvas backing store ImageBuffer's IOSurface.
+ Normally, if we draw into an IOSurface that has been wrapped by a
+ CGImage, this will cause the CGImage to obtain a unique copy of the
+ IOSurface's pixels, rather than continue holding on to the IOSurface
+ itself.
+
+ When the drawing of this CGImage on to the layer backing store
+ IOSurface happens, the "draw image" command is queued up to be
+ processed later. It's only when the flush happens in step 3 above that
+ the drawing occurs. This means that the CGImage wrapping the IOSurface
+ exists until that flush.
+
+ But while the CGImage does exist, it's in the Web process, and the
+ canvas drawing on to the same IOSurface happens in the GPU process.
+ Since QuartzCore doesn't know of its existence, it doesn't cause the
+ copy-on-write to occur. The effect of this is that subsequent page
+ drawing on to the canvas can make its way into the layer transaction
+ unexpectedly.
+
+ This patch induces the copy-on-write in the Web process when needed,
+ by tracking whether a canvas has been drawn into a layer backing
+ store and the layer transaction flush hasn't happened yet. Just before
+ we do any more drawing on the canvas, if we are in this state, we
+ perform a no-op drawing command on the IOSurface, then flush, to make
+ the CGImage copy the IOSurface data out.
+
+ * html/HTMLCanvasElement.cpp:
+ (WebCore::imageDrawingRequiresGuardAgainstUseByPendingLayerTransaction):
+ We only need to induce the CGImage copy-on-write behavior if both the
+ layer and canvas backing stores are accelerated, the canvas
+ ImageBuffer is remote, and the layer ImageBuffer is not remote.
+ (WebCore::HTMLCanvasElement::paint):
+ (WebCore::HTMLCanvasElement::drawingContext const):
+ Use drawingContext as a convenient single place to check whether we
+ are performing the first drawing command before the layer tree
+ transaction flush has happened.
+ * html/HTMLCanvasElement.h:
+ * page/Page.h:
+ (WebCore::Page::setIsAwaitingLayerTreeTransactionFlush):
+ (WebCore::Page::isAwaitingLayerTreeTransactionFlush const):
+ * platform/graphics/ImageBufferBackend.h:
+ (WebCore::ImageBufferBackend::ensureNativeImagesHaveCopiedBackingStore):
+ * platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp:
+ (WebCore::ImageBufferIOSurfaceBackend::ensureNativeImagesHaveCopiedBackingStore):
+ The flush is needed since the CGImage wrapping the IOSurface will only
+ copy the pixel data out once the no-op drawing command is processed.
+ * platform/graphics/cg/ImageBufferIOSurfaceBackend.h:
+
2022-01-15 Sam Weinig <[email protected]>
CSS Gradients: interpolation mode should default to OKLab if any non-legacy color syntax colors are used in the stops
Modified: trunk/Source/WebCore/html/HTMLCanvasElement.cpp (288075 => 288076)
--- trunk/Source/WebCore/html/HTMLCanvasElement.cpp 2022-01-16 21:44:55 UTC (rev 288075)
+++ trunk/Source/WebCore/html/HTMLCanvasElement.cpp 2022-01-16 22:10:02 UTC (rev 288076)
@@ -601,7 +601,19 @@
return true;
}
+#if PLATFORM(COCOA)
+static bool imageDrawingRequiresGuardAgainstUseByPendingLayerTransaction(GraphicsContext& context, const ImageBuffer& imageBuffer)
+{
+ if (context.renderingMode() != RenderingMode::Accelerated || !context.hasPlatformContext())
+ return false;
+ if (imageBuffer.renderingMode() != RenderingMode::Accelerated || imageBuffer.context().hasPlatformContext())
+ return false;
+
+ return true;
+}
+#endif
+
void HTMLCanvasElement::paint(GraphicsContext& context, const LayoutRect& r)
{
if (m_context)
@@ -620,8 +632,12 @@
if (shouldPaint) {
if (hasCreatedImageBuffer()) {
- if (ImageBuffer* imageBuffer = buffer())
+ if (ImageBuffer* imageBuffer = buffer()) {
context.drawImageBuffer(*imageBuffer, snappedIntRect(r));
+#if PLATFORM(COCOA)
+ m_mustGuardAgainstUseByPendingLayerTransaction |= imageDrawingRequiresGuardAgainstUseByPendingLayerTransaction(context, *imageBuffer);
+#endif
+ }
}
}
}
@@ -1040,4 +1056,25 @@
return m_context && m_context->isPlaceholder();
}
+#if PLATFORM(COCOA)
+GraphicsContext* HTMLCanvasElement::drawingContext() const
+{
+ auto context = CanvasBase::drawingContext();
+ if (!context)
+ return nullptr;
+
+ if (m_mustGuardAgainstUseByPendingLayerTransaction) {
+ if (auto page = document().page()) {
+ if (page->isAwaitingLayerTreeTransactionFlush()) {
+ if (auto backend = buffer()->backend())
+ backend->ensureNativeImagesHaveCopiedBackingStore();
+ }
+ }
+ m_mustGuardAgainstUseByPendingLayerTransaction = false;
+ }
+
+ return context;
}
+#endif
+
+}
Modified: trunk/Source/WebCore/html/HTMLCanvasElement.h (288075 => 288076)
--- trunk/Source/WebCore/html/HTMLCanvasElement.h 2022-01-16 21:44:55 UTC (rev 288075)
+++ trunk/Source/WebCore/html/HTMLCanvasElement.h 2022-01-16 22:10:02 UTC (rev 288076)
@@ -139,6 +139,10 @@
WEBCORE_EXPORT static size_t maxActivePixelMemory();
+#if PLATFORM(COCOA)
+ GraphicsContext* drawingContext() const final;
+#endif
+
private:
HTMLCanvasElement(const QualifiedName&, Document&);
@@ -192,6 +196,9 @@
bool m_hasRelevantWebGLEventListener { false };
#endif
bool m_isSnapshotting { false };
+#if PLATFORM(COCOA)
+ mutable bool m_mustGuardAgainstUseByPendingLayerTransaction { false };
+#endif
};
} // namespace WebCore
Modified: trunk/Source/WebCore/page/Page.h (288075 => 288076)
--- trunk/Source/WebCore/page/Page.h 2022-01-16 21:44:55 UTC (rev 288075)
+++ trunk/Source/WebCore/page/Page.h 2022-01-16 22:10:02 UTC (rev 288076)
@@ -927,6 +927,11 @@
void setAccessibilityRootObject(AccessibilityRootAtspi* rootObject) { m_accessibilityRootObject = rootObject; }
#endif
+#if PLATFORM(COCOA)
+ void setIsAwaitingLayerTreeTransactionFlush(bool isAwaiting) { m_isAwaitingLayerTreeTransactionFlush = isAwaiting; }
+ bool isAwaitingLayerTreeTransactionFlush() const { return m_isAwaitingLayerTreeTransactionFlush; }
+#endif
+
private:
struct Navigation {
RegistrableDomain domain;
@@ -1188,6 +1193,10 @@
bool m_isEditableRegionEnabled { false };
#endif
+#if PLATFORM(COCOA)
+ bool m_isAwaitingLayerTreeTransactionFlush { false };
+#endif
+
Vector<OptionSet<RenderingUpdateStep>, 2> m_renderingUpdateRemainingSteps;
OptionSet<RenderingUpdateStep> m_unfulfilledRequestedSteps;
Modified: trunk/Source/WebCore/platform/graphics/ImageBufferBackend.h (288075 => 288076)
--- trunk/Source/WebCore/platform/graphics/ImageBufferBackend.h 2022-01-16 21:44:55 UTC (rev 288075)
+++ trunk/Source/WebCore/platform/graphics/ImageBufferBackend.h 2022-01-16 22:10:02 UTC (rev 288076)
@@ -129,6 +129,8 @@
static constexpr bool canMapBackingStore = true;
static constexpr RenderingMode renderingMode = RenderingMode::Unaccelerated;
+ virtual void ensureNativeImagesHaveCopiedBackingStore() { }
+
protected:
WEBCORE_EXPORT ImageBufferBackend(const Parameters&);
Modified: trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp (288075 => 288076)
--- trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp 2022-01-16 21:44:55 UTC (rev 288075)
+++ trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp 2022-01-16 22:10:02 UTC (rev 288076)
@@ -244,6 +244,12 @@
IOSurface::moveToPool(WTFMove(m_surface));
}
+void ImageBufferIOSurfaceBackend::ensureNativeImagesHaveCopiedBackingStore()
+{
+ invalidateCachedNativeImage();
+ flushContext();
+}
+
} // namespace WebCore
#endif // HAVE(IOSURFACE)
Modified: trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.h (288075 => 288076)
--- trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.h 2022-01-16 21:44:55 UTC (rev 288075)
+++ trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.h 2022-01-16 22:10:02 UTC (rev 288076)
@@ -69,6 +69,8 @@
VolatilityState setVolatile(bool) override;
void releaseBufferToPool() override;
+ void ensureNativeImagesHaveCopiedBackingStore() final;
+
static constexpr RenderingMode renderingMode = RenderingMode::Accelerated;
protected:
Modified: trunk/Source/WebKit/ChangeLog (288075 => 288076)
--- trunk/Source/WebKit/ChangeLog 2022-01-16 21:44:55 UTC (rev 288075)
+++ trunk/Source/WebKit/ChangeLog 2022-01-16 22:10:02 UTC (rev 288076)
@@ -1,3 +1,18 @@
+2022-01-16 Cameron McCormack <[email protected]>
+
+ Ensure in flight layer transactions don't accumulate further canvas drawing
+ https://bugs.webkit.org/show_bug.cgi?id=231163
+ <rdar://problem/83863292>
+
+ Reviewed by Simon Fraser.
+
+ * WebProcess/WebPage/WebPage.cpp:
+ (WebKit::WebPage::willCommitLayerTree):
+ (WebKit::WebPage::didFlushLayerTreeAtTime):
+ Inform the WebCore::Page about the state of the layer tree transaction
+ building.
+ * WebProcess/WebPage/WebPage.h:
+
2022-01-15 Chris Dumez <[email protected]>
Introduce dynamicDowncast<>() for convenience
Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (288075 => 288076)
--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp 2022-01-16 21:44:55 UTC (rev 288075)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp 2022-01-16 22:10:02 UTC (rev 288076)
@@ -4170,6 +4170,8 @@
if (!frameView)
return;
+ corePage()->setIsAwaitingLayerTreeTransactionFlush(true);
+
layerTransaction.setContentsSize(frameView->contentsSize());
layerTransaction.setScrollOrigin(frameView->scrollOrigin());
layerTransaction.setPageScaleFactor(corePage()->pageScaleFactor());
@@ -4227,6 +4229,7 @@
#else
UNUSED_PARAM(timestamp);
#endif
+ corePage()->setIsAwaitingLayerTreeTransactionFlush(false);
}
#endif