[ Mojave WK1 ] Some Image tests are flakey failures and are failing in tandem with zoomed in or blank image results
https://bugs.webkit.org/show_bug.cgi?id=193108

Patch by Said Abou-Hallawa <sabouhallawa@apple.com> on 2019-07-15
Reviewed by Simon Fraser.

Add a workaround for <rdar://problem/17084993> in createBitmapContextFromWebView().
Re-request the snapshot at kCGWindowImageNominalResolution if it was captured
at the wrong scale.

* DumpRenderTree/mac/PixelDumpSupportMac.mm:
(takeWindowSnapshot):
(createBitmapContextFromWebView):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@247466 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Tools/ChangeLog b/Tools/ChangeLog
index 8f42aac..7674946 100644
--- a/Tools/ChangeLog
+++ b/Tools/ChangeLog
@@ -1,3 +1,18 @@
+2019-07-15  Said Abou-Hallawa  <sabouhallawa@apple.com>
+
+        [ Mojave WK1 ] Some Image tests are flakey failures and are failing in tandem with zoomed in or blank image results
+        https://bugs.webkit.org/show_bug.cgi?id=193108
+
+        Reviewed by Simon Fraser.
+
+        Add a workaround for <rdar://problem/17084993> in createBitmapContextFromWebView().
+        Re-request the snapshot at kCGWindowImageNominalResolution if it was captured
+        at the wrong scale.
+
+        * DumpRenderTree/mac/PixelDumpSupportMac.mm:
+        (takeWindowSnapshot):
+        (createBitmapContextFromWebView):
+
 2019-07-15  Brady Eidson  <beidson@apple.com>
 
         Make WKURLSchemeTask thread safe.
diff --git a/Tools/DumpRenderTree/mac/PixelDumpSupportMac.mm b/Tools/DumpRenderTree/mac/PixelDumpSupportMac.mm
index 0b2a97c..7cabb12 100644
--- a/Tools/DumpRenderTree/mac/PixelDumpSupportMac.mm
+++ b/Tools/DumpRenderTree/mac/PixelDumpSupportMac.mm
@@ -36,6 +36,7 @@
 #import "TestRunner.h"
 #import <CoreGraphics/CGBitmapContext.h>
 #import <QuartzCore/QuartzCore.h>
+#import <pal/spi/cg/CoreGraphicsSPI.h>
 #import <wtf/Assertions.h>
 #import <wtf/RefPtr.h>
 
@@ -82,6 +83,12 @@
     CGContextRestoreGState(context);
 }
 
+static CGImageRef takeWindowSnapshot(CGSWindowID windowID, CGWindowImageOption imageOptions)
+{
+    imageOptions |= kCGWindowImageBoundsIgnoreFraming | kCGWindowImageShouldBeOpaque;
+    return CGWindowListCreateImage(CGRectNull, kCGWindowListOptionIncludingWindow, windowID, imageOptions);
+}
+
 RefPtr<BitmapContext> createBitmapContextFromWebView(bool onscreen, bool incrementalRepaint, bool sweepHorizontally, bool drawSelectionRect)
 {
     WebView* view = [mainFrame webView];
@@ -128,7 +135,19 @@
 
             // Ask the window server to provide us a composited version of the *real* window content including surfaces (i.e. OpenGL content)
             // Note that the returned image might differ very slightly from the window backing because of dithering artifacts in the window server compositor.
-            CGImageRef image = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionIncludingWindow, [[view window] windowNumber], kCGWindowImageBoundsIgnoreFraming | kCGWindowImageShouldBeOpaque);
+            NSWindow *window = [view window];
+            CGImageRef image = takeWindowSnapshot([window windowNumber], kCGWindowImageDefault);
+
+            if (image) {
+                // Work around <rdar://problem/17084993>; re-request the snapshot at kCGWindowImageNominalResolution if it was captured at the wrong scale.
+                CGFloat desiredSnapshotWidth = window.frame.size.width * deviceScaleFactor;
+                if (CGImageGetWidth(image) != desiredSnapshotWidth)
+                    image = takeWindowSnapshot([window windowNumber], kCGWindowImageNominalResolution);
+            }
+
+            if (!image)
+                return nullptr;
+
             CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
             CGImageRelease(image);