Title: [265116] trunk
Revision
265116
Author
[email protected]
Date
2020-07-30 16:22:55 -0700 (Thu, 30 Jul 2020)

Log Message

[WebGL] Safari snapshots of WebGL content in the tab picker don't work
https://bugs.webkit.org/show_bug.cgi?id=214452
<rdar://problem/21243082>

Source/WebCore:

Original patch by Justin Fan. Reviewed by Darin Adler.

Test is in TestWebKitAPI/Tests/WebKitCocoa/WKWebViewSnapshot.mm.

* html/HTMLCanvasElement.cpp:
(WebCore::HTMLCanvasElement::paint): Will properly request a paint when a snapshot is requested.
* html/HTMLCanvasElement.h:
* html/canvas/WebGLRenderingContextBase.cpp:
(WebCore::WebGLRenderingContextBase::paintRenderingResultsToCanvas): Only clear the last snapshot if readPixels will succeed.
* rendering/RenderHTMLCanvas.cpp:
(WebCore::RenderHTMLCanvas::paintReplaced):

Tools:

Test for snapshotting. Marked as PLATFORM(MAC) for now.

* TestWebKitAPI/Tests/WebKitCocoa/WKWebViewSnapshot.mm:
(TEST):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (265115 => 265116)


--- trunk/Source/WebCore/ChangeLog	2020-07-30 23:08:00 UTC (rev 265115)
+++ trunk/Source/WebCore/ChangeLog	2020-07-30 23:22:55 UTC (rev 265116)
@@ -1,3 +1,21 @@
+2020-07-30  Dean Jackson  <[email protected]>
+
+        [WebGL] Safari snapshots of WebGL content in the tab picker don't work
+        https://bugs.webkit.org/show_bug.cgi?id=214452
+        <rdar://problem/21243082>
+
+        Original patch by Justin Fan. Reviewed by Darin Adler.
+
+        Test is in TestWebKitAPI/Tests/WebKitCocoa/WKWebViewSnapshot.mm.
+
+        * html/HTMLCanvasElement.cpp:
+        (WebCore::HTMLCanvasElement::paint): Will properly request a paint when a snapshot is requested.
+        * html/HTMLCanvasElement.h:
+        * html/canvas/WebGLRenderingContextBase.cpp:
+        (WebCore::WebGLRenderingContextBase::paintRenderingResultsToCanvas): Only clear the last snapshot if readPixels will succeed.
+        * rendering/RenderHTMLCanvas.cpp:
+        (WebCore::RenderHTMLCanvas::paintReplaced):
+
 2020-07-30  Jer Noble  <[email protected]>
 
         [Cocoa] Adopt -[AVContentKeyRequest willOutputBeObscuredDueToInsufficientExternalProtectionForDisplays:]

Modified: trunk/Source/WebCore/html/HTMLCanvasElement.cpp (265115 => 265116)


--- trunk/Source/WebCore/html/HTMLCanvasElement.cpp	2020-07-30 23:08:00 UTC (rev 265115)
+++ trunk/Source/WebCore/html/HTMLCanvasElement.cpp	2020-07-30 23:22:55 UTC (rev 265116)
@@ -635,7 +635,7 @@
         bool shouldPaint = true;
 
         if (m_context) {
-            shouldPaint = paintsIntoCanvasBuffer() || document().printing();
+            shouldPaint = paintsIntoCanvasBuffer() || document().printing() || m_isSnapshotting;
             if (shouldPaint)
                 m_context->paintRenderingResultsToCanvas();
         }

Modified: trunk/Source/WebCore/html/HTMLCanvasElement.h (265115 => 265116)


--- trunk/Source/WebCore/html/HTMLCanvasElement.h	2020-07-30 23:08:00 UTC (rev 265115)
+++ trunk/Source/WebCore/html/HTMLCanvasElement.h	2020-07-30 23:22:55 UTC (rev 265116)
@@ -132,6 +132,9 @@
     bool needsPreparationForDisplay();
     void prepareForDisplay();
 
+    void setIsSnapshotting(bool isSnapshotting) { m_isSnapshotting = isSnapshotting; }
+    bool isSnapshotting() const { return m_isSnapshotting; }
+
 private:
     HTMLCanvasElement(const QualifiedName&, Document&);
 
@@ -188,6 +191,8 @@
     bool m_hasRelevantWebGLEventListener { false };
 #endif
 
+    bool m_isSnapshotting { false };
+
     mutable RefPtr<Image> m_presentedImage;
     mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue).
 };

Modified: trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp (265115 => 265116)


--- trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp	2020-07-30 23:08:00 UTC (rev 265115)
+++ trunk/Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp	2020-07-30 23:22:55 UTC (rev 265116)
@@ -1188,13 +1188,13 @@
     if (!canvas)
         return;
 
-    if (canvas->document().printing())
+    if (canvas->document().printing() || (canvas->isSnapshotting() && canvas->document().page()->isVisible()))
         canvas->clearPresentationCopy();
 
     // Until the canvas is written to by the application, the clear that
     // happened after it was composited should be ignored by the compositor.
     if (m_context->layerComposited() && !m_attributes.preserveDrawingBuffer) {
-        m_context->paintCompositedResultsToCanvas(canvas->buffer());
+        m_context->paintRenderingResultsToCanvas(canvas->buffer());
 
         canvas->makePresentationCopy();
     } else

Modified: trunk/Source/WebCore/rendering/RenderHTMLCanvas.cpp (265115 => 265116)


--- trunk/Source/WebCore/rendering/RenderHTMLCanvas.cpp	2020-07-30 23:08:00 UTC (rev 265115)
+++ trunk/Source/WebCore/rendering/RenderHTMLCanvas.cpp	2020-07-30 23:22:55 UTC (rev 265116)
@@ -92,7 +92,10 @@
         page().addRelevantRepaintedObject(this, intersection(replacedContentRect, contentBoxRect));
 
     InterpolationQualityMaintainer interpolationMaintainer(context, ImageQualityController::interpolationQualityFromStyle(style()));
+
+    canvasElement().setIsSnapshotting(paintInfo.paintBehavior.contains(PaintBehavior::Snapshotting));
     canvasElement().paint(context, replacedContentRect);
+    canvasElement().setIsSnapshotting(false);
 }
 
 void RenderHTMLCanvas::canvasSizeChanged()

Modified: trunk/Tools/ChangeLog (265115 => 265116)


--- trunk/Tools/ChangeLog	2020-07-30 23:08:00 UTC (rev 265115)
+++ trunk/Tools/ChangeLog	2020-07-30 23:22:55 UTC (rev 265116)
@@ -1,3 +1,14 @@
+2020-07-30  Dean Jackson  <[email protected]>
+
+        [WebGL] Safari snapshots of WebGL content in the tab picker don't work
+        https://bugs.webkit.org/show_bug.cgi?id=214452
+        <rdar://problem/21243082>
+
+        Test for snapshotting. Marked as PLATFORM(MAC) for now.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WKWebViewSnapshot.mm:
+        (TEST):
+
 2020-07-30  Jer Noble  <[email protected]>
 
         [Cocoa] Adopt -[AVContentKeyRequest willOutputBeObscuredDueToInsufficientExternalProtectionForDisplays:]

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewSnapshot.mm (265115 => 265116)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewSnapshot.mm	2020-07-30 23:08:00 UTC (rev 265115)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewSnapshot.mm	2020-07-30 23:22:55 UTC (rev 265116)
@@ -420,3 +420,63 @@
     
     TestWebKitAPI::Util::run(&isDone);
 }
+
+// FIXME: Confirm that this works on iOS too.
+#if PLATFORM(MAC)
+TEST(WKWebView, SnapshotWebGL)
+{
+    NSInteger viewWidth = 800;
+    NSInteger viewHeight = 600;
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, viewWidth, viewHeight)]);
+
+    RetainPtr<PlatformWindow> window;
+    CGFloat backingScaleFactor;
+
+#if PLATFORM(MAC)
+    window = adoptNS([[NSWindow alloc] initWithContentRect:[webView frame] styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]);
+    [[window contentView] addSubview:webView.get()];
+    backingScaleFactor = [window backingScaleFactor];
+#elif PLATFORM(IOS_FAMILY)
+    window = adoptNS([[UIWindow alloc] initWithFrame:[webView frame]]);
+    [window addSubview:webView.get()];
+    backingScaleFactor = [[window screen] scale];
+#endif
+
+    [webView loadHTMLString:@"<style>body{margin:0;padding:0}</style><canvas></canvas><script>window.addEventListener('load', () => { let ctx = document.querySelector('canvas').getContext('webgl'); ctx.clearColor(0, 1, 0, 1); ctx.clear(ctx.COLOR_BUFFER_BIT); }, false);</script>" baseURL:nil];
+    [webView _test_waitForDidFinishNavigation];
+
+    RetainPtr<WKSnapshotConfiguration> snapshotConfiguration = adoptNS([[WKSnapshotConfiguration alloc] init]);
+    [snapshotConfiguration setRect:NSMakeRect(0, 0, viewWidth, viewHeight)];
+    [snapshotConfiguration setSnapshotWidth:@(viewWidth)];
+    [snapshotConfiguration setAfterScreenUpdates:YES];
+
+    isDone = false;
+    [webView takeSnapshotWithConfiguration:snapshotConfiguration.get() completionHandler:^(PlatformImage snapshotImage, NSError *error) {
+        EXPECT_NULL(error);
+
+        EXPECT_EQ(viewWidth, snapshotImage.size.width);
+
+        RetainPtr<CGImageRef> cgImage = convertToCGImage(snapshotImage);
+        RetainPtr<CGColorSpaceRef> colorSpace = adoptCF(CGColorSpaceCreateDeviceRGB());
+
+        NSInteger viewWidthInPixels = viewWidth * backingScaleFactor;
+        NSInteger viewHeightInPixels = viewHeight * backingScaleFactor;
+
+        uint8_t *rgba = (unsigned char *)calloc(viewWidthInPixels * viewHeightInPixels * 4, sizeof(unsigned char));
+        RetainPtr<CGContextRef> context = CGBitmapContextCreate(rgba, viewWidthInPixels, viewHeightInPixels, 8, 4 * viewWidthInPixels, colorSpace.get(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
+        CGContextDrawImage(context.get(), CGRectMake(0, 0, viewWidthInPixels, viewHeightInPixels), cgImage.get());
+
+        NSInteger pixelIndex = getPixelIndex(0, 0, viewWidthInPixels);
+        EXPECT_EQ(0, rgba[pixelIndex]);
+        EXPECT_EQ(255, rgba[pixelIndex + 1]);
+        EXPECT_EQ(0, rgba[pixelIndex + 2]);
+        EXPECT_EQ(255, rgba[pixelIndex + 3]);
+
+        free(rgba);
+
+        isDone = true;
+    }];
+
+    TestWebKitAPI::Util::run(&isDone);
+}
+#endif
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to