TestInvocationCairo.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2009 Apple Inc. All rights reserved.
  3. * (C) 2011 Brent Fulgham <bfulgham@webkit.org>. All rights reserved.
  4. * (C) 2010, 2011 Igalia S.L
  5. * (C) 2012 Intel Corporation. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  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. *
  16. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  26. * THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #include "TestInvocation.h"
  30. #include "PixelDumpSupport.h"
  31. #include "PlatformWebView.h"
  32. #include "TestController.h"
  33. #include <WebKit2/WKImageCairo.h>
  34. #include <cairo/cairo.h>
  35. #include <cstdio>
  36. #include <wtf/Assertions.h>
  37. #include <wtf/MD5.h>
  38. #include <wtf/StringExtras.h>
  39. namespace WTR {
  40. void computeMD5HashStringForCairoSurface(cairo_surface_t* surface, char hashString[33])
  41. {
  42. ASSERT(cairo_image_surface_get_format(surface) == CAIRO_FORMAT_ARGB32); // ImageDiff assumes 32 bit RGBA, we must as well.
  43. size_t pixelsHigh = cairo_image_surface_get_height(surface);
  44. size_t pixelsWide = cairo_image_surface_get_width(surface);
  45. size_t bytesPerRow = cairo_image_surface_get_stride(surface);
  46. MD5 md5Context;
  47. unsigned char* bitmapData = static_cast<unsigned char*>(cairo_image_surface_get_data(surface));
  48. for (size_t row = 0; row < pixelsHigh; ++row) {
  49. md5Context.addBytes(bitmapData, 4 * pixelsWide);
  50. bitmapData += bytesPerRow;
  51. }
  52. Vector<uint8_t, 16> hash;
  53. md5Context.checksum(hash);
  54. snprintf(hashString, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  55. hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7],
  56. hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]);
  57. }
  58. static cairo_status_t writeFunction(void* closure, const unsigned char* data, unsigned length)
  59. {
  60. Vector<unsigned char>* in = reinterpret_cast<Vector<unsigned char>*>(closure);
  61. in->append(data, length);
  62. return CAIRO_STATUS_SUCCESS;
  63. }
  64. static void dumpBitmap(cairo_surface_t* surface, const char* checksum)
  65. {
  66. Vector<unsigned char> pixelData;
  67. cairo_surface_write_to_png_stream(surface, writeFunction, &pixelData);
  68. const size_t dataLength = pixelData.size();
  69. const unsigned char* data = pixelData.data();
  70. printPNG(data, dataLength, checksum);
  71. }
  72. static void paintRepaintRectOverlay(cairo_surface_t* surface, WKArrayRef repaintRects)
  73. {
  74. cairo_t* context = cairo_create(surface);
  75. cairo_push_group(context);
  76. // Paint the gray mask over the original image.
  77. cairo_set_source_rgba(context, 0, 0, 0, 0.66);
  78. cairo_paint(context);
  79. // Paint transparent rectangles over the mask to show the repainted regions.
  80. cairo_set_source_rgba(context, 0, 0, 0, 0);
  81. cairo_set_operator(context, CAIRO_OPERATOR_SOURCE);
  82. size_t count = WKArrayGetSize(repaintRects);
  83. for (size_t i = 0; i < count; ++i) {
  84. WKRect rect = WKRectGetValue(static_cast<WKRectRef>(WKArrayGetItemAtIndex(repaintRects, i)));
  85. cairo_rectangle(context, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
  86. cairo_fill(context);
  87. }
  88. cairo_pop_group_to_source(context);
  89. cairo_paint(context);
  90. cairo_destroy(context);
  91. }
  92. #if PLATFORM(EFL)
  93. void TestInvocation::forceRepaintDoneCallback(WKErrorRef, void *context)
  94. {
  95. static_cast<TestInvocation*>(context)->m_gotRepaint = true;
  96. TestController::shared().notifyDone();
  97. }
  98. #endif
  99. void TestInvocation::dumpPixelsAndCompareWithExpected(WKImageRef wkImage, WKArrayRef repaintRects)
  100. {
  101. #if USE(ACCELERATED_COMPOSITING) && PLATFORM(EFL)
  102. UNUSED_PARAM(wkImage);
  103. cairo_surface_t* surface;
  104. WKPageRef page = TestController::shared().mainWebView()->page();
  105. WKPageForceRepaint(page, this, &forceRepaintDoneCallback);
  106. TestController::shared().runUntil(m_gotRepaint, TestController::ShortTimeout);
  107. if (!m_gotRepaint) {
  108. m_error = true;
  109. m_errorMessage = "Timed out waiting for repaint\n";
  110. m_webProcessIsUnresponsive = true;
  111. return;
  112. }
  113. surface = WKImageCreateCairoSurface(TestController::shared().mainWebView()->windowSnapshotImage().get());
  114. #else
  115. cairo_surface_t* surface = WKImageCreateCairoSurface(wkImage);
  116. #endif
  117. if (repaintRects)
  118. paintRepaintRectOverlay(surface, repaintRects);
  119. char actualHash[33];
  120. computeMD5HashStringForCairoSurface(surface, actualHash);
  121. if (!compareActualHashToExpectedAndDumpResults(actualHash))
  122. dumpBitmap(surface, actualHash);
  123. cairo_surface_destroy(surface);
  124. }
  125. } // namespace WTR