PixelDumpSupportMac.mm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
  3. * (C) 2007 Graham Dennis (graham.dennis@gmail.com)
  4. * (C) 2007 Eric Seidel <eric@webkit.org>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  16. * its contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #include "PixelDumpSupport.h"
  32. #include "PixelDumpSupportCG.h"
  33. #include "DumpRenderTree.h"
  34. #include "TestRunner.h"
  35. #include <CoreGraphics/CGBitmapContext.h>
  36. #include <wtf/Assertions.h>
  37. #include <wtf/RefPtr.h>
  38. #import <WebKit/WebCoreStatistics.h>
  39. #import <WebKit/WebDocumentPrivate.h>
  40. #import <WebKit/WebHTMLViewPrivate.h>
  41. #import <WebKit/WebKit.h>
  42. #import <WebKit/WebViewPrivate.h>
  43. static PassRefPtr<BitmapContext> createBitmapContext(size_t pixelsWide, size_t pixelsHigh, size_t& rowBytes, void*& buffer)
  44. {
  45. rowBytes = (4 * pixelsWide + 63) & ~63; // Use a multiple of 64 bytes to improve CG performance
  46. buffer = calloc(pixelsHigh, rowBytes);
  47. if (!buffer)
  48. return 0;
  49. // Creating this bitmap in the device color space prevents any color conversion when the image of the web view is drawn into it.
  50. RetainPtr<CGColorSpaceRef> colorSpace = adoptCF(CGColorSpaceCreateDeviceRGB());
  51. CGContextRef context = CGBitmapContextCreate(buffer, pixelsWide, pixelsHigh, 8, rowBytes, colorSpace.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host); // Use ARGB8 on PPC or BGRA8 on X86 to improve CG performance
  52. if (!context) {
  53. free(buffer);
  54. return 0;
  55. }
  56. return BitmapContext::createByAdoptingBitmapAndContext(buffer, context);
  57. }
  58. static void paintRepaintRectOverlay(WebView* webView, CGContextRef context)
  59. {
  60. CGRect viewRect = NSRectToCGRect([webView bounds]);
  61. CGContextSaveGState(context);
  62. // Using a transparency layer is easier than futzing with clipping.
  63. CGContextBeginTransparencyLayer(context, 0);
  64. // Flip the context.
  65. CGContextScaleCTM(context, 1, -1);
  66. CGContextTranslateCTM(context, 0, -viewRect.size.height);
  67. CGContextSetRGBFillColor(context, 0, 0, 0, static_cast<CGFloat>(0.66));
  68. CGContextFillRect(context, viewRect);
  69. NSArray *repaintRects = [webView trackedRepaintRects];
  70. if (repaintRects) {
  71. for (NSValue *value in repaintRects) {
  72. CGRect currRect = NSRectToCGRect([value rectValue]);
  73. CGContextClearRect(context, currRect);
  74. }
  75. }
  76. CGContextEndTransparencyLayer(context);
  77. CGContextRestoreGState(context);
  78. }
  79. PassRefPtr<BitmapContext> createBitmapContextFromWebView(bool onscreen, bool incrementalRepaint, bool sweepHorizontally, bool drawSelectionRect)
  80. {
  81. WebView* view = [mainFrame webView];
  82. // If the WebHTMLView uses accelerated compositing, we need for force the on-screen capture path
  83. // and also force Core Animation to start its animations with -display since the DRT window has autodisplay disabled.
  84. if ([view _isUsingAcceleratedCompositing])
  85. onscreen = YES;
  86. float deviceScaleFactor = [view _backingScaleFactor];
  87. NSSize webViewSize = [view frame].size;
  88. size_t pixelsWide = static_cast<size_t>(webViewSize.width * deviceScaleFactor);
  89. size_t pixelsHigh = static_cast<size_t>(webViewSize.height * deviceScaleFactor);
  90. size_t rowBytes = 0;
  91. void* buffer = 0;
  92. RefPtr<BitmapContext> bitmapContext = createBitmapContext(pixelsWide, pixelsHigh, rowBytes, buffer);
  93. if (!bitmapContext)
  94. return 0;
  95. CGContextRef context = bitmapContext->cgContext();
  96. CGContextScaleCTM(context, deviceScaleFactor, deviceScaleFactor);
  97. NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
  98. ASSERT(nsContext);
  99. if (incrementalRepaint) {
  100. if (sweepHorizontally) {
  101. for (NSRect column = NSMakeRect(0, 0, 1, webViewSize.height); column.origin.x < webViewSize.width; column.origin.x++)
  102. [view displayRectIgnoringOpacity:column inContext:nsContext];
  103. } else {
  104. for (NSRect line = NSMakeRect(0, 0, webViewSize.width, 1); line.origin.y < webViewSize.height; line.origin.y++)
  105. [view displayRectIgnoringOpacity:line inContext:nsContext];
  106. }
  107. } else {
  108. if (deviceScaleFactor != 1) {
  109. // Call displayRectIgnoringOpacity for HiDPI tests since it ensures we paint directly into the context
  110. // that we have appropriately sized and scaled.
  111. [view displayRectIgnoringOpacity:[view bounds] inContext:nsContext];
  112. if ([view isTrackingRepaints])
  113. paintRepaintRectOverlay(view, context);
  114. } else if (onscreen) {
  115. // displayIfNeeded does not update the CA layers if the layer-hosting view was not marked as needing display, so
  116. // we're at the mercy of CA's display-link callback to update layers in time. So we need to force a display of the view
  117. // to get AppKit to update the CA layers synchronously.
  118. // FIXME: this will break repaint testing if we have compositing in repaint tests
  119. // (displayWebView() painted gray over the webview, but we'll be making everything repaint again).
  120. [view display];
  121. // Ask the window server to provide us a composited version of the *real* window content including surfaces (i.e. OpenGL content)
  122. // Note that the returned image might differ very slightly from the window backing because of dithering artifacts in the window server compositor.
  123. CGImageRef image = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionIncludingWindow, [[view window] windowNumber], kCGWindowImageBoundsIgnoreFraming | kCGWindowImageShouldBeOpaque);
  124. CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
  125. CGImageRelease(image);
  126. if ([view isTrackingRepaints])
  127. paintRepaintRectOverlay(view, context);
  128. } else {
  129. // Make sure the view has been painted.
  130. [view displayIfNeeded];
  131. // Grab directly the contents of the window backing buffer (this ignores any surfaces on the window)
  132. // FIXME: This path is suboptimal: data is read from window backing store, converted to RGB8 then drawn again into an RGBA8 bitmap
  133. [view lockFocus];
  134. NSBitmapImageRep *imageRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:[view frame]] autorelease];
  135. [view unlockFocus];
  136. RetainPtr<NSGraphicsContext> savedContext = [NSGraphicsContext currentContext];
  137. [NSGraphicsContext setCurrentContext:nsContext];
  138. [imageRep draw];
  139. if ([view isTrackingRepaints])
  140. paintRepaintRectOverlay(view, context);
  141. [NSGraphicsContext setCurrentContext:savedContext.get()];
  142. }
  143. }
  144. if (drawSelectionRect) {
  145. NSView *documentView = [[mainFrame frameView] documentView];
  146. ASSERT([documentView conformsToProtocol:@protocol(WebDocumentSelection)]);
  147. NSRect rect = [documentView convertRect:[(id <WebDocumentSelection>)documentView selectionRect] fromView:nil];
  148. CGContextSaveGState(context);
  149. CGContextSetLineWidth(context, 1.0);
  150. CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
  151. CGContextStrokeRect(context, CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height));
  152. CGContextRestoreGState(context);
  153. }
  154. return bitmapContext.release();
  155. }
  156. PassRefPtr<BitmapContext> createPagedBitmapContext()
  157. {
  158. int pageWidthInPixels = TestRunner::viewWidth;
  159. int pageHeightInPixels = TestRunner::viewHeight;
  160. int numberOfPages = [mainFrame numberOfPagesWithPageWidth:pageWidthInPixels pageHeight:pageHeightInPixels];
  161. size_t rowBytes = 0;
  162. void* buffer = 0;
  163. RefPtr<BitmapContext> bitmapContext = createBitmapContext(pageWidthInPixels, numberOfPages * (pageHeightInPixels + 1) - 1, rowBytes, buffer);
  164. [mainFrame printToCGContext:bitmapContext->cgContext() pageWidth:pageWidthInPixels pageHeight:pageHeightInPixels];
  165. return bitmapContext.release();
  166. }