<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[168812] branches/safari-538.34-branch/Source/WebKit2</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/168812">168812</a></dd>
<dt>Author</dt> <dd>lforschler@apple.com</dd>
<dt>Date</dt> <dd>2014-05-14 01:13:55 -0700 (Wed, 14 May 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Merged <a href="http://trac.webkit.org/projects/webkit/changeset/168689">r168689</a>.  </pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branchessafari53834branchSourceWebKit2ChangeLog">branches/safari-538.34-branch/Source/WebKit2/ChangeLog</a></li>
<li><a href="#branchessafari53834branchSourceWebKit2UIProcessAPICocoaWKWebViewmm">branches/safari-538.34-branch/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm</a></li>
<li><a href="#branchessafari53834branchSourceWebKit2UIProcessAPImacWKViewmm">branches/safari-538.34-branch/Source/WebKit2/UIProcess/API/mac/WKView.mm</a></li>
<li><a href="#branchessafari53834branchSourceWebKit2UIProcessiosViewGestureControllerIOSmm">branches/safari-538.34-branch/Source/WebKit2/UIProcess/ios/ViewGestureControllerIOS.mm</a></li>
<li><a href="#branchessafari53834branchSourceWebKit2UIProcessmacViewGestureControllerh">branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewGestureController.h</a></li>
<li><a href="#branchessafari53834branchSourceWebKit2UIProcessmacViewGestureControllerMacmm">branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewGestureControllerMac.mm</a></li>
<li><a href="#branchessafari53834branchSourceWebKit2UIProcessmacViewSnapshotStoreh">branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.h</a></li>
<li><a href="#branchessafari53834branchSourceWebKit2UIProcessmacViewSnapshotStoremm">branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branchessafari53834branchSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: branches/safari-538.34-branch/Source/WebKit2/ChangeLog (168811 => 168812)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-538.34-branch/Source/WebKit2/ChangeLog        2014-05-14 08:11:06 UTC (rev 168811)
+++ branches/safari-538.34-branch/Source/WebKit2/ChangeLog        2014-05-14 08:13:55 UTC (rev 168812)
</span><span class="lines">@@ -1,5 +1,60 @@
</span><span class="cx"> 2014-05-14  Lucas Forschler  &lt;lforschler@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Merge r168689
+
+    2014-05-13  Tim Horton  &lt;timothy_horton@apple.com&gt;
+
+            WebKit2 View Gestures (Swipe): Encode snapshots as JPEG on some platforms
+            https://bugs.webkit.org/show_bug.cgi?id=127788
+            &lt;rdar://problem/15928241&gt;
+
+            Reviewed by Anders Carlsson.
+
+            Allow JPEG snapshots. Compression is done asynchronously.
+            Enable JPEG snapshots (for now) on all PLATFORM(MAC).
+            We'll switch back to IOSurface snapshots on 10.9+ after &lt;rdar://problem/16734031&gt; is resolved.
+
+            * UIProcess/API/Cocoa/WKWebView.mm:
+            * UIProcess/API/mac/WKView.mm:
+            (-[WKView _takeViewSnapshot]):
+            Store the image size on the ViewSnapshot.
+            Move IOSurface-related code to ViewSnapshotStore.
+
+            * UIProcess/ios/ViewGestureControllerIOS.mm:
+            (WebKit::ViewGestureController::beginSwipeGesture):
+            Adopt asLayerContents().
+
+            * UIProcess/mac/ViewGestureController.h:
+            * UIProcess/mac/ViewGestureControllerMac.mm:
+            (WebKit::ViewGestureController::retrieveSnapshotForItem):
+            (WebKit::ViewGestureController::beginSwipeGesture):
+            (WebKit::ViewGestureController::removeSwipeSnapshot):
+            Adopt asLayerContents().
+            Put the snapshot on the right layer (the parent of the snapshot image layer);
+            if it's on the snapshot image layer itself, if there is no image, the shadow
+            is displayed on top of the white placeholder.
+            Only reset snapshot purgeability if we're using IOSurface snapshots.
+
+            * UIProcess/mac/ViewSnapshotStore.h:
+            Add a bunch of macros to allow us to switch the snapshot backing store implementation.
+
+            * UIProcess/mac/ViewSnapshotStore.mm:
+            (WebKit::ViewSnapshotStore::ViewSnapshotStore):
+            (WebKit::ViewSnapshotStore::~ViewSnapshotStore):
+            (WebKit::ViewSnapshotStore::recordSnapshot):
+            If takeViewSnapshot() fails, don't remove the existing snapshot.
+
+            (WebKit::createIOSurfaceFromImage):
+            (WebKit::compressImageAsJPEG):
+            (WebKit::ViewSnapshotStore::reduceSnapshotMemoryCost):
+            (WebKit::ViewSnapshotStore::didCompressSnapshot):
+            (WebKit::ViewSnapshot::clearImage):
+            (WebKit::ViewSnapshot::asLayerContents):
+            Asynchronously compress snapshots - if we're using JPEG snapshots - for a
+            very large memory win (~20-30x).
+
+2014-05-14  Lucas Forschler  &lt;lforschler@apple.com&gt;
+
</ins><span class="cx">         Merge r168661
</span><span class="cx"> 
</span><span class="cx">     2014-05-12  Simon Fraser  &lt;simon.fraser@apple.com&gt;
</span></span></pre></div>
<a id="branchessafari53834branchSourceWebKit2UIProcessAPICocoaWKWebViewmm"></a>
<div class="modfile"><h4>Modified: branches/safari-538.34-branch/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm (168811 => 168812)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-538.34-branch/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm        2014-05-14 08:11:06 UTC (rev 168811)
+++ branches/safari-538.34-branch/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm        2014-05-14 08:13:55 UTC (rev 168812)
</span><span class="lines">@@ -505,6 +505,7 @@
</span><span class="cx">     CATransform3D transform = CATransform3DMakeScale(deviceScale, deviceScale, 1);
</span><span class="cx">     CARenderServerCaptureLayerWithTransform(MACH_PORT_NULL, self.layer.context.contextId, (uint64_t)self.layer, snapshot.slotID, 0, 0, &amp;transform);
</span><span class="cx"> 
</span><ins>+    snapshot.size = WebCore::expandedIntSize(WebCore::FloatSize(snapshotSize));
</ins><span class="cx">     snapshot.imageSizeInBytes = snapshotSize.width * snapshotSize.height * 4;
</span><span class="cx"> 
</span><span class="cx">     return snapshot;
</span></span></pre></div>
<a id="branchessafari53834branchSourceWebKit2UIProcessAPImacWKViewmm"></a>
<div class="modfile"><h4>Modified: branches/safari-538.34-branch/Source/WebKit2/UIProcess/API/mac/WKView.mm (168811 => 168812)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-538.34-branch/Source/WebKit2/UIProcess/API/mac/WKView.mm        2014-05-14 08:11:06 UTC (rev 168811)
+++ branches/safari-538.34-branch/Source/WebKit2/UIProcess/API/mac/WKView.mm        2014-05-14 08:13:55 UTC (rev 168812)
</span><span class="lines">@@ -3040,19 +3040,6 @@
</span><span class="cx">     return _data-&gt;_rootLayer.get();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static RefPtr&lt;IOSurface&gt; createIOSurfaceFromImage(CGImageRef image)
-{
-    size_t width = CGImageGetWidth(image);
-    size_t height = CGImageGetHeight(image);
-
-    RefPtr&lt;IOSurface&gt; surface = IOSurface::create(IntSize(width, height), ColorSpaceDeviceRGB);
-    RetainPtr&lt;CGContextRef&gt; surfaceContext = surface-&gt;ensurePlatformContext();
-    CGContextDrawImage(surfaceContext.get(), CGRectMake(0, 0, width, height), image);
-    CGContextFlush(surfaceContext.get());
-
-    return surface;
-}
-
</del><span class="cx"> - (ViewSnapshot)_takeViewSnapshot
</span><span class="cx"> {
</span><span class="cx">     NSWindow *window = self.window;
</span><span class="lines">@@ -3091,8 +3078,12 @@
</span><span class="cx"> 
</span><span class="cx">     auto croppedSnapshotImage = adoptCF(CGImageCreateWithImageInRect(windowSnapshotImage.get(), NSRectToCGRect([window convertRectToBacking:croppedImageRect])));
</span><span class="cx"> 
</span><del>-    snapshot.surface = createIOSurfaceFromImage(croppedSnapshotImage.get());
-    snapshot.imageSizeInBytes = snapshot.surface-&gt;totalBytes();
</del><ins>+    snapshot.image = croppedSnapshotImage.get();
+
+    IntSize imageSize(CGImageGetWidth(croppedSnapshotImage.get()), CGImageGetHeight(croppedSnapshotImage.get()));
+    snapshot.size = imageSize;
+    snapshot.imageSizeInBytes = imageSize.width() * imageSize.height() * 4;
+
</ins><span class="cx">     return snapshot;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="branchessafari53834branchSourceWebKit2UIProcessiosViewGestureControllerIOSmm"></a>
<div class="modfile"><h4>Modified: branches/safari-538.34-branch/Source/WebKit2/UIProcess/ios/ViewGestureControllerIOS.mm (168811 => 168812)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-538.34-branch/Source/WebKit2/UIProcess/ios/ViewGestureControllerIOS.mm        2014-05-14 08:11:06 UTC (rev 168811)
+++ branches/safari-538.34-branch/Source/WebKit2/UIProcess/ios/ViewGestureControllerIOS.mm        2014-05-14 08:13:55 UTC (rev 168812)
</span><span class="lines">@@ -158,7 +158,7 @@
</span><span class="cx"> 
</span><span class="cx">     ViewSnapshot snapshot;
</span><span class="cx">     if (ViewSnapshotStore::shared().getSnapshot(targetItem, snapshot) &amp;&amp; snapshot.hasImage())
</span><del>-        [m_snapshotView layer].contents = [CAContext objectForSlot:snapshot.slotID];
</del><ins>+        [m_snapshotView layer].contents = snapshot.asLayerContents();
</ins><span class="cx"> 
</span><span class="cx">     [m_snapshotView setBackgroundColor:[UIColor whiteColor]];
</span><span class="cx">     [m_snapshotView layer].contentsGravity = kCAGravityTopLeft;
</span></span></pre></div>
<a id="branchessafari53834branchSourceWebKit2UIProcessmacViewGestureControllerh"></a>
<div class="modfile"><h4>Modified: branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewGestureController.h (168811 => 168812)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewGestureController.h        2014-05-14 08:11:06 UTC (rev 168811)
+++ branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewGestureController.h        2014-05-14 08:13:55 UTC (rev 168812)
</span><span class="lines">@@ -50,6 +50,7 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><ins>+struct ViewSnapshot;
</ins><span class="cx"> class WebBackForwardListItem;
</span><span class="cx"> class WebPageProxy;
</span><span class="cx"> 
</span><span class="lines">@@ -131,7 +132,7 @@
</span><span class="cx">     void endSwipeGesture(WebBackForwardListItem* targetItem, bool cancelled);
</span><span class="cx">     bool deltaIsSufficientToBeginSwipe(NSEvent *);
</span><span class="cx">     bool scrollEventCanBecomeSwipe(NSEvent *, SwipeDirection&amp;);
</span><del>-    WebCore::IOSurface* retrieveSnapshotForItem(WebBackForwardListItem*, WebCore::FloatSize swipeLayerSize, float topContentInset);
</del><ins>+    bool retrieveSnapshotForItem(WebBackForwardListItem* targetItem, WebCore::FloatSize swipeLayerSize, float topContentInset, ViewSnapshot&amp;);
</ins><span class="cx"> 
</span><span class="cx">     CALayer *determineSnapshotLayerParent() const;
</span><span class="cx">     CALayer *determineLayerAdjacentToSnapshotForParent(SwipeDirection, CALayer *snapshotLayerParent) const;
</span></span></pre></div>
<a id="branchessafari53834branchSourceWebKit2UIProcessmacViewGestureControllerMacmm"></a>
<div class="modfile"><h4>Modified: branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewGestureControllerMac.mm (168811 => 168812)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewGestureControllerMac.mm        2014-05-14 08:11:06 UTC (rev 168811)
+++ branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewGestureControllerMac.mm        2014-05-14 08:13:55 UTC (rev 168812)
</span><span class="lines">@@ -451,28 +451,21 @@
</span><span class="cx">     return layerAdjacentToSnapshot;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-IOSurface* ViewGestureController::retrieveSnapshotForItem(WebBackForwardListItem* targetItem, FloatSize swipeLayerSize, float topContentInset)
</del><ins>+bool ViewGestureController::retrieveSnapshotForItem(WebBackForwardListItem* targetItem, FloatSize swipeLayerSize, float topContentInset, ViewSnapshot&amp; snapshot)
</ins><span class="cx"> {
</span><del>-    ViewSnapshot snapshot;
</del><span class="cx">     if (!ViewSnapshotStore::shared().getSnapshot(targetItem, snapshot))
</span><del>-        return nullptr;
</del><ins>+        return false;
</ins><span class="cx"> 
</span><del>-    if (!snapshot.surface)
-        return nullptr;
-
</del><span class="cx">     float deviceScaleFactor = m_webPageProxy.deviceScaleFactor();
</span><span class="cx">     if (snapshot.deviceScaleFactor != deviceScaleFactor)
</span><del>-        return nullptr;
</del><ins>+        return false;
</ins><span class="cx"> 
</span><span class="cx">     FloatSize unobscuredSwipeLayerSizeInDeviceCoordinates = swipeLayerSize - FloatSize(0, topContentInset);
</span><span class="cx">     unobscuredSwipeLayerSizeInDeviceCoordinates.scale(deviceScaleFactor);
</span><del>-    if (snapshot.surface-&gt;size() != unobscuredSwipeLayerSizeInDeviceCoordinates)
-        return nullptr;
</del><ins>+    if (snapshot.size != unobscuredSwipeLayerSizeInDeviceCoordinates)
+        return false;
</ins><span class="cx"> 
</span><del>-    if (snapshot.surface-&gt;setIsVolatile(false) != IOSurface::SurfaceState::Valid)
-        return nullptr;
-
-    return snapshot.surface.get();
</del><ins>+    return true;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static bool layerGeometryFlippedToRoot(CALayer *layer)
</span><span class="lines">@@ -535,10 +528,12 @@
</span><span class="cx">     CALayer *snapshotLayerParent = determineSnapshotLayerParent();
</span><span class="cx">     bool geometryIsFlippedToRoot = layerGeometryFlippedToRoot(snapshotLayerParent);
</span><span class="cx"> 
</span><del>-    IOSurface* snapshot = retrieveSnapshotForItem(targetItem, swipeArea.size(), topContentInset);
-    if (snapshot) {
-        m_currentSwipeSnapshotSurface = snapshot;
-        [m_swipeSnapshotLayer setContents:(id)snapshot-&gt;surface()];
</del><ins>+    ViewSnapshot snapshot;
+    if (retrieveSnapshotForItem(targetItem, swipeArea.size(), topContentInset, snapshot)) {
+        [m_swipeSnapshotLayer setContents:snapshot.asLayerContents()];
+#if USE_IOSURFACE_VIEW_SNAPSHOTS
+        m_currentSwipeSnapshotSurface = snapshot.surface;
+#endif
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     [m_swipeLayer setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)];
</span><span class="lines">@@ -571,11 +566,11 @@
</span><span class="cx">             [rootContentLayer setShadowRadius:swipeOverlayShadowRadius];
</span><span class="cx">             [rootContentLayer setShadowPath:shadowPath.get()];
</span><span class="cx">         } else {
</span><del>-            RetainPtr&lt;CGPathRef&gt; shadowPath = adoptCF(CGPathCreateWithRect([m_swipeSnapshotLayer bounds], 0));
-            [m_swipeSnapshotLayer setShadowColor:CGColorGetConstantColor(kCGColorBlack)];
-            [m_swipeSnapshotLayer setShadowOpacity:swipeOverlayShadowOpacity];
-            [m_swipeSnapshotLayer setShadowRadius:swipeOverlayShadowRadius];
-            [m_swipeSnapshotLayer setShadowPath:shadowPath.get()];
</del><ins>+            RetainPtr&lt;CGPathRef&gt; shadowPath = adoptCF(CGPathCreateWithRect([m_swipeLayer bounds], 0));
+            [m_swipeLayer setShadowColor:CGColorGetConstantColor(kCGColorBlack)];
+            [m_swipeLayer setShadowOpacity:swipeOverlayShadowOpacity];
+            [m_swipeLayer setShadowRadius:swipeOverlayShadowRadius];
+            [m_swipeLayer setShadowPath:shadowPath.get()];
</ins><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -671,9 +666,11 @@
</span><span class="cx">     if (m_activeGestureType != ViewGestureType::Swipe)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><ins>+#if USE_IOSURFACE_VIEW_SNAPSHOTS
</ins><span class="cx">     if (m_currentSwipeSnapshotSurface)
</span><span class="cx">         m_currentSwipeSnapshotSurface-&gt;setIsVolatile(true);
</span><span class="cx">     m_currentSwipeSnapshotSurface = nullptr;
</span><ins>+#endif
</ins><span class="cx"> 
</span><span class="cx">     for (const auto&amp; layer : m_currentSwipeLiveLayers)
</span><span class="cx">         [layer setTransform:CATransform3DIdentity];
</span></span></pre></div>
<a id="branchessafari53834branchSourceWebKit2UIProcessmacViewSnapshotStoreh"></a>
<div class="modfile"><h4>Modified: branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.h (168811 => 168812)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.h        2014-05-14 08:11:06 UTC (rev 168811)
+++ branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.h        2014-05-14 08:13:55 UTC (rev 168812)
</span><span class="lines">@@ -35,26 +35,40 @@
</span><span class="cx"> 
</span><span class="cx"> OBJC_CLASS CAContext;
</span><span class="cx"> 
</span><ins>+// FIXME: Enable IOSurface snapshots for 10.9+ when &lt;rdar://problem/16734031&gt; is resolved.
+#if PLATFORM(MAC)
+#define USE_JPEG_VIEW_SNAPSHOTS true
+#define USE_IOSURFACE_VIEW_SNAPSHOTS false
+#define USE_RENDER_SERVER_VIEW_SNAPSHOTS false
+#else
+#define USE_JPEG_VIEW_SNAPSHOTS false
+#define USE_IOSURFACE_VIEW_SNAPSHOTS false
+#define USE_RENDER_SERVER_VIEW_SNAPSHOTS true
+#endif
+
</ins><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><span class="cx"> class WebBackForwardListItem;
</span><span class="cx"> class WebPageProxy;
</span><span class="cx"> 
</span><span class="cx"> struct ViewSnapshot {
</span><del>-#if PLATFORM(MAC)
</del><ins>+#if USE_JPEG_VIEW_SNAPSHOTS || USE_IOSURFACE_VIEW_SNAPSHOTS
</ins><span class="cx">     RefPtr&lt;WebCore::IOSurface&gt; surface;
</span><ins>+    RetainPtr&lt;CGImageRef&gt; image;
</ins><span class="cx"> #endif
</span><del>-#if PLATFORM(IOS)
</del><ins>+#if USE_RENDER_SERVER_VIEW_SNAPSHOTS
</ins><span class="cx">     uint32_t slotID = 0;
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     std::chrono::steady_clock::time_point creationTime;
</span><span class="cx">     uint64_t renderTreeSize;
</span><span class="cx">     float deviceScaleFactor;
</span><ins>+    WebCore::IntSize size;
</ins><span class="cx">     size_t imageSizeInBytes = 0;
</span><span class="cx"> 
</span><span class="cx">     void clearImage();
</span><span class="cx">     bool hasImage() const;
</span><ins>+    id asLayerContents();
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> class ViewSnapshotStore {
</span><span class="lines">@@ -80,7 +94,13 @@
</span><span class="cx"> private:
</span><span class="cx">     void pruneSnapshots(WebPageProxy&amp;);
</span><span class="cx">     void removeSnapshotImage(ViewSnapshot&amp;);
</span><ins>+    void reduceSnapshotMemoryCost(const String&amp; uuid);
</ins><span class="cx"> 
</span><ins>+#if USE_JPEG_VIEW_SNAPSHOTS
+    void didCompressSnapshot(const String&amp; uuid, RetainPtr&lt;CGImageRef&gt; newImage, size_t newImageSize);
+    dispatch_queue_t m_compressionQueue;
+#endif
+
</ins><span class="cx">     HashMap&lt;String, ViewSnapshot&gt; m_snapshotMap;
</span><span class="cx"> 
</span><span class="cx">     bool m_enabled;
</span></span></pre></div>
<a id="branchessafari53834branchSourceWebKit2UIProcessmacViewSnapshotStoremm"></a>
<div class="modfile"><h4>Modified: branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.mm (168811 => 168812)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.mm        2014-05-14 08:11:06 UTC (rev 168811)
+++ branches/safari-538.34-branch/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.mm        2014-05-14 08:13:55 UTC (rev 168812)
</span><span class="lines">@@ -38,10 +38,10 @@
</span><span class="cx"> 
</span><span class="cx"> using namespace WebCore;
</span><span class="cx"> 
</span><del>-#if PLATFORM(MAC)
</del><ins>+#if USE_IOSURFACE_VIEW_SNAPSHOTS
</ins><span class="cx"> static const size_t maximumSnapshotCacheSize = 400 * (1024 * 1024);
</span><del>-#elif PLATFORM(IOS)
-// Because snapshots are not purgeable, we should keep fewer around.
</del><ins>+#elif USE_JPEG_VIEW_SNAPSHOTS || USE_RENDER_SERVER_VIEW_SNAPSHOTS
+// Because non-IOSurface snapshots are not purgeable, we should keep fewer around.
</ins><span class="cx"> static const size_t maximumSnapshotCacheSize = 50 * (1024 * 1024);
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="lines">@@ -51,10 +51,16 @@
</span><span class="cx">     : m_enabled(true)
</span><span class="cx">     , m_snapshotCacheSize(0)
</span><span class="cx"> {
</span><ins>+#if USE_JPEG_VIEW_SNAPSHOTS
+    m_compressionQueue = dispatch_queue_create(&quot;com.apple.WebKit.ViewSnapshotStore.CompressionQueue&quot;, nullptr);
+#endif
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> ViewSnapshotStore::~ViewSnapshotStore()
</span><span class="cx"> {
</span><ins>+#if USE_JPEG_VIEW_SNAPSHOTS
+    dispatch_release(m_compressionQueue);
+#endif
</ins><span class="cx">     discardSnapshots();
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -158,6 +164,10 @@
</span><span class="cx"> 
</span><span class="cx">     pruneSnapshots(webPageProxy);
</span><span class="cx"> 
</span><ins>+    ViewSnapshot snapshot = webPageProxy.takeViewSnapshot();
+    if (!snapshot.hasImage())
+        return;
+
</ins><span class="cx">     String oldSnapshotUUID = item-&gt;snapshotUUID();
</span><span class="cx">     if (!oldSnapshotUUID.isEmpty()) {
</span><span class="cx">         const auto&amp; oldSnapshotIter = m_snapshotMap.find(oldSnapshotUUID);
</span><span class="lines">@@ -167,15 +177,16 @@
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    item-&gt;setSnapshotUUID(createCanonicalUUIDString());
-
-    ViewSnapshot snapshot = webPageProxy.takeViewSnapshot();
</del><span class="cx">     snapshot.creationTime = std::chrono::steady_clock::now();
</span><span class="cx">     snapshot.renderTreeSize = webPageProxy.renderTreeSize();
</span><span class="cx">     snapshot.deviceScaleFactor = webPageProxy.deviceScaleFactor();
</span><span class="cx"> 
</span><ins>+    item-&gt;setSnapshotUUID(createCanonicalUUIDString());
+
</ins><span class="cx">     m_snapshotMap.add(item-&gt;snapshotUUID(), snapshot);
</span><span class="cx">     m_snapshotCacheSize += snapshot.imageSizeInBytes;
</span><ins>+
+    reduceSnapshotMemoryCost(item-&gt;snapshotUUID());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool ViewSnapshotStore::getSnapshot(WebBackForwardListItem* item, ViewSnapshot&amp; snapshot)
</span><span class="lines">@@ -196,6 +207,84 @@
</span><span class="cx">         removeSnapshotImage(snapshot);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#if USE_IOSURFACE_VIEW_SNAPSHOTS
+static RefPtr&lt;IOSurface&gt; createIOSurfaceFromImage(CGImageRef image)
+{
+    size_t width = CGImageGetWidth(image);
+    size_t height = CGImageGetHeight(image);
+
+    RefPtr&lt;IOSurface&gt; surface = IOSurface::create(IntSize(width, height), ColorSpaceDeviceRGB);
+    RetainPtr&lt;CGContextRef&gt; surfaceContext = surface-&gt;ensurePlatformContext();
+    CGContextDrawImage(surfaceContext.get(), CGRectMake(0, 0, width, height), image);
+    CGContextFlush(surfaceContext.get());
+
+    surface-&gt;setIsVolatile(true);
+
+    return surface;
+}
+#endif
+
+#if USE_JPEG_VIEW_SNAPSHOTS
+static std::pair&lt;RetainPtr&lt;CGImageRef&gt;, size_t&gt; compressImageAsJPEG(CGImageRef image)
+{
+    RetainPtr&lt;NSData&gt; compressedData = adoptNS([[NSMutableData alloc] init]);
+    RetainPtr&lt;CGImageDestinationRef&gt; destination = adoptCF(CGImageDestinationCreateWithData((CFMutableDataRef)compressedData.get(), kUTTypeJPEG, 1, 0));
+
+    RetainPtr&lt;NSDictionary&gt; options = @{
+        (NSString*)kCGImageDestinationLossyCompressionQuality: @0.9
+    };
+
+    CGImageDestinationAddImage(destination.get(), image, (CFDictionaryRef)options.get());
+    CGImageDestinationFinalize(destination.get());
+
+    RetainPtr&lt;CGImageSourceRef&gt; imageSourceRef = adoptCF(CGImageSourceCreateWithData((CFDataRef)compressedData.get(), 0));
+    RetainPtr&lt;CGImageRef&gt; compressedSnapshot = adoptCF(CGImageSourceCreateImageAtIndex(imageSourceRef.get(), 0, 0));
+    
+    return std::make_pair(compressedSnapshot, [compressedData length]);
+}
+#endif
+
+void ViewSnapshotStore::reduceSnapshotMemoryCost(const String&amp; uuid)
+{
+    const auto&amp; snapshotIterator = m_snapshotMap.find(uuid);
+    if (snapshotIterator == m_snapshotMap.end())
+        return;
+    ViewSnapshot&amp; snapshot = snapshotIterator-&gt;value;
+    if (!snapshot.hasImage())
+        return;
+
+#if USE_IOSURFACE_VIEW_SNAPSHOTS
+    snapshot.surface = createIOSurfaceFromImage(snapshot.image.get());
+    snapshot.image = nullptr;
+#elif USE_JPEG_VIEW_SNAPSHOTS
+    RetainPtr&lt;CGImageRef&gt; originalImage = snapshot.image.get();
+    dispatch_async(m_compressionQueue, [uuid, originalImage] {
+        auto imageAndBytes = compressImageAsJPEG(originalImage.get());
+        dispatch_async(dispatch_get_main_queue(), [uuid, imageAndBytes] {
+            ViewSnapshotStore::shared().didCompressSnapshot(uuid, imageAndBytes.first, imageAndBytes.second);
+        });
+    });
+#endif
+}
+
+#if USE_JPEG_VIEW_SNAPSHOTS
+void ViewSnapshotStore::didCompressSnapshot(const String&amp; uuid, RetainPtr&lt;CGImageRef&gt; newImage, size_t newImageSize)
+{
+    const auto&amp; snapshotIterator = m_snapshotMap.find(uuid);
+    if (snapshotIterator == m_snapshotMap.end())
+        return;
+    ViewSnapshot&amp; snapshot = snapshotIterator-&gt;value;
+
+    size_t originalImageSize = snapshot.imageSizeInBytes;
+    if (!newImage || newImageSize &gt; originalImageSize)
+        return;
+    snapshot.image = newImage;
+    snapshot.imageSizeInBytes = newImageSize;
+    m_snapshotCacheSize += newImageSize;
+    m_snapshotCacheSize -= originalImageSize;
+}
+#endif
+
</ins><span class="cx"> bool ViewSnapshot::hasImage() const
</span><span class="cx"> {
</span><span class="cx">     return imageSizeInBytes;
</span><span class="lines">@@ -203,9 +292,13 @@
</span><span class="cx"> 
</span><span class="cx"> void ViewSnapshot::clearImage()
</span><span class="cx"> {
</span><del>-#if PLATFORM(MAC)
</del><ins>+#if USE_IOSURFACE_VIEW_SNAPSHOTS
</ins><span class="cx">     surface = nullptr;
</span><del>-#elif PLATFORM(IOS)
</del><ins>+#endif
+#if USE_IOSURFACE_VIEW_SNAPSHOTS || USE_JPEG_VIEW_SNAPSHOTS
+    image = nullptr;
+#endif
+#if USE_RENDER_SERVER_VIEW_SNAPSHOTS
</ins><span class="cx">     if (slotID)
</span><span class="cx">         [ViewSnapshotStore::snapshottingContext() deleteSlot:slotID];
</span><span class="cx">     slotID = 0;
</span><span class="lines">@@ -213,4 +306,22 @@
</span><span class="cx">     imageSizeInBytes = 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+id ViewSnapshot::asLayerContents()
+{
+#if USE_IOSURFACE_VIEW_SNAPSHOTS
+    if (surface) {
+        if (surface-&gt;setIsVolatile(false) != IOSurface::SurfaceState::Valid)
+            return nullptr;
+
+        return (id)surface-&gt;surface();
+    }
+#endif
+#if USE_IOSURFACE_VIEW_SNAPSHOTS || USE_JPEG_VIEW_SNAPSHOTS
+    return (id)image.get();
+#endif
+#if USE_RENDER_SERVER_VIEW_SNAPSHOTS
+    return [CAContext objectForSlot:slotID];
+#endif
+}
+
</ins><span class="cx"> } // namespace WebKit
</span></span></pre>
</div>
</div>

</body>
</html>