<html>
    <head>
      <base href="https://bugs.webkit.org/" />
    </head>
    <body>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - No reliable way to get a snapshot of WKWebView (macOS)"
   href="https://bugs.webkit.org/show_bug.cgi?id=161450#c3">Comment # 3</a>
              on <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - No reliable way to get a snapshot of WKWebView (macOS)"
   href="https://bugs.webkit.org/show_bug.cgi?id=161450">bug 161450</a>
              from <span class="vcard"><a class="email" href="mailto:thorton&#64;apple.com" title="Tim Horton &lt;thorton&#64;apple.com&gt;"> <span class="fn">Tim Horton</span></a>
</span></b>
        <pre>Comment on <span class=""><a href="attachment.cgi?id=288553&amp;action=diff" name="attach_288553" title="Patch">attachment 288553</a> <a href="attachment.cgi?id=288553&amp;action=edit" title="Patch">[details]</a></span>
Patch

View in context: <a href="https://bugs.webkit.org/attachment.cgi?id=288553&amp;action=review">https://bugs.webkit.org/attachment.cgi?id=288553&amp;action=review</a>

Thanks for the patch! Since it adds new public API, the process for getting it landed is a little... different than usual. I'll CC the appropriate people.

<span class="quote">&gt; Source/WebKit2/ChangeLog:1
&gt; +2016-09-11  DAN SAUNDERS  &lt;<a href="mailto:dasau&#64;microsoft.com">dasau&#64;microsoft.com</a>&gt;</span >

You probably want to repair the capitalization of your name.

<span class="quote">&gt; Source/WebKit2/UIProcess/API/Cocoa/WKWebView.h:228
&gt; +- (void)snapshotViewportWithScaleFactor:(CGFloat)scaleFactor completionHandler:(void(^)(CGImageRef _Nullable snapshotImage, NSError * _Nullable error))completionHandler WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));</span >

I think I prefer something more like the API that we have for iOS WebKit:

- (void)_snapshotRect:(CGRect)rectInViewCoordinates intoImageOfWidth:(CGFloat)imageWidth completionHandler:(void(^)(CGImageRef))completionHandler;

A rect and a width instead of a scale. This has worked out well for its clients, too.

<span class="quote">&gt; Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm:885
&gt; +    _page-&gt;takeSnapshot(snapshotRect, bitmapSize, WebKit::SnapshotOptionsInViewCoordinates, [handler](const WebKit::ShareableBitmap::Handle&amp; imageHandle, WebKit::CallbackBase::Error errorCode) {</span >

This is a software painted snapshot, meaning that 3D transforms will be flattened and ugly, and video/WebGL may-or-may-not work. So, it's not great.

We have code (WebViewImpl::takeViewSnapshot) that shows how to do a window-server snapshot, which captures all of those things, but it has a downside: if the web view is obscured, the thing it's obscured will will end up in the snapshot as well.

_snapshotRect:intoImageOfWidth:completionHandler: gets around this, but uses functions that are only available/possible on iOS. We should definitely make use of that to implement this method on iOS, though, since it is much, much better than a software snapshot.

<span class="quote">&gt; Tools/TestWebKitAPI/Tests/WebKit2Cocoa/WKWebViewSnapshot.mm:44
&gt; +    [webView snapshotViewportWithScaleFactor:1.0 completionHandler:^(CGImageRef snapshotImage, NSError * error) {</span >

No space between the * and 'error'

<span class="quote">&gt; Tools/TestWebKitAPI/Tests/WebKit2Cocoa/WKWebViewSnapshot.mm:65
&gt; +    [webView snapshotViewportWithScaleFactor:1.0 completionHandler:^(CGImageRef snapshotImage, NSError * error) {</span >

Ditto.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>