TestInvocationCG.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "TestInvocation.h"
  27. #include "PixelDumpSupport.h"
  28. #include "PlatformWebView.h"
  29. #include "TestController.h"
  30. #include <ImageIO/CGImageDestination.h>
  31. #include <WebKit2/WKImageCG.h>
  32. #include <wtf/MD5.h>
  33. #include <wtf/RetainPtr.h>
  34. #include <wtf/StringExtras.h>
  35. #if PLATFORM(MAC)
  36. #include <LaunchServices/UTCoreTypes.h>
  37. #endif
  38. #if PLATFORM(WIN)
  39. static const CFStringRef kUTTypePNG = CFSTR("public.png");
  40. #endif
  41. namespace WTR {
  42. enum FlipGraphicsContextOrNot {
  43. DontFlipGraphicsContext,
  44. FlipGraphicsContext
  45. };
  46. static CGContextRef createCGContextFromImage(WKImageRef wkImage, FlipGraphicsContextOrNot flip = DontFlipGraphicsContext)
  47. {
  48. RetainPtr<CGImageRef> image = adoptCF(WKImageCreateCGImage(wkImage));
  49. size_t pixelsWide = CGImageGetWidth(image.get());
  50. size_t pixelsHigh = CGImageGetHeight(image.get());
  51. size_t rowBytes = (4 * pixelsWide + 63) & ~63;
  52. // Creating this bitmap in the device color space should prevent any color conversion when the image of the web view is drawn into it.
  53. RetainPtr<CGColorSpaceRef> colorSpace = adoptCF(CGColorSpaceCreateDeviceRGB());
  54. CGContextRef context = CGBitmapContextCreate(0, pixelsWide, pixelsHigh, 8, rowBytes, colorSpace.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
  55. if (flip == FlipGraphicsContext) {
  56. CGContextSaveGState(context);
  57. CGContextScaleCTM(context, 1, -1);
  58. CGContextTranslateCTM(context, 0, -static_cast<CGFloat>(pixelsHigh));
  59. }
  60. CGContextDrawImage(context, CGRectMake(0, 0, pixelsWide, pixelsHigh), image.get());
  61. if (flip == FlipGraphicsContext)
  62. CGContextRestoreGState(context);
  63. return context;
  64. }
  65. void computeMD5HashStringForContext(CGContextRef bitmapContext, char hashString[33])
  66. {
  67. ASSERT(CGBitmapContextGetBitsPerPixel(bitmapContext) == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
  68. size_t pixelsHigh = CGBitmapContextGetHeight(bitmapContext);
  69. size_t pixelsWide = CGBitmapContextGetWidth(bitmapContext);
  70. size_t bytesPerRow = CGBitmapContextGetBytesPerRow(bitmapContext);
  71. // We need to swap the bytes to ensure consistent hashes independently of endianness
  72. MD5 md5;
  73. unsigned char* bitmapData = static_cast<unsigned char*>(CGBitmapContextGetData(bitmapContext));
  74. #if PLATFORM(MAC)
  75. if ((CGBitmapContextGetBitmapInfo(bitmapContext) & kCGBitmapByteOrderMask) == kCGBitmapByteOrder32Big) {
  76. for (unsigned row = 0; row < pixelsHigh; row++) {
  77. Vector<uint8_t> buffer(4 * pixelsWide);
  78. for (unsigned column = 0; column < pixelsWide; column++)
  79. buffer[column] = OSReadLittleInt32(bitmapData, 4 * column);
  80. md5.addBytes(buffer);
  81. bitmapData += bytesPerRow;
  82. }
  83. } else
  84. #endif
  85. {
  86. for (unsigned row = 0; row < pixelsHigh; row++) {
  87. md5.addBytes(bitmapData, 4 * pixelsWide);
  88. bitmapData += bytesPerRow;
  89. }
  90. }
  91. Vector<uint8_t, 16> hash;
  92. md5.checksum(hash);
  93. hashString[0] = '\0';
  94. for (int i = 0; i < 16; i++)
  95. snprintf(hashString, 33, "%s%02x", hashString, hash[i]);
  96. }
  97. static void dumpBitmap(CGContextRef bitmapContext, const char* checksum)
  98. {
  99. RetainPtr<CGImageRef> image = adoptCF(CGBitmapContextCreateImage(bitmapContext));
  100. RetainPtr<CFMutableDataRef> imageData = adoptCF(CFDataCreateMutable(0, 0));
  101. RetainPtr<CGImageDestinationRef> imageDest = adoptCF(CGImageDestinationCreateWithData(imageData.get(), kUTTypePNG, 1, 0));
  102. CGImageDestinationAddImage(imageDest.get(), image.get(), 0);
  103. CGImageDestinationFinalize(imageDest.get());
  104. const unsigned char* data = CFDataGetBytePtr(imageData.get());
  105. const size_t dataLength = CFDataGetLength(imageData.get());
  106. printPNG(data, dataLength, checksum);
  107. }
  108. static void paintRepaintRectOverlay(CGContextRef context, WKImageRef image, WKArrayRef repaintRects)
  109. {
  110. WKSize imageSize = WKImageGetSize(image);
  111. CGContextSaveGState(context);
  112. // Using a transparency layer is easier than futzing with clipping.
  113. CGContextBeginTransparencyLayer(context, 0);
  114. // Flip the context.
  115. CGContextScaleCTM(context, 1, -1);
  116. CGContextTranslateCTM(context, 0, -imageSize.height);
  117. CGContextSetRGBFillColor(context, 0, 0, 0, static_cast<CGFloat>(0.66));
  118. CGContextFillRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height));
  119. // Clear the repaint rects.
  120. size_t count = WKArrayGetSize(repaintRects);
  121. for (size_t i = 0; i < count; ++i) {
  122. WKRect rect = WKRectGetValue(static_cast<WKRectRef>(WKArrayGetItemAtIndex(repaintRects, i)));
  123. CGRect cgRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
  124. CGContextClearRect(context, cgRect);
  125. }
  126. CGContextEndTransparencyLayer(context);
  127. CGContextRestoreGState(context);
  128. }
  129. void TestInvocation::dumpPixelsAndCompareWithExpected(WKImageRef image, WKArrayRef repaintRects)
  130. {
  131. PlatformWebView* webView = TestController::shared().mainWebView();
  132. WKRetainPtr<WKImageRef> windowSnapshot = webView->windowSnapshotImage();
  133. // There is no way at this time to fake a window's scale factor, so we need to avoid the window
  134. // snapshots for HiDPI tests.
  135. if (WKPageGetBackingScaleFactor(webView->page()) != 1)
  136. windowSnapshot = 0;
  137. RetainPtr<CGContextRef> context;
  138. if (windowSnapshot)
  139. context = adoptCF(createCGContextFromImage(windowSnapshot.get(), DontFlipGraphicsContext));
  140. else
  141. context = adoptCF(createCGContextFromImage(image));
  142. // A non-null repaintRects array means we're doing a repaint test.
  143. if (repaintRects)
  144. paintRepaintRectOverlay(context.get(), image, repaintRects);
  145. char actualHash[33];
  146. computeMD5HashStringForContext(context.get(), actualHash);
  147. if (!compareActualHashToExpectedAndDumpResults(actualHash))
  148. dumpBitmap(context.get(), actualHash);
  149. }
  150. } // namespace WTR