Title: [217492] branches/safari-604.1.21-branch
Revision
217492
Author
[email protected]
Date
2017-05-26 10:03:38 -0700 (Fri, 26 May 2017)

Log Message

Cherry-pick r217296. rdar://problem/32414363

Modified Paths

Added Paths

Diff

Modified: branches/safari-604.1.21-branch/Source/WebCore/ChangeLog (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/ChangeLog	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/ChangeLog	2017-05-26 17:03:38 UTC (rev 217492)
@@ -1,3 +1,108 @@
+2017-05-26  Matthew Hanson  <[email protected]>
+
+        Cherry-pick r217296. rdar://problem/32414363
+
+    2017-05-22  Simon Fraser  <[email protected]>
+
+            Snapshotting via -renderInContext: should do synchronous image decodes
+            https://bugs.webkit.org/show_bug.cgi?id=172485
+            rdar://problem/32276146
+
+            Reviewed by Tim Horton.
+
+            When UIWebView clients snapshot the view via -renderInContext: on the UIView's layer,
+            WebKit should do synchronous image decodes so that the images show in the snapshot.
+
+            Fix by plumbing through a "snapshotting" paint behavior flag from two source locations:
+            WebSimpleLayer, used by compositing layers, and LegacyTileLayer, used for the WK1 tile cache.
+            On WebSimpleLayer the flag propagates through PlatformCALayer to GraphicsLayer, and on LegacyTileLayer
+            it sets some state on the WAKWindow, which is detected by -[WebFrame _paintBehaviorForDestinationContext:].
+            In both cases, the flag becomes PaintBehaviorSnapshotting which is passed down through painting
+            in PaintInfo.paintBehavior.
+
+            Fix code that was checking paintBehavior on the FrameView to just looking at PaintInfo.paintBehavior,
+            which required passing PaintInfo along in RenderImage::paintIntoRect().
+
+            We can't just use the PaintBehaviorFlattenCompositingLayers flag, because that triggers
+            a flattening paint of compositing layers, and we don't want that behavior for -renderInContext:,
+            since CoreAnimation calls -renderInContext: on each of our layers and we need to avoid double-painting.
+            So PaintBehaviorSnapshotting is now added to the call sites that also set PaintBehaviorFlattenCompositingLayers
+            in most cases.
+
+            Tested by SnapshotViaRenderInContext API test.
+
+            * html/shadow/MediaControlElements.cpp:
+            (WebCore::MediaControlTextTrackContainerElement::createTextTrackRepresentationImage):
+            * page/FrameView.cpp:
+            (WebCore::FrameView::willPaintContents):
+            (WebCore::FrameView::paintContentsForSnapshot):
+            * page/PageOverlayController.cpp:
+            (WebCore::PageOverlayController::paintContents):
+            * page/PageOverlayController.h:
+            * page/mac/ServicesOverlayController.h:
+            * page/mac/ServicesOverlayController.mm:
+            (WebCore::ServicesOverlayController::Highlight::paintContents):
+            * platform/graphics/GraphicsLayer.cpp:
+            (WebCore::GraphicsLayer::paintGraphicsLayerContents):
+            * platform/graphics/GraphicsLayer.h:
+            * platform/graphics/GraphicsLayerClient.h:
+            (WebCore::GraphicsLayerClient::paintContents):
+            * platform/graphics/ca/GraphicsLayerCA.cpp:
+            (WebCore::GraphicsLayerCA::recursiveCommitChanges):
+            (WebCore::GraphicsLayerCA::platformCALayerPaintContents):
+            * platform/graphics/ca/GraphicsLayerCA.h:
+            * platform/graphics/ca/PlatformCALayer.h:
+            * platform/graphics/ca/PlatformCALayerClient.h:
+            * platform/graphics/ca/TileCoverageMap.cpp:
+            (WebCore::TileCoverageMap::platformCALayerPaintContents):
+            * platform/graphics/ca/TileCoverageMap.h:
+            * platform/graphics/ca/TileGrid.cpp:
+            (WebCore::TileGrid::platformCALayerPaintContents):
+            * platform/graphics/ca/TileGrid.h:
+            * platform/graphics/ca/cocoa/PlatformCALayerCocoa.mm:
+            (PlatformCALayer::drawLayerContents):
+            * platform/graphics/mac/WebLayer.mm:
+            (-[WebLayer drawInContext:]):
+            (-[WebSimpleLayer renderInContext:]):
+            (-[WebSimpleLayer drawInContext:]):
+            * platform/ios/LegacyTileCache.h: Use initializers.
+            * platform/ios/LegacyTileCache.mm:
+            (WebCore::LegacyTileCache::LegacyTileCache):
+            (WebCore::LegacyTileCache::drawWindowContent):
+            (WebCore::LegacyTileCache::drawLayer):
+            * platform/ios/LegacyTileLayer.h:
+            * platform/ios/LegacyTileLayer.mm:
+            (-[LegacyTileHostLayer renderInContext:]):
+            (-[LegacyTileLayer renderInContext:]):
+            (-[LegacyTileLayer drawInContext:]):
+            (+[LegacyTileLayer layerBeingPainted]): Deleted. There were no callers.
+            * platform/ios/wak/WAKView.mm:
+            (-[WAKView _drawRect:context:lockFocus:]): bool -> BOOL
+            * platform/ios/wak/WAKWindow.h:
+            * platform/ios/wak/WAKWindow.mm:
+            (-[WAKWindow setIsInSnapshottingPaint:]):
+            (-[WAKWindow isInSnapshottingPaint]):
+            * rendering/PaintPhase.h:
+            * rendering/RenderBox.cpp:
+            (WebCore::RenderBox::paintMaskImages):
+            * rendering/RenderBoxModelObject.cpp:
+            (WebCore::RenderBoxModelObject::paintFillLayerExtended):
+            * rendering/RenderImage.cpp:
+            (WebCore::RenderImage::paintReplaced):
+            (WebCore::RenderImage::paintIntoRect):
+            * rendering/RenderImage.h:
+            * rendering/RenderLayer.cpp:
+            (WebCore::RenderLayer::paintLayerContents):
+            (WebCore::RenderLayer::paintForegroundForFragments):
+            * rendering/RenderLayerBacking.cpp:
+            (WebCore::RenderLayerBacking::paintContents):
+            * rendering/RenderLayerBacking.h:
+            * rendering/RenderLayerCompositor.cpp:
+            (WebCore::RenderLayerCompositor::paintContents):
+            * rendering/RenderLayerCompositor.h:
+            * rendering/RenderVideo.cpp:
+            (WebCore::RenderVideo::paintReplaced):
+
 2017-05-24  Jason Marcell  <[email protected]>
 
         Cherry-pick r217382. rdar://problem/32380083

Modified: branches/safari-604.1.21-branch/Source/WebCore/html/shadow/MediaControlElements.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/html/shadow/MediaControlElements.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/html/shadow/MediaControlElements.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -1402,7 +1402,7 @@
     if (!buffer)
         return nullptr;
 
-    layer->paint(buffer->context(), paintingRect, LayoutSize(), PaintBehaviorFlattenCompositingLayers, nullptr, RenderLayer::PaintLayerPaintingCompositingAllPhases);
+    layer->paint(buffer->context(), paintingRect, LayoutSize(), PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting, nullptr, RenderLayer::PaintLayerPaintingCompositingAllPhases);
 
     return ImageBuffer::sinkIntoImage(WTFMove(buffer));
 }

Modified: branches/safari-604.1.21-branch/Source/WebCore/page/FrameView.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/page/FrameView.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/page/FrameView.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -4373,10 +4373,13 @@
     if (FrameView* parentView = parentFrameView()) {
         if (parentView->paintBehavior() & PaintBehaviorFlattenCompositingLayers)
             m_paintBehavior |= PaintBehaviorFlattenCompositingLayers;
+            
+        if (parentView->paintBehavior() & PaintBehaviorSnapshotting)
+            m_paintBehavior |= PaintBehaviorSnapshotting;
     }
 
     if (document->printing())
-        m_paintBehavior |= PaintBehaviorFlattenCompositingLayers;
+        m_paintBehavior |= (PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting);
 
     paintingState.isFlatteningPaintOfRootFrame = (m_paintBehavior & PaintBehaviorFlattenCompositingLayers) && !frame().ownerElement();
     if (paintingState.isFlatteningPaintOfRootFrame)
@@ -4503,7 +4506,7 @@
 
     // Cache paint behavior and set a new behavior appropriate for snapshots.
     PaintBehavior oldBehavior = paintBehavior();
-    setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
+    setPaintBehavior(oldBehavior | (PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting));
 
     // If the snapshot should exclude selection, then we'll clear the current selection
     // in the render tree only. This will allow us to restore the selection from the DOM

Modified: branches/safari-604.1.21-branch/Source/WebCore/page/PageOverlayController.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/page/PageOverlayController.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/page/PageOverlayController.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -360,7 +360,7 @@
     return { };
 }
 
-void PageOverlayController::paintContents(const WebCore::GraphicsLayer* graphicsLayer, WebCore::GraphicsContext& graphicsContext, WebCore::GraphicsLayerPaintingPhase, const WebCore::FloatRect& clipRect)
+void PageOverlayController::paintContents(const WebCore::GraphicsLayer* graphicsLayer, WebCore::GraphicsContext& graphicsContext, WebCore::GraphicsLayerPaintingPhase, const WebCore::FloatRect& clipRect, GraphicsLayerPaintFlags)
 {
     for (auto& overlayAndGraphicsLayer : m_overlayGraphicsLayers) {
         if (overlayAndGraphicsLayer.value.get() != graphicsLayer)

Modified: branches/safari-604.1.21-branch/Source/WebCore/page/PageOverlayController.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/page/PageOverlayController.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/page/PageOverlayController.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -88,7 +88,7 @@
 
     // GraphicsLayerClient
     void notifyFlushRequired(const GraphicsLayer*) override;
-    void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clipRect) override;
+    void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clipRect, GraphicsLayerPaintFlags) override;
     float deviceScaleFactor() const override;
     bool shouldSkipLayerInDump(const GraphicsLayer*, LayerTreeAsTextBehavior) const override;
     void tiledBackingUsageChanged(const GraphicsLayer*, bool) override;

Modified: branches/safari-604.1.21-branch/Source/WebCore/page/linux/ResourceUsageOverlayLinux.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/page/linux/ResourceUsageOverlayLinux.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/page/linux/ResourceUsageOverlayLinux.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -85,7 +85,7 @@
     }
 
 private:
-    void paintContents(const GraphicsLayer*, GraphicsContext& context, GraphicsLayerPaintingPhase, const FloatRect& clip) override
+    void paintContents(const GraphicsLayer*, GraphicsContext& context, GraphicsLayerPaintingPhase, const FloatRect& clip, GraphicsLayerPaintFlags) override
     {
         GraphicsContextStateSaver stateSaver(context);
         context.fillRect(clip, Color(0.0f, 0.0f, 0.0f, 0.8f));

Modified: branches/safari-604.1.21-branch/Source/WebCore/page/mac/ServicesOverlayController.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/page/mac/ServicesOverlayController.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/page/mac/ServicesOverlayController.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -82,7 +82,7 @@
 
         // GraphicsLayerClient
         void notifyFlushRequired(const GraphicsLayer*) override;
-        void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& inClip) override;
+        void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& inClip, GraphicsLayerPaintFlags) override;
         float deviceScaleFactor() const override;
 
         void didFinishFadeOutAnimation();

Modified: branches/safari-604.1.21-branch/Source/WebCore/page/mac/ServicesOverlayController.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/page/mac/ServicesOverlayController.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/page/mac/ServicesOverlayController.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -129,7 +129,7 @@
     page->chrome().client().scheduleCompositingLayerFlush();
 }
 
-void ServicesOverlayController::Highlight::paintContents(const GraphicsLayer*, GraphicsContext& graphicsContext, GraphicsLayerPaintingPhase, const FloatRect&)
+void ServicesOverlayController::Highlight::paintContents(const GraphicsLayer*, GraphicsContext& graphicsContext, GraphicsLayerPaintingPhase, const FloatRect&, GraphicsLayerPaintFlags)
 {
     if (!DataDetectorsLibrary())
         return;

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/GraphicsLayer.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/GraphicsLayer.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/GraphicsLayer.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -417,7 +417,7 @@
     m_backgroundColor = color;
 }
 
-void GraphicsLayer::paintGraphicsLayerContents(GraphicsContext& context, const FloatRect& clip)
+void GraphicsLayer::paintGraphicsLayerContents(GraphicsContext& context, const FloatRect& clip, GraphicsLayerPaintFlags flags)
 {
     FloatSize offset = offsetFromRenderer();
     context.translate(-offset);
@@ -425,7 +425,7 @@
     FloatRect clipRect(clip);
     clipRect.move(offset);
 
-    m_client.paintContents(this, context, m_paintingPhase, clipRect);
+    m_client.paintContents(this, context, m_paintingPhase, clipRect, flags);
 }
 
 String GraphicsLayer::animationNameForTransition(AnimatedPropertyID property)

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/GraphicsLayer.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/GraphicsLayer.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/GraphicsLayer.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -442,7 +442,7 @@
 
     // Transitions are identified by a special animation name that cannot clash with a keyframe identifier.
     static String animationNameForTransition(AnimatedPropertyID);
-    
+
     // Return true if the animation is handled by the compositing system. If this returns
     // false, the animation will be run by CSSAnimationController.
     // These methods handle both transitions and keyframe animations.
@@ -452,7 +452,7 @@
 
     WEBCORE_EXPORT virtual void suspendAnimations(double time);
     WEBCORE_EXPORT virtual void resumeAnimations();
-    
+
     // Layer contents
     virtual void setContentsToImage(Image*) { }
     virtual bool shouldDirectlyCompositeImage(Image*) const { return true; }
@@ -465,11 +465,11 @@
     virtual bool usesContentsLayer() const { return false; }
 
     // Callback from the underlying graphics system to draw layer contents.
-    void paintGraphicsLayerContents(GraphicsContext&, const FloatRect& clip);
-    
+    void paintGraphicsLayerContents(GraphicsContext&, const FloatRect& clip, GraphicsLayerPaintFlags = GraphicsLayerPaintFlags::None);
+
     // For hosting this GraphicsLayer in a native layer hierarchy.
     virtual PlatformLayer* platformLayer() const { return 0; }
-    
+
     enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp };
 
     // Flippedness of the contents of this layer. Does not affect sublayer geometry.

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/GraphicsLayerClient.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/GraphicsLayerClient.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/GraphicsLayerClient.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -23,8 +23,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  */
 
-#ifndef GraphicsLayerClient_h
-#define GraphicsLayerClient_h
+#pragma once
 
 #include "IntSize.h"
 #include "TiledBacking.h"
@@ -76,6 +75,8 @@
 };
 typedef unsigned LayerTreeAsTextBehavior;
 
+enum class GraphicsLayerPaintFlags { None, Snapshotting };
+    
 class GraphicsLayerClient {
 public:
     virtual ~GraphicsLayerClient() {}
@@ -93,7 +94,7 @@
     // Notification that this layer requires a flush before the next display refresh.
     virtual void notifyFlushBeforeDisplayRefresh(const GraphicsLayer*) { }
 
-    virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& /* inClip */) { }
+    virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& /* inClip */, GraphicsLayerPaintFlags) { }
     virtual void didCommitChangesForLayer(const GraphicsLayer*) const { }
 
     // Provides current transform (taking transform-origin and animations into account). Input matrix has been
@@ -139,4 +140,3 @@
 
 } // namespace WebCore
 
-#endif // GraphicsLayerClient_h

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/avfoundation/cf/MediaPlayerPrivateAVFoundationCF.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/avfoundation/cf/MediaPlayerPrivateAVFoundationCF.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/avfoundation/cf/MediaPlayerPrivateAVFoundationCF.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -220,7 +220,7 @@
 
     virtual void platformCALayerAnimationStarted(CFTimeInterval beginTime) { }
     virtual GraphicsLayer::CompositingCoordinatesOrientation platformCALayerContentsOrientation() const { return GraphicsLayer::CompositingCoordinatesBottomUp; }
-    virtual void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect&) { }
+    virtual void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect&, GraphicsLayerPaintFlags) { }
     virtual bool platformCALayerShowDebugBorders() const { return false; }
     virtual bool platformCALayerShowRepaintCounter(PlatformCALayer*) const { return false; }
     virtual int platformCALayerIncrementRepaintCount(PlatformCALayer*) { return 0; }

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/GraphicsLayerCA.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/GraphicsLayerCA.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/GraphicsLayerCA.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -1563,7 +1563,7 @@
     return isShowingRepaintCounter();
 }
 
-void GraphicsLayerCA::platformCALayerPaintContents(PlatformCALayer*, GraphicsContext& context, const FloatRect& clip)
+void GraphicsLayerCA::platformCALayerPaintContents(PlatformCALayer*, GraphicsContext& context, const FloatRect& clip, GraphicsLayerPaintFlags flags)
 {
     m_hasEverPainted = true;
     if (m_displayList) {
@@ -1579,7 +1579,7 @@
     }
 
     TraceScope tracingScope(PaintLayerStart, PaintLayerEnd);
-    paintGraphicsLayerContents(context, clip);
+    paintGraphicsLayerContents(context, clip, flags);
 }
 
 void GraphicsLayerCA::platformCALayerSetNeedsToRevalidateTiles()

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/GraphicsLayerCA.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/GraphicsLayerCA.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/GraphicsLayerCA.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -187,7 +187,7 @@
     WEBCORE_EXPORT void platformCALayerAnimationStarted(const String& animationKey, CFTimeInterval beginTime) override;
     WEBCORE_EXPORT void platformCALayerAnimationEnded(const String& animationKey) override;
     CompositingCoordinatesOrientation platformCALayerContentsOrientation() const override { return contentsOrientation(); }
-    WEBCORE_EXPORT void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect& clip) override;
+    WEBCORE_EXPORT void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect& clip, GraphicsLayerPaintFlags) override;
     bool platformCALayerShowDebugBorders() const override { return isShowingDebugBorder(); }
     WEBCORE_EXPORT bool platformCALayerShowRepaintCounter(PlatformCALayer*) const override;
     int platformCALayerIncrementRepaintCount(PlatformCALayer*) override { return incrementRepaintCount(); }

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/PlatformCALayer.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/PlatformCALayer.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/PlatformCALayer.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -278,7 +278,7 @@
         
     // Functions allows us to share implementation across WebTiledLayer and WebLayer
     static RepaintRectList collectRectsToPaint(CGContextRef, PlatformCALayer*);
-    static void drawLayerContents(CGContextRef, PlatformCALayer*, RepaintRectList& dirtyRects);
+    static void drawLayerContents(CGContextRef, PlatformCALayer*, RepaintRectList& dirtyRects, GraphicsLayerPaintFlags);
     static void drawRepaintIndicator(CGContextRef, PlatformCALayer*, int repaintCount, CGColorRef customBackgroundColor);
     static CGRect frameForLayer(const PlatformLayer*);
 

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/PlatformCALayerClient.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/PlatformCALayerClient.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/PlatformCALayerClient.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -23,8 +23,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  */
 
-#ifndef PlatformCALayerClient_h
-#define PlatformCALayerClient_h
+#pragma once
 
 #include "GraphicsLayer.h"
 
@@ -44,7 +43,7 @@
     virtual void platformCALayerAnimationStarted(const String& /*animationKey*/, CFTimeInterval) { }
     virtual void platformCALayerAnimationEnded(const String& /*animationKey*/) { }
     virtual GraphicsLayer::CompositingCoordinatesOrientation platformCALayerContentsOrientation() const { return GraphicsLayer::CompositingCoordinatesTopDown; }
-    virtual void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect& inClip) = 0;
+    virtual void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect& inClip, GraphicsLayerPaintFlags) = 0;
     virtual bool platformCALayerShowDebugBorders() const { return false; }
     virtual bool platformCALayerShowRepaintCounter(PlatformCALayer*) const { return false; }
     virtual int platformCALayerIncrementRepaintCount(PlatformCALayer*) { return 0; }
@@ -72,4 +71,3 @@
 
 }
 
-#endif // PlatformCALayerClient_h

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileCoverageMap.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileCoverageMap.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileCoverageMap.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -152,7 +152,7 @@
     m_visibleViewportIndicatorLayer.get().setBorderColor(visibleRectIndicatorColor);
 }
 
-void TileCoverageMap::platformCALayerPaintContents(PlatformCALayer* platformCALayer, GraphicsContext& context, const FloatRect&)
+void TileCoverageMap::platformCALayerPaintContents(PlatformCALayer* platformCALayer, GraphicsContext& context, const FloatRect&, GraphicsLayerPaintFlags)
 {
     ASSERT_UNUSED(platformCALayer, platformCALayer == m_layer.ptr());
     m_controller.tileGrid().drawTileMapContents(context.platformContext(), m_layer.get().bounds());

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileCoverageMap.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileCoverageMap.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileCoverageMap.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -61,7 +61,7 @@
     GraphicsLayer::CompositingCoordinatesOrientation platformCALayerContentsOrientation() const override { return GraphicsLayer::CompositingCoordinatesTopDown; }
     bool platformCALayerContentsOpaque() const override { return true; }
     bool platformCALayerDrawsContent() const override { return true; }
-    void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect&) override;
+    void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect&, GraphicsLayerPaintFlags) override;
     float platformCALayerDeviceScaleFactor() const override;
 
     void updateTimerFired();

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileGrid.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileGrid.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileGrid.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -712,7 +712,7 @@
     }
 }
 
-void TileGrid::platformCALayerPaintContents(PlatformCALayer* platformCALayer, GraphicsContext& context, const FloatRect&)
+void TileGrid::platformCALayerPaintContents(PlatformCALayer* platformCALayer, GraphicsContext& context, const FloatRect&, GraphicsLayerPaintFlags flags)
 {
 #if PLATFORM(IOS)
     if (pthread_main_np())
@@ -727,7 +727,7 @@
         context.scale(m_scale);
 
         PlatformCALayer::RepaintRectList dirtyRects = PlatformCALayer::collectRectsToPaint(context.platformContext(), platformCALayer);
-        PlatformCALayer::drawLayerContents(context.platformContext(), &m_controller.rootLayer(), dirtyRects);
+        PlatformCALayer::drawLayerContents(context.platformContext(), &m_controller.rootLayer(), dirtyRects, flags);
     }
 
     int repaintCount = platformCALayerIncrementRepaintCount(platformCALayer);

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileGrid.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileGrid.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/TileGrid.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -144,7 +144,7 @@
     void removeTiles(Vector<TileGrid::TileIndex>& toRemove);
 
     // PlatformCALayerClient
-    void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect&) override;
+    void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect&, GraphicsLayerPaintFlags) override;
     bool platformCALayerShowDebugBorders() const override;
     bool platformCALayerShowRepaintCounter(PlatformCALayer*) const override;
     int platformCALayerIncrementRepaintCount(PlatformCALayer*) override;

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/cocoa/PlatformCALayerCocoa.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/cocoa/PlatformCALayerCocoa.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/cocoa/PlatformCALayerCocoa.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -1112,7 +1112,7 @@
     return dirtyRects;
 }
 
-void PlatformCALayer::drawLayerContents(CGContextRef context, WebCore::PlatformCALayer* platformCALayer, RepaintRectList& dirtyRects)
+void PlatformCALayer::drawLayerContents(CGContextRef context, WebCore::PlatformCALayer* platformCALayer, RepaintRectList& dirtyRects, GraphicsLayerPaintFlags flags)
 {
     WebCore::PlatformCALayerClient* layerContents = platformCALayer->owner();
     if (!layerContents)
@@ -1159,7 +1159,7 @@
             GraphicsContextStateSaver stateSaver(graphicsContext);
             graphicsContext.clip(rect);
             
-            layerContents->platformCALayerPaintContents(platformCALayer, graphicsContext, rect);
+            layerContents->platformCALayerPaintContents(platformCALayer, graphicsContext, rect, flags);
         }
         
 #if PLATFORM(IOS)

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/win/PlatformCALayerWin.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/win/PlatformCALayerWin.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/win/PlatformCALayerWin.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -109,7 +109,7 @@
     return dirtyRects;
 }
 
-void PlatformCALayer::drawLayerContents(CGContextRef context, WebCore::PlatformCALayer* platformCALayer, RepaintRectList&)
+void PlatformCALayer::drawLayerContents(CGContextRef context, WebCore::PlatformCALayer* platformCALayer, RepaintRectList&, GraphicsLayerPaintFlags)
 {
     intern(platformCALayer)->displayCallback(platformCALayer->platformLayer(), context);
 }

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/win/PlatformCALayerWinInternal.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/win/PlatformCALayerWinInternal.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/win/PlatformCALayerWinInternal.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -104,7 +104,7 @@
     // smaller than the layer bounds (e.g. tiled layers)
     CGRect clipBounds = CGContextGetClipBoundingBox(context);
     IntRect clip(enclosingIntRect(clipBounds));
-    client->platformCALayerPaintContents(owner(), graphicsContext, clip);
+    client->platformCALayerPaintContents(owner(), graphicsContext, clip, GraphicsLayerPaintFlags::None);
 
     if (client->platformCALayerShowRepaintCounter(owner())
         && !repaintCountersAreDrawnByGridController(layerType)) {

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/win/WebTiledBackingLayerWin.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/win/WebTiledBackingLayerWin.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/ca/win/WebTiledBackingLayerWin.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -95,7 +95,7 @@
     // smaller than the layer bounds (e.g. tiled layers)
     CGRect clipBounds = CGContextGetClipBoundingBox(context);
     IntRect clip(enclosingIntRect(clipBounds));
-    client->platformCALayerPaintContents(owner(), graphicsContext, clip);
+    client->platformCALayerPaintContents(owner(), graphicsContext, clip, GraphicsLayerPaintFlags::None);
 
     if (client->platformCALayerShowRepaintCounter(owner())) {
         int drawCount = client->platformCALayerIncrementRepaintCount(owner());

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/mac/WebLayer.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/mac/WebLayer.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/mac/WebLayer.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -29,7 +29,10 @@
 #include <QuartzCore/QuartzCore.h>
 
 #ifdef __OBJC__
-@interface WebSimpleLayer : CALayer
+@interface WebSimpleLayer : CALayer {
+    BOOL _isRenderingInContext;
+}
+@property (nonatomic, readonly) BOOL isRenderingInContext;
 @end
 
 @interface WebLayer : WebSimpleLayer

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/mac/WebLayer.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/mac/WebLayer.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/graphics/mac/WebLayer.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -30,6 +30,7 @@
 #import "GraphicsLayerCA.h"
 #import "PlatformCALayer.h"
 #import <QuartzCore/QuartzCore.h>
+#import <wtf/SetForScope.h>
 
 #if PLATFORM(IOS)
 #import "WKGraphics.h"
@@ -56,7 +57,7 @@
     PlatformCALayer* layer = PlatformCALayer::platformCALayer(self);
     if (layer) {
         PlatformCALayer::RepaintRectList rectsToPaint = PlatformCALayer::collectRectsToPaint(context, layer);
-        PlatformCALayer::drawLayerContents(context, layer, rectsToPaint);
+        PlatformCALayer::drawLayerContents(context, layer, rectsToPaint, self.isRenderingInContext ? GraphicsLayerPaintFlags::Snapshotting : GraphicsLayerPaintFlags::None);
     }
 }
 
@@ -64,6 +65,14 @@
 
 @implementation WebSimpleLayer
 
+@synthesize isRenderingInContext = _isRenderingInContext;
+
+- (void)renderInContext:(CGContextRef)context
+{
+    SetForScope<BOOL> change(_isRenderingInContext, YES);
+    [super renderInContext:context];
+}
+
 - (id<CAAction>)actionForKey:(NSString *)key
 {
     // Fix for <rdar://problem/9015675>: Force the layer content to be updated when the tree is reparented.
@@ -128,7 +137,7 @@
         graphicsContext.setIsAcceleratedContext(layer->acceleratesDrawing());
 
         FloatRect clipBounds = CGContextGetClipBoundingBox(context);
-        layer->owner()->platformCALayerPaintContents(layer, graphicsContext, clipBounds);
+        layer->owner()->platformCALayerPaintContents(layer, graphicsContext, clipBounds, self.isRenderingInContext ? GraphicsLayerPaintFlags::Snapshotting : GraphicsLayerPaintFlags::None);
     }
 }
 

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileCache.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileCache.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileCache.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -133,7 +133,9 @@
 
     // Internal
     void doLayoutTiles();
-    void drawLayer(LegacyTileLayer*, CGContextRef);
+    
+    enum class DrawingFlags { None, Snapshotting };
+    void drawLayer(LegacyTileLayer*, CGContextRef, DrawingFlags);
     void prepareToDraw();
     void finishedCreatingTiles(bool didCreateTiles, bool createMore);
     FloatRect visibleRectInLayer(CALayer *) const;
@@ -172,33 +174,33 @@
     void tileCreationTimerFired();
 
     void drawReplacementImage(LegacyTileLayer*, CGContextRef, CGImageRef);
-    void drawWindowContent(LegacyTileLayer*, CGContextRef, CGRect dirtyRect);
+    void drawWindowContent(LegacyTileLayer*, CGContextRef, CGRect dirtyRect, DrawingFlags);
 
-    WAKWindow* m_window;
+    WAKWindow* m_window { nullptr };
 
     RetainPtr<CGImageRef> m_contentReplacementImage;
 
-    bool m_keepsZoomedOutTiles;
-
-    bool m_hasPendingLayoutTiles;
-    bool m_hasPendingUpdateTilingMode;
     // Ensure there are no async calls on a dead tile cache.
     RetainPtr<LegacyTileCacheTombstone> m_tombstone;
 
     std::optional<FloatRect> m_overrideVisibleRect;
 
-    TilingMode m_tilingMode;
-    TilingDirection m_tilingDirection;
-
-    IntSize m_tileSize;
-    bool m_tilesOpaque;
-
-    bool m_tileBordersVisible;
-    bool m_tilePaintCountersVisible;
-    bool m_acceleratedDrawingEnabled;
-    bool m_isSpeculativeTileCreationEnabled;
-
-    bool m_didCallWillStartScrollingOrZooming;
+    IntSize m_tileSize { 512, 512 };
+    
+    TilingMode m_tilingMode { Normal };
+    TilingDirection m_tilingDirection { TilingDirectionDown };
+    
+    bool m_keepsZoomedOutTiles { false };
+    bool m_hasPendingLayoutTiles { false };
+    bool m_hasPendingUpdateTilingMode { false };
+    bool m_tilesOpaque { true };
+    bool m_tileBordersVisible { false };
+    bool m_tilePaintCountersVisible { false };
+    bool m_acceleratedDrawingEnabled { false };
+    bool m_isSpeculativeTileCreationEnabled { true };
+    bool m_tileControllerShouldUseLowScaleTiles { false };
+    bool m_didCallWillStartScrollingOrZooming { false };
+    
     std::unique_ptr<LegacyTileGrid> m_zoomedOutTileGrid;
     std::unique_ptr<LegacyTileGrid> m_zoomedInTileGrid;
 
@@ -206,16 +208,14 @@
 
     Vector<IntRect> m_savedDisplayRects;
 
-    float m_currentScale;
+    float m_currentScale { 1 };
 
-    float m_pendingScale;
-    float m_pendingZoomedOutScale;
+    float m_pendingScale { 0 };
+    float m_pendingZoomedOutScale { 0 };
 
     mutable Lock m_tileMutex;
     mutable Lock m_savedDisplayRectMutex;
     mutable Lock m_contentReplacementImageMutex;
-
-    bool m_tileControllerShouldUseLowScaleTiles;
 };
 
 } // namespace WebCore

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileCache.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileCache.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileCache.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -70,25 +70,9 @@
 
 LegacyTileCache::LegacyTileCache(WAKWindow* window)
     : m_window(window)
-    , m_keepsZoomedOutTiles(false)
-    , m_hasPendingLayoutTiles(false)
-    , m_hasPendingUpdateTilingMode(false)
     , m_tombstone(adoptNS([[LegacyTileCacheTombstone alloc] init]))
-    , m_tilingMode(Normal)
-    , m_tilingDirection(TilingDirectionDown)
-    , m_tileSize(512, 512)
-    , m_tilesOpaque(true)
-    , m_tileBordersVisible(false)
-    , m_tilePaintCountersVisible(false)
-    , m_acceleratedDrawingEnabled(false)
-    , m_isSpeculativeTileCreationEnabled(true)
-    , m_didCallWillStartScrollingOrZooming(false)
     , m_zoomedOutTileGrid(std::make_unique<LegacyTileGrid>(*this, m_tileSize))
     , m_tileCreationTimer(*this, &LegacyTileCache::tileCreationTimerFired)
-    , m_currentScale(1.f)
-    , m_pendingScale(0)
-    , m_pendingZoomedOutScale(0)
-    , m_tileControllerShouldUseLowScaleTiles(false)
 {
     [hostLayer() insertSublayer:m_zoomedOutTileGrid->tileHostLayer() atIndex:0];
     hostLayerSizeChanged();
@@ -520,14 +504,18 @@
     CGContextDrawImage(context, imageRect, image);
 }
 
-void LegacyTileCache::drawWindowContent(LegacyTileLayer* layer, CGContextRef context, CGRect dirtyRect)
+void LegacyTileCache::drawWindowContent(LegacyTileLayer* layer, CGContextRef context, CGRect dirtyRect, DrawingFlags drawingFlags)
 {
     CGRect frame = [layer frame];
     FontAntialiasingStateSaver fontAntialiasingState(context, [m_window useOrientationDependentFontAntialiasing] && [layer isOpaque]);
     fontAntialiasingState.setup([WAKWindow hasLandscapeOrientation]);
 
+    if (drawingFlags == DrawingFlags::Snapshotting)
+        [m_window setIsInSnapshottingPaint:YES];
+        
     CGSRegionObj drawRegion = (CGSRegionObj)[layer regionBeingDrawn];
     CGFloat contentsScale = [layer contentsScale];
+    
     if (drawRegion && shouldRepaintInPieces(dirtyRect, drawRegion, contentsScale)) {
         // Use fine grained repaint rectangles to minimize the amount of painted pixels.
         CGSRegionEnumeratorObj enumerator = CGSRegionEnumerator(drawRegion);
@@ -550,9 +538,12 @@
     }
 
     fontAntialiasingState.restore();
+    
+    if (drawingFlags == DrawingFlags::Snapshotting)
+        [m_window setIsInSnapshottingPaint:NO];
 }
 
-void LegacyTileCache::drawLayer(LegacyTileLayer* layer, CGContextRef context)
+void LegacyTileCache::drawLayer(LegacyTileLayer* layer, CGContextRef context, DrawingFlags drawingFlags)
 {
     // The web lock unlock observer runs after CA commit observer.
     if (!WebThreadIsLockedOrDisabled()) {
@@ -571,7 +562,7 @@
     if (RetainPtr<CGImage> contentReplacementImage = this->contentReplacementImage())
         drawReplacementImage(layer, context, contentReplacementImage.get());
     else
-        drawWindowContent(layer, context, dirtyRect);
+        drawWindowContent(layer, context, dirtyRect, drawingFlags);
 
     ++layer.paintCount;
     if (m_tilePaintCountersVisible) {

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileLayer.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileLayer.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileLayer.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -23,8 +23,7 @@
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef LegacyTileLayer_h
-#define LegacyTileLayer_h
+#pragma once
 
 #if PLATFORM(IOS)
 
@@ -37,10 +36,11 @@
 @interface LegacyTileLayer : CALayer {
     WebCore::LegacyTileGrid* _tileGrid;
     unsigned _paintCount;
+    BOOL _isRenderingInContext;
 }
 @property (nonatomic) unsigned paintCount;
 @property (nonatomic) WebCore::LegacyTileGrid* tileGrid;
-+ (LegacyTileLayer *)layerBeingPainted;
+@property (nonatomic, readonly) BOOL isRenderingInContext;
 @end
 
 @interface LegacyTileHostLayer : CALayer {
@@ -50,4 +50,4 @@
 @end
 
 #endif // PLATFORM(IOS)
-#endif // TileLayer_h
+

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileLayer.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileLayer.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/ios/LegacyTileLayer.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -31,6 +31,7 @@
 #include "LegacyTileCache.h"
 #include "LegacyTileGrid.h"
 #include "WebCoreThread.h"
+#include <wtf/SetForScope.h>
 
 using namespace WebCore;
 
@@ -71,9 +72,8 @@
 @implementation LegacyTileLayer
 @synthesize paintCount = _paintCount;
 @synthesize tileGrid = _tileGrid;
+@synthesize isRenderingInContext = _isRenderingInContext;
 
-static LegacyTileLayer *layerBeingPainted;
-
 - (void)setNeedsDisplayInRect:(CGRect)rect
 {
     // We need to do WebKit layout before painting. Layout may generate new repaint rects and
@@ -93,6 +93,12 @@
         _tileGrid->tileCache().prepareToDraw();
 }
 
+- (void)renderInContext:(CGContextRef)context
+{
+    SetForScope<BOOL> change(_isRenderingInContext, YES);
+    [super renderInContext:context];
+}
+
 - (void)drawInContext:(CGContextRef)context
 {
     // Bugs in clients or other frameworks may cause tile invalidation from within a CA commit.
@@ -105,7 +111,7 @@
         WebThreadLock();
 
     if (_tileGrid)
-        _tileGrid->tileCache().drawLayer(self, context);
+        _tileGrid->tileCache().drawLayer(self, context, self.isRenderingInContext ? LegacyTileCache::DrawingFlags::Snapshotting : LegacyTileCache::DrawingFlags::None);
 }
 
 - (id<CAAction>)actionForKey:(NSString *)key
@@ -115,11 +121,6 @@
     return nil;
 }
 
-+ (LegacyTileLayer *)layerBeingPainted
-{
-    return layerBeingPainted;
-}
-
 @end
 
 #endif // PLATFORM(IOS)

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/ios/wak/WAKView.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/ios/wak/WAKView.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/ios/wak/WAKView.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -505,7 +505,7 @@
     sInterpolationQuality = toCGInterpolationQuality((WebCore::InterpolationQuality)quality);
 }
 
-- (void)_drawRect:(NSRect)dirtyRect context:(CGContextRef)context lockFocus:(bool)lockFocus
+- (void)_drawRect:(NSRect)dirtyRect context:(CGContextRef)context lockFocus:(BOOL)lockFocus
 {
     if (_isHidden)
         return;

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/ios/wak/WAKWindow.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/ios/wak/WAKWindow.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/ios/wak/WAKWindow.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -84,6 +84,7 @@
     WAKView *_nextResponder;
 
     BOOL _visible;
+    BOOL _isInSnapshottingPaint;
     BOOL _useOrientationDependentFontAntialiasing;
     BOOL _entireWindowVisibleForTesting;
 }
@@ -131,6 +132,9 @@
 - (void)sendEventSynchronously:(WebEvent *)anEvent;
 - (void)sendMouseMoveEvent:(WebEvent *)anEvent contentChange:(WKContentChange *)aContentChange;
 
+- (void)setIsInSnapshottingPaint:(BOOL)isInSnapshottingPaint;
+- (BOOL)isInSnapshottingPaint;
+
 // Thread safe way of providing the "usable" rect of the WAKWindow in the viewport/scrollview.
 - (CGRect)exposedScrollViewRect;
 // setExposedScrollViewRect should only ever be called from UIKit.

Modified: branches/safari-604.1.21-branch/Source/WebCore/platform/ios/wak/WAKWindow.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/platform/ios/wak/WAKWindow.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/platform/ios/wak/WAKWindow.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -438,6 +438,16 @@
     _tileCache->setTilesOpaque(opaque);
 }
 
+- (void)setIsInSnapshottingPaint:(BOOL)isInSnapshottingPaint
+{
+    _isInSnapshottingPaint = isInSnapshottingPaint;
+}
+
+- (BOOL)isInSnapshottingPaint
+{
+    return _isInSnapshottingPaint;
+}
+
 - (void)setEntireWindowVisibleForTesting:(BOOL)entireWindowVisible
 {
     _entireWindowVisibleForTesting = entireWindowVisible;

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/PaintPhase.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/PaintPhase.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/PaintPhase.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -58,12 +58,13 @@
     PaintBehaviorSkipSelectionHighlight      = 1 << 1,
     PaintBehaviorForceBlackText              = 1 << 2,
     PaintBehaviorForceWhiteText              = 1 << 3,
-    PaintBehaviorFlattenCompositingLayers    = 1 << 4,
-    PaintBehaviorRenderingSVGMask            = 1 << 5,
-    PaintBehaviorSkipRootBackground          = 1 << 6,
-    PaintBehaviorRootBackgroundOnly          = 1 << 7,
-    PaintBehaviorSelectionAndBackgroundsOnly = 1 << 8,
-    PaintBehaviorExcludeSelection            = 1 << 9,
+    PaintBehaviorRenderingSVGMask            = 1 << 4,
+    PaintBehaviorSkipRootBackground          = 1 << 5,
+    PaintBehaviorRootBackgroundOnly          = 1 << 6,
+    PaintBehaviorSelectionAndBackgroundsOnly = 1 << 7,
+    PaintBehaviorExcludeSelection            = 1 << 8,
+    PaintBehaviorFlattenCompositingLayers    = 1 << 9, // Paint doesn't stop at compositing layer boundaries.
+    PaintBehaviorSnapshotting                = 1 << 10, // Indicates snapshotting, e.g. force synchronous image decoding.
 };
 
 typedef unsigned PaintBehavior;

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderBox.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderBox.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderBox.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -1612,7 +1612,7 @@
     // Figure out if we need to push a transparency layer to render our mask.
     bool pushTransparencyLayer = false;
     bool compositedMask = hasLayer() && layer()->hasCompositedMask();
-    bool flattenCompositingLayers = view().frameView().paintBehavior() & PaintBehaviorFlattenCompositingLayers;
+    bool flattenCompositingLayers = paintInfo.paintBehavior & PaintBehaviorFlattenCompositingLayers;
     CompositeOperator compositeOp = CompositeSourceOver;
 
     bool allMaskImagesLoaded = true;

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderBoxModelObject.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderBoxModelObject.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderBoxModelObject.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -882,7 +882,7 @@
                 downcast<BitmapImage>(*image).updateFromSettings(settings());
 
             auto interpolation = chooseInterpolationQuality(context, *image, &bgLayer, geometry.tileSize());
-            auto decodingMode = (view().frameView().paintBehavior() & PaintBehaviorFlattenCompositingLayers) ? DecodingMode::Synchronous : DecodingMode::Asynchronous;
+            auto decodingMode = (paintInfo.paintBehavior & (PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting)) ? DecodingMode::Synchronous : DecodingMode::Asynchronous;
             context.drawTiledImage(*image, geometry.destRect(), toLayoutPoint(geometry.relativePhase()), geometry.tileSize(), geometry.spaceSize(), ImagePaintingOptions(compositeOp, bgLayer.blendMode(), decodingMode, ImageOrientationDescription(), interpolation));
         }
     }

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderImage.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderImage.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderImage.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -475,7 +475,7 @@
     if (clip)
         context.clip(contentBoxRect);
 
-    paintIntoRect(context, snapRectToDevicePixels(replacedContentRect, deviceScaleFactor));
+    paintIntoRect(paintInfo, snapRectToDevicePixels(replacedContentRect, deviceScaleFactor));
     
     if (cachedImage() && paintInfo.phase == PaintPhaseForeground) {
         // For now, count images as unpainted if they are still progressively loading. We may want 
@@ -559,7 +559,7 @@
     repaint();
 }
 
-void RenderImage::paintIntoRect(GraphicsContext& context, const FloatRect& rect)
+void RenderImage::paintIntoRect(PaintInfo& paintInfo, const FloatRect& rect)
 {
     if (!imageResource().hasImage() || imageResource().errorOccurred() || rect.width() <= 0 || rect.height() <= 0)
         return;
@@ -573,7 +573,7 @@
 
     // FIXME: Document when image != img.get().
     Image* image = imageResource().image().get();
-    InterpolationQuality interpolation = image ? chooseInterpolationQuality(context, *image, image, LayoutSize(rect.size())) : InterpolationDefault;
+    InterpolationQuality interpolation = image ? chooseInterpolationQuality(paintInfo.context(), *image, image, LayoutSize(rect.size())) : InterpolationDefault;
 
 #if USE(CG)
     if (is<PDFDocumentImage>(image))
@@ -584,8 +584,9 @@
         downcast<BitmapImage>(*image).updateFromSettings(settings());
 
     ImageOrientationDescription orientationDescription(shouldRespectImageOrientation(), style().imageOrientation());
-    auto decodingMode = (view().frameView().paintBehavior() & PaintBehaviorFlattenCompositingLayers) ? DecodingMode::Synchronous : DecodingMode::Asynchronous;
-    context.drawImage(*img, rect, ImagePaintingOptions(compositeOperator, BlendModeNormal, decodingMode, orientationDescription, interpolation));
+
+    auto decodingMode = (paintInfo.paintBehavior & (PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting)) ? DecodingMode::Synchronous : DecodingMode::Asynchronous;
+    paintInfo.context().drawImage(*img, rect, ImagePaintingOptions(compositeOperator, BlendModeNormal, decodingMode, orientationDescription, interpolation));
 }
 
 bool RenderImage::boxShadowShouldBeAppliedToBackground(const LayoutPoint& paintOffset, BackgroundBleedAvoidance bleedAvoidance, InlineFlowBox*) const

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderImage.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderImage.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderImage.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -88,7 +88,7 @@
 
     void imageChanged(WrappedImagePtr, const IntRect* = nullptr) override;
 
-    void paintIntoRect(GraphicsContext&, const FloatRect&);
+    void paintIntoRect(PaintInfo&, const FloatRect&);
     void paint(PaintInfo&, const LayoutPoint&) final;
     void layout() override;
 

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayer.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayer.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayer.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -4374,6 +4374,9 @@
         else if (localPaintFlags & PaintLayerPaintingRootBackgroundOnly)
             paintBehavior |= PaintBehaviorRootBackgroundOnly;
 
+        if (paintingInfo.paintBehavior & PaintBehaviorSnapshotting)
+            paintBehavior |= PaintBehaviorSnapshotting;
+
         if (paintingInfo.paintBehavior & PaintBehaviorExcludeSelection)
             paintBehavior |= PaintBehaviorExcludeSelection;
 
@@ -4776,6 +4779,9 @@
     if (localPaintingInfo.paintBehavior & PaintBehaviorExcludeSelection)
         localPaintBehavior |= PaintBehaviorExcludeSelection;
 
+    if (localPaintingInfo.paintBehavior & PaintBehaviorSnapshotting)
+        localPaintBehavior |= PaintBehaviorSnapshotting;
+
     // Optimize clipping for the single fragment case.
     bool shouldClip = localPaintingInfo.clipToDirtyRect && layerFragments.size() == 1 && layerFragments[0].shouldPaintContent && !layerFragments[0].foregroundRect.isEmpty();
     ClipRect clippedRect;

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerBacking.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerBacking.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerBacking.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -2556,7 +2556,7 @@
 }
 
 // Up-call from compositing layer drawing callback.
-void RenderLayerBacking::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase paintingPhase, const FloatRect& clip)
+void RenderLayerBacking::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase paintingPhase, const FloatRect& clip, GraphicsLayerPaintFlags flags)
 {
 #ifndef NDEBUG
     renderer().page().setIsPainting(true);
@@ -2579,8 +2579,12 @@
             dirtyRect.intersect(enclosingIntRect(compositedBoundsIncludingMargin()));
 
         // We have to use the same root as for hit testing, because both methods can compute and cache clipRects.
-        paintIntoLayer(graphicsLayer, context, dirtyRect, PaintBehaviorNormal, paintingPhase);
+        PaintBehavior behavior = PaintBehaviorNormal;
+        if (flags == GraphicsLayerPaintFlags::Snapshotting)
+            behavior |= PaintBehaviorSnapshotting;
 
+        paintIntoLayer(graphicsLayer, context, dirtyRect, behavior, paintingPhase);
+
         InspectorInstrumentation::didPaint(renderer(), dirtyRect);
     } else if (graphicsLayer == layerForHorizontalScrollbar()) {
         paintScrollbar(m_owningLayer.horizontalScrollbar(), context, dirtyRect);

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerBacking.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerBacking.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerBacking.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -197,7 +197,7 @@
     void notifyFlushRequired(const GraphicsLayer*) override;
     void notifyFlushBeforeDisplayRefresh(const GraphicsLayer*) override;
 
-    void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clip) override;
+    void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clip, GraphicsLayerPaintFlags) override;
 
     float deviceScaleFactor() const override;
     float contentsScaleMultiplierForNewTiles(const GraphicsLayer*) const override;

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerCompositor.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerCompositor.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerCompositor.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -2867,7 +2867,7 @@
     context.restore();
 }
 
-void RenderLayerCompositor::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase, const FloatRect& clip)
+void RenderLayerCompositor::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase, const FloatRect& clip, GraphicsLayerPaintFlags)
 {
     IntRect pixelSnappedRectForIntegralPositionedItems = snappedIntRect(LayoutRect(clip));
     if (graphicsLayer == layerForHorizontalScrollbar())

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerCompositor.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerCompositor.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderLayerCompositor.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -332,7 +332,7 @@
 
     // GraphicsLayerClient implementation
     void notifyFlushRequired(const GraphicsLayer*) override;
-    void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect&) override;
+    void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect&, GraphicsLayerPaintFlags) override;
     void customPositionForVisibleRectComputation(const GraphicsLayer*, FloatPoint&) const override;
     bool isTrackingRepaints() const override;
     

Modified: branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderVideo.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderVideo.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebCore/rendering/RenderVideo.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -198,9 +198,9 @@
         context.clip(contentRect);
 
     if (displayingPoster)
-        paintIntoRect(context, rect);
+        paintIntoRect(paintInfo, rect);
     else if (!videoElement().isFullscreen() || !mediaPlayer->supportsAcceleratedRendering()) {
-        if (view().frameView().paintBehavior() & PaintBehaviorFlattenCompositingLayers)
+        if (paintInfo.paintBehavior & PaintBehaviorFlattenCompositingLayers)
             mediaPlayer->paintCurrentFrameInContext(context, rect);
         else
             mediaPlayer->paint(context, rect);

Modified: branches/safari-604.1.21-branch/Source/WebKit/mac/ChangeLog (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit/mac/ChangeLog	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit/mac/ChangeLog	2017-05-26 17:03:38 UTC (rev 217492)
@@ -1,3 +1,34 @@
+2017-05-26  Matthew Hanson  <[email protected]>
+
+        Cherry-pick r217296. rdar://problem/32414363
+
+    2017-05-22  Simon Fraser  <[email protected]>
+
+            Snapshotting via -renderInContext: should do synchronous image decodes
+            https://bugs.webkit.org/show_bug.cgi?id=172485
+            rdar://problem/32276146
+
+            Reviewed by Tim Horton.
+
+            Remove the _setIncludesFlattenedCompositingLayersWhenDrawingToBitmap: SPI that no longer
+            has any callers.
+
+            * Plugins/WebBaseNetscapePluginView.h:
+            * Plugins/WebBaseNetscapePluginView.mm:
+            (-[WebBaseNetscapePluginView inFlatteningPaint]): Deleted.
+            * WebView/WebFrame.mm:
+            (-[WebFrame _paintBehaviorForDestinationContext:]):
+            (-[WebFrame _drawRect:contentsOnly:]):
+            (-[WebFrame _shouldFlattenCompositingLayers:]): Deleted.
+            * WebView/WebHTMLView.mm:
+            (imageFromRect):
+            * WebView/WebView.mm:
+            (-[WebView _commonInitializationWithFrameName:groupName:]):
+            (-[WebView _setIncludesFlattenedCompositingLayersWhenDrawingToBitmap:]): Deleted.
+            (-[WebView _includesFlattenedCompositingLayersWhenDrawingToBitmap]): Deleted.
+            * WebView/WebViewData.h:
+            * WebView/WebViewPrivate.h:
+
 2017-05-17  Jason Marcell  <[email protected]>
 
         Cherry-pick r216901. rdar://problem/31469922

Modified: branches/safari-604.1.21-branch/Source/WebKit/mac/Plugins/WebBaseNetscapePluginView.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit/mac/Plugins/WebBaseNetscapePluginView.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit/mac/Plugins/WebBaseNetscapePluginView.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -113,7 +113,6 @@
 - (void)addWindowObservers;
 - (void)removeWindowObservers;
 - (BOOL)shouldClipOutPlugin;
-- (BOOL)inFlatteningPaint;
 
 - (BOOL)supportsSnapshotting;
 - (void)cacheSnapshot;

Modified: branches/safari-604.1.21-branch/Source/WebKit/mac/Plugins/WebBaseNetscapePluginView.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit/mac/Plugins/WebBaseNetscapePluginView.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit/mac/Plugins/WebBaseNetscapePluginView.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -438,14 +438,6 @@
     return !window || [window isMiniaturized] || [NSApp isHidden] || ![self isDescendantOf:[[self window] contentView]] || [self isHiddenOrHasHiddenAncestor];
 }
 
-- (BOOL)inFlatteningPaint
-{
-    auto* renderer = _element->renderer();
-    if (!is<RenderEmbeddedObject>(renderer))
-        return NO;
-    return !!(downcast<RenderEmbeddedObject>(*renderer).view().frameView().paintBehavior() & PaintBehaviorFlattenCompositingLayers);
-}
-
 - (BOOL)supportsSnapshotting
 {
     return [_pluginPackage.get() supportsSnapshotting];

Modified: branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebFrame.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebFrame.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebFrame.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -120,6 +120,7 @@
 #import <WebCore/RenderLayer.h>
 #import <WebCore/TextResourceDecoder.h>
 #import <WebCore/WAKScrollView.h>
+#import <WebCore/WAKWindow.h>
 #import <WebCore/WKGraphics.h>
 #import <WebCore/WebCoreThreadRun.h>
 #endif
@@ -582,24 +583,31 @@
     return plainText(core(range), TextIteratorDefaultBehavior, true);
 }
 
-- (BOOL)_shouldFlattenCompositingLayers:(CGContextRef)context
+
+- (PaintBehavior)_paintBehaviorForDestinationContext:(CGContextRef)context
 {
 #if !PLATFORM(IOS)
     // -currentContextDrawingToScreen returns YES for bitmap contexts.
     BOOL isPrinting = ![NSGraphicsContext currentContextDrawingToScreen];
     if (isPrinting)
-        return YES;
+        return PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting;
 #endif
 
     if (!WKCGContextIsBitmapContext(context))
-        return NO;
+        return 0;
 
     // If we're drawing into a bitmap, we might be snapshotting, or drawing into a layer-backed view.
     id documentView = [_private->webFrameView documentView];
-    if ([documentView isKindOfClass:[WebHTMLView class]] && [(WebHTMLView *)documentView _web_isDrawingIntoLayer])
-        return NO;
-
-    return [getWebView(self) _includesFlattenedCompositingLayersWhenDrawingToBitmap];
+    if ([documentView isKindOfClass:[WebHTMLView class]]) {
+#if PLATFORM(IOS)
+        if ([[documentView window] isInSnapshottingPaint])
+            return PaintBehaviorSnapshotting;
+#endif
+        if ([(WebHTMLView *)documentView _web_isDrawingIntoLayer])
+            return 0;
+    }
+    
+    return PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting;
 }
 
 - (void)_drawRect:(NSRect)rect contentsOnly:(BOOL)contentsOnly
@@ -622,28 +630,29 @@
 
     FrameView* view = _private->coreFrame->view();
     
-    bool shouldFlatten = false;
+    PaintBehavior oldBehavior = view->paintBehavior();
+    PaintBehavior paintBehavior = oldBehavior;
+    
     if (Frame* parentFrame = _private->coreFrame->tree().parent()) {
         // For subframes, we need to inherit the paint behavior from our parent
-        FrameView* parentView = parentFrame ? parentFrame->view() : 0;
-        if (parentView)
-            shouldFlatten = parentView->paintBehavior() & PaintBehaviorFlattenCompositingLayers;
+        if (FrameView* parentView = parentFrame ? parentFrame->view() : nullptr) {
+            if (parentView->paintBehavior() & PaintBehaviorFlattenCompositingLayers)
+                paintBehavior |= PaintBehaviorFlattenCompositingLayers;
+                
+            if (parentView->paintBehavior() & PaintBehaviorSnapshotting)
+                paintBehavior |= PaintBehaviorSnapshotting;
+        }
     } else
-        shouldFlatten = [self _shouldFlattenCompositingLayers:ctx];
+        paintBehavior |= [self _paintBehaviorForDestinationContext:ctx];
+        
+    view->setPaintBehavior(paintBehavior);
 
-    PaintBehavior oldBehavior = PaintBehaviorNormal;
-    if (shouldFlatten) {
-        oldBehavior = view->paintBehavior();
-        view->setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
-    }
-    
     if (contentsOnly)
         view->paintContents(context, enclosingIntRect(rect));
     else
         view->paint(context, enclosingIntRect(rect));
 
-    if (shouldFlatten)
-        view->setPaintBehavior(oldBehavior);
+    view->setPaintBehavior(oldBehavior);
 }
 
 - (BOOL)_getVisibleRect:(NSRect*)rect

Modified: branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebHTMLView.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebHTMLView.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebHTMLView.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -7297,8 +7297,8 @@
     WebHTMLView *view = (WebHTMLView *)documentView;
     
     PaintBehavior oldPaintBehavior = frame->view()->paintBehavior();
-    frame->view()->setPaintBehavior(oldPaintBehavior | PaintBehaviorFlattenCompositingLayers);
-    
+    frame->view()->setPaintBehavior(oldPaintBehavior | PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting);
+
     BEGIN_BLOCK_OBJC_EXCEPTIONS;
     
     CGRect bounds = [view bounds];

Modified: branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebView.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebView.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebView.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -1369,7 +1369,6 @@
 #else
     _private->backgroundColor = CGColorRetain(cachedCGColor(Color::white));
 #endif
-    _private->includesFlattenedCompositingLayersWhenDrawingToBitmap = YES;
 
 #if PLATFORM(MAC)
     _private->windowVisibilityObserver = adoptNS([[WebWindowVisibilityObserver alloc] initWithView:self]);
@@ -4504,16 +4503,6 @@
     return YES;
 }
 
-- (void)_setIncludesFlattenedCompositingLayersWhenDrawingToBitmap:(BOOL)flag
-{
-    _private->includesFlattenedCompositingLayersWhenDrawingToBitmap = flag;
-}
-
-- (BOOL)_includesFlattenedCompositingLayersWhenDrawingToBitmap
-{
-    return _private->includesFlattenedCompositingLayersWhenDrawingToBitmap;
-}
-
 - (void)setTracksRepaints:(BOOL)flag
 {
     Frame* coreFrame = [self _mainCoreFrame];

Modified: branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebViewData.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebViewData.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebViewData.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -307,8 +307,6 @@
 
     BOOL shouldUpdateWhileOffscreen;
 
-    BOOL includesFlattenedCompositingLayersWhenDrawingToBitmap;
-
     // When this flag is set, next time a WebHTMLView draws, it needs to temporarily disable screen updates
     // so that the NSView drawing is visually synchronized with CALayer updates.
     BOOL needsOneShotDrawingSynchronization;

Modified: branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebViewPrivate.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebViewPrivate.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit/mac/WebView/WebViewPrivate.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -765,10 +765,6 @@
 
 // Returns YES if NSView -displayRectIgnoringOpacity:inContext: will produce a faithful representation of the content.
 - (BOOL)_isSoftwareRenderable;
-// When drawing into a bitmap context, we normally flatten compositing layers (and distort 3D transforms).
-// Clients who are able to capture their own copy of the compositing layers need to be able to disable this.
-- (void)_setIncludesFlattenedCompositingLayersWhenDrawingToBitmap:(BOOL)flag;
-- (BOOL)_includesFlattenedCompositingLayersWhenDrawingToBitmap;
 
 - (void)setTracksRepaints:(BOOL)flag;
 - (BOOL)isTrackingRepaints;

Modified: branches/safari-604.1.21-branch/Source/WebKit/win/FullscreenVideoController.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit/win/FullscreenVideoController.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit/win/FullscreenVideoController.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -187,7 +187,7 @@
 
     virtual void platformCALayerAnimationStarted(CFTimeInterval beginTime) { }
     virtual GraphicsLayer::CompositingCoordinatesOrientation platformCALayerContentsOrientation() const { return GraphicsLayer::CompositingCoordinatesBottomUp; }
-    virtual void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect&) { }
+    virtual void platformCALayerPaintContents(PlatformCALayer*, GraphicsContext&, const FloatRect&, GraphicsLayerPaintFlags) { }
     virtual bool platformCALayerShowDebugBorders() const { return false; }
     virtual bool platformCALayerShowRepaintCounter(PlatformCALayer*) const { return false; }
     virtual int platformCALayerIncrementRepaintCount(PlatformCALayer*) { return 0; }

Modified: branches/safari-604.1.21-branch/Source/WebKit2/ChangeLog (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit2/ChangeLog	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit2/ChangeLog	2017-05-26 17:03:38 UTC (rev 217492)
@@ -1,3 +1,24 @@
+2017-05-26  Matthew Hanson  <[email protected]>
+
+        Cherry-pick r217296. rdar://problem/32414363
+
+    2017-05-22  Simon Fraser  <[email protected]>
+
+            Snapshotting via -renderInContext: should do synchronous image decodes
+            https://bugs.webkit.org/show_bug.cgi?id=172485
+            rdar://problem/32276146
+
+            Reviewed by Tim Horton.
+
+            Pass the PaintBehaviorSnapshotting flag in functions that do snapshotting.
+
+            * Shared/mac/RemoteLayerBackingStore.mm:
+            (WebKit::RemoteLayerBackingStore::drawInContext):
+            * WebProcess/InjectedBundle/DOM/InjectedBundleNodeHandle.cpp:
+            (WebKit::imageForRect):
+            * WebProcess/InjectedBundle/DOM/InjectedBundleRangeHandle.cpp:
+            (WebKit::InjectedBundleRangeHandle::renderedImage):
+
 2017-05-24  Jason Marcell  <[email protected]>
 
         Cherry-pick r217382. rdar://problem/32380083

Modified: branches/safari-604.1.21-branch/Source/WebKit2/Shared/mac/RemoteLayerBackingStore.mm (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit2/Shared/mac/RemoteLayerBackingStore.mm	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit2/Shared/mac/RemoteLayerBackingStore.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -340,17 +340,17 @@
     switch (m_layer->layerType()) {
     case PlatformCALayer::LayerTypeSimpleLayer:
     case PlatformCALayer::LayerTypeTiledBackingTileLayer:
-        m_layer->owner()->platformCALayerPaintContents(m_layer, context, dirtyBounds);
+        m_layer->owner()->platformCALayerPaintContents(m_layer, context, dirtyBounds, GraphicsLayerPaintFlags::None);
         break;
     case PlatformCALayer::LayerTypeWebLayer:
     case PlatformCALayer::LayerTypeBackdropLayer:
-        PlatformCALayer::drawLayerContents(cgContext, m_layer, m_paintingRects);
+        PlatformCALayer::drawLayerContents(cgContext, m_layer, m_paintingRects, GraphicsLayerPaintFlags::None);
         break;
     case PlatformCALayer::LayerTypeDarkSystemBackdropLayer:
     case PlatformCALayer::LayerTypeLightSystemBackdropLayer:
         // FIXME: These have a more complicated layer hierarchy. We need to paint into
         // a child layer in order to see the rendered results.
-        PlatformCALayer::drawLayerContents(cgContext, m_layer, m_paintingRects);
+        PlatformCALayer::drawLayerContents(cgContext, m_layer, m_paintingRects, GraphicsLayerPaintFlags::None);
         break;
     case PlatformCALayer::LayerTypeLayer:
     case PlatformCALayer::LayerTypeTransformLayer:

Modified: branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/InjectedBundle/DOM/InjectedBundleNodeHandle.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/InjectedBundle/DOM/InjectedBundleNodeHandle.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/InjectedBundle/DOM/InjectedBundleNodeHandle.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -151,7 +151,7 @@
     if (options & SnapshotOptionsExcludeSelectionHighlighting)
         shouldPaintSelection = FrameView::ExcludeSelection;
 
-    PaintBehavior paintBehavior = frameView->paintBehavior() | PaintBehaviorFlattenCompositingLayers;
+    PaintBehavior paintBehavior = frameView->paintBehavior() | (PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting);
     if (options & SnapshotOptionsForceBlackText)
         paintBehavior |= PaintBehaviorForceBlackText;
     if (options & SnapshotOptionsForceWhiteText)

Modified: branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/InjectedBundle/DOM/InjectedBundleRangeHandle.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/InjectedBundle/DOM/InjectedBundleRangeHandle.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/InjectedBundle/DOM/InjectedBundleRangeHandle.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -143,7 +143,7 @@
     graphicsContext->translate(-paintRect.x(), -paintRect.y());
 
     PaintBehavior oldPaintBehavior = frameView->paintBehavior();
-    PaintBehavior paintBehavior = oldPaintBehavior | PaintBehaviorSelectionOnly | PaintBehaviorFlattenCompositingLayers;
+    PaintBehavior paintBehavior = oldPaintBehavior | PaintBehaviorSelectionOnly | PaintBehaviorFlattenCompositingLayers | PaintBehaviorSnapshotting;
     if (options & SnapshotOptionsForceBlackText)
         paintBehavior |= PaintBehaviorForceBlackText;
     if (options & SnapshotOptionsForceWhiteText)

Modified: branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.cpp (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.cpp	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.cpp	2017-05-26 17:03:38 UTC (rev 217492)
@@ -268,7 +268,7 @@
         m_client.notifyFlushRequired();
 }
 
-void CompositingCoordinator::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& graphicsContext, GraphicsLayerPaintingPhase, const FloatRect& clipRect)
+void CompositingCoordinator::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& graphicsContext, GraphicsLayerPaintingPhase, const FloatRect& clipRect, GraphicsLayerPaintFlags)
 {
     m_client.paintLayerContents(graphicsLayer, graphicsContext, enclosingIntRect(clipRect));
 }

Modified: branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.h (217491 => 217492)


--- branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.h	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CompositingCoordinator.h	2017-05-26 17:03:38 UTC (rev 217492)
@@ -98,7 +98,7 @@
     // GraphicsLayerClient
     void notifyAnimationStarted(const WebCore::GraphicsLayer*, const String&, double time) override;
     void notifyFlushRequired(const WebCore::GraphicsLayer*) override;
-    void paintContents(const WebCore::GraphicsLayer*, WebCore::GraphicsContext&, WebCore::GraphicsLayerPaintingPhase, const WebCore::FloatRect& clipRect) override;
+    void paintContents(const WebCore::GraphicsLayer*, WebCore::GraphicsContext&, WebCore::GraphicsLayerPaintingPhase, const WebCore::FloatRect& clipRect, WebCore::GraphicsLayerPaintFlags) override;
     float deviceScaleFactor() const override;
     float pageScaleFactor() const override;
 

Modified: branches/safari-604.1.21-branch/Tools/ChangeLog (217491 => 217492)


--- branches/safari-604.1.21-branch/Tools/ChangeLog	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Tools/ChangeLog	2017-05-26 17:03:38 UTC (rev 217492)
@@ -1,3 +1,24 @@
+2017-05-26  Matthew Hanson  <[email protected]>
+
+        Cherry-pick r217296. rdar://problem/32414363
+
+    2017-05-22  Simon Fraser  <[email protected]>
+
+            Snapshotting via -renderInContext: should do synchronous image decodes
+            https://bugs.webkit.org/show_bug.cgi?id=172485
+            rdar://problem/32276146
+
+            Reviewed by Tim Horton.
+
+            New API test.
+
+            * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+            * TestWebKitAPI/Tests/WebKit/ios/SnapshotViaRenderInContext.mm: Added.
+            (-[RenderInContextWebViewDelegate webViewDidFinishLoad:]):
+            (-[RenderInContextWebViewDelegate webView:didFailLoadWithError:]):
+            (TestWebKitAPI::getPixelIndex):
+            (TestWebKitAPI::TEST):
+
 2017-05-24  Ryan Haddad  <[email protected]>
 
         Merge r217165.

Modified: branches/safari-604.1.21-branch/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (217491 => 217492)


--- branches/safari-604.1.21-branch/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2017-05-26 16:13:19 UTC (rev 217491)
+++ branches/safari-604.1.21-branch/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2017-05-26 17:03:38 UTC (rev 217492)
@@ -33,6 +33,7 @@
 		0F139E791A42457000F590F5 /* PlatformUtilitiesCocoa.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0F139E721A423A2B00F590F5 /* PlatformUtilitiesCocoa.mm */; };
 		0F2C20B81DCD545000542D9E /* Time.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0F2C20B71DCD544800542D9E /* Time.cpp */; };
 		0F3B94A71A77267400DE3272 /* WKWebViewEvaluateJavaScript.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0F3B94A51A77266C00DE3272 /* WKWebViewEvaluateJavaScript.mm */; };
+		0F4FFA9E1ED3AA8500F7111F /* SnapshotViaRenderInContext.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0F4FFA9D1ED3AA8500F7111F /* SnapshotViaRenderInContext.mm */; };
 		1A02C870125D4CFD00E3F4BD /* find.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1A02C84B125D4A5E00E3F4BD /* find.html */; };
 		1A3524AE1D63A4FB0031729B /* Scope.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A3524AC1D63A4FB0031729B /* Scope.cpp */; };
 		1A4F81CF1BDFFD53004E672E /* RemoteObjectRegistryPlugIn.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1A4F81CD1BDFFD53004E672E /* RemoteObjectRegistryPlugIn.mm */; };
@@ -894,6 +895,8 @@
 		0F17BBD415AF6C4D007AB753 /* WebCoreStatisticsWithNoWebProcess.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebCoreStatisticsWithNoWebProcess.cpp; sourceTree = "<group>"; };
 		0F2C20B71DCD544800542D9E /* Time.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Time.cpp; sourceTree = "<group>"; };
 		0F3B94A51A77266C00DE3272 /* WKWebViewEvaluateJavaScript.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKWebViewEvaluateJavaScript.mm; sourceTree = "<group>"; };
+		0F4FFA9D1ED3AA8500F7111F /* SnapshotViaRenderInContext.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SnapshotViaRenderInContext.mm; sourceTree = "<group>"; };
+		0F4FFAA01ED3D0DE00F7111F /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; };
 		0FC6C4CB141027E0005B7F0C /* RedBlackTree.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RedBlackTree.cpp; sourceTree = "<group>"; };
 		0FC6C4CE141034AD005B7F0C /* MetaAllocator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MetaAllocator.cpp; sourceTree = "<group>"; };
 		0FE447971B76F1E3009498EB /* ParkingLot.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParkingLot.cpp; sourceTree = "<group>"; };
@@ -1858,6 +1861,7 @@
 		7C83E0281D0A5CDF00FEBCF3 /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
+				0F4FFAA01ED3D0DE00F7111F /* ImageIO.framework */,
 				634910DF1E9D3FF300880309 /* CoreLocation.framework */,
 				7A010BCA1D877C0500EDE72A /* CoreGraphics.framework */,
 				7C83E0331D0A5F2700FEBCF3 /* libicucore.dylib */,
@@ -2506,6 +2510,7 @@
 			children = (
 				CDC8E49A1BC728FE00594FEC /* Resources */,
 				CDC8E4851BC5B19400594FEC /* AudioSessionCategoryIOS.mm */,
+				0F4FFA9D1ED3AA8500F7111F /* SnapshotViaRenderInContext.mm */,
 			);
 			path = ios;
 			sourceTree = "<group>";
@@ -3029,6 +3034,7 @@
 				2EFF06D41D8AEDBB0004BB30 /* TestWKWebView.mm in Sources */,
 				CE3524F91B1441C40028A7C5 /* TextFieldDidBeginAndEndEditing.cpp in Sources */,
 				7CCE7EDD1A411A9200447C4C /* TimeRanges.cpp in Sources */,
+				0F4FFA9E1ED3AA8500F7111F /* SnapshotViaRenderInContext.mm in Sources */,
 				7CCE7ED31A411A7E00447C4C /* TypingStyleCrash.mm in Sources */,
 				7CCE7EDE1A411A9200447C4C /* URL.cpp in Sources */,
 				7CCE7EB01A411A4400447C4C /* URLExtras.mm in Sources */,

Added: branches/safari-604.1.21-branch/Tools/TestWebKitAPI/Tests/WebKit/ios/SnapshotViaRenderInContext.mm (0 => 217492)


--- branches/safari-604.1.21-branch/Tools/TestWebKitAPI/Tests/WebKit/ios/SnapshotViaRenderInContext.mm	                        (rev 0)
+++ branches/safari-604.1.21-branch/Tools/TestWebKitAPI/Tests/WebKit/ios/SnapshotViaRenderInContext.mm	2017-05-26 17:03:38 UTC (rev 217492)
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#import "config.h"
+
+#if PLATFORM(IOS)
+
+#import "PlatformUtilities.h"
+#import <UIKit/UIKit.h>
+#import <stdlib.h>
+#import <wtf/RetainPtr.h>
+
+static bool loadComplete = false;
+static bool loadFailed = false;
+
+@interface RenderInContextWebViewDelegate : NSObject <UIWebViewDelegate>
+@end
+
+@implementation RenderInContextWebViewDelegate
+
+- (void)webViewDidFinishLoad:(UIWebView *)webView
+{
+    loadComplete = true;
+}
+
+- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
+{
+    loadComplete = true;
+    loadFailed = true;
+}
+
+@end
+
+namespace TestWebKitAPI {
+
+static NSInteger getPixelIndex(NSInteger x, NSInteger y, NSInteger width)
+{
+    return (y * width + x) * 4;
+}
+
+TEST(WebKit1, RenderInContextSnapshot)
+{
+    const NSInteger width = 800;
+    const NSInteger height = 600;
+    
+    RetainPtr<UIWindow> uiWindow = adoptNS([[UIWindow alloc] initWithFrame:NSMakeRect(0, 0, width, height)]);
+    RetainPtr<UIWebView> uiWebView = adoptNS([[UIWebView alloc] initWithFrame:NSMakeRect(0, 0, width, height)]);
+    [uiWindow addSubview:uiWebView.get()];
+    
+    RetainPtr<RenderInContextWebViewDelegate> uiDelegate = adoptNS([[RenderInContextWebViewDelegate alloc] init]);
+    uiWebView.get().delegate = uiDelegate.get();
+    
+    NSURL *url = "" mainBundle] URLForResource:@"large-red-square-image" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"];
+    NSLog(@"Loading %@", url);
+    [uiWebView loadRequest:[NSURLRequest requestWithURL:url]];
+    
+    Util::run(&loadComplete);
+    
+    EXPECT_TRUE(loadComplete);
+    EXPECT_FALSE(loadFailed);
+    
+    unsigned char* pixelBuffer = static_cast<unsigned char*>(calloc(width * height, 4));
+    
+    RetainPtr<CGColorSpaceRef> colorSpace = adoptCF(CGColorSpaceCreateDeviceRGB());
+    RetainPtr<CGContextRef> context = CGBitmapContextCreate(pixelBuffer, width, height, 8, 4 * width, colorSpace.get(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
+    
+    // Flip the context
+    CGContextScaleCTM(context.get(), 1, -1);
+    CGContextTranslateCTM(context.get(), 0, -height);
+    
+    [uiWebView setFrame:CGRectMake(0, 0, 801, 601)]; // This resize stops Core Animation from using cached layer backing store
+    [[uiWebView layer] renderInContext: context.get()];
+    
+    // There should be a red square at 0,0 - 100x100
+    NSInteger pixelIndex = getPixelIndex(1, 1, width);
+    unsigned char value = pixelBuffer[pixelIndex];
+    EXPECT_EQ(255, value);
+    EXPECT_EQ(0, pixelBuffer[pixelIndex + 1]);
+    EXPECT_EQ(0, pixelBuffer[pixelIndex + 2]);
+    
+    // Right-bottom corner of the div (0, 0, 100, 100)
+    pixelIndex = getPixelIndex(98, 98, width);
+    EXPECT_EQ(255, pixelBuffer[pixelIndex]);
+    EXPECT_EQ(0, pixelBuffer[pixelIndex + 1]);
+    EXPECT_EQ(0, pixelBuffer[pixelIndex + 2]);
+    
+    // Outside the div (0, 0, 100, 100)
+    pixelIndex = getPixelIndex(101, 101, width);
+    EXPECT_EQ(255, pixelBuffer[pixelIndex]);
+    EXPECT_EQ(255, pixelBuffer[pixelIndex + 1]);
+    EXPECT_EQ(255, pixelBuffer[pixelIndex + 2]);
+    
+    free(pixelBuffer);
+}
+
+}
+
+#endif
+
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to