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
29#include "config.h"
30#include "TestInvocation.h"
31
32#include "PixelDumpSupport.h"
33#include "PlatformWebView.h"
34#include "TestController.h"
35#include <WebKit/WKImageCairo.h>
36#include <cairo/cairo.h>
37#include <cstdio>
38#include <wtf/Assertions.h>
39#include <wtf/MD5.h>
40#include <wtf/StringExtras.h>
41
42namespace WTR {
43
44static void computeMD5HashStringForCairoSurface(cairo_surface_t* surface, char hashString[33])
45{
46 ASSERT(cairo_image_surface_get_format(surface) == CAIRO_FORMAT_ARGB32 || cairo_image_surface_get_format(surface) == CAIRO_FORMAT_RGB24);
47
48 size_t pixelsHigh = cairo_image_surface_get_height(surface);
49 size_t pixelsWide = cairo_image_surface_get_width(surface);
50 size_t bytesPerRow = cairo_image_surface_get_stride(surface);
51
52 MD5 md5Context;
53 unsigned char* bitmapData = static_cast<unsigned char*>(cairo_image_surface_get_data(surface));
54 for (size_t row = 0; row < pixelsHigh; ++row) {
55 md5Context.addBytes(bitmapData, 4 * pixelsWide);
56 bitmapData += bytesPerRow;
57 }
58 MD5::Digest hash;
59 md5Context.checksum(hash);
60
61 snprintf(hashString, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
62 hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7],
63 hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]);
64}
65
66static cairo_status_t writeFunction(void* closure, const unsigned char* data, unsigned length)
67{
68 Vector<unsigned char>* in = reinterpret_cast<Vector<unsigned char>*>(closure);
69 in->append(data, length);
70 return CAIRO_STATUS_SUCCESS;
71}
72
73static void dumpBitmap(cairo_surface_t* surface, const char* checksum)
74{
75 Vector<unsigned char> pixelData;
76 cairo_surface_write_to_png_stream(surface, writeFunction, &pixelData);
77 const size_t dataLength = pixelData.size();
78 const unsigned char* data = pixelData.data();
79
80 printPNG(data, dataLength, checksum);
81}
82
83static void paintRepaintRectOverlay(cairo_surface_t* surface, WKArrayRef repaintRects)
84{
85 cairo_t* context = cairo_create(surface);
86
87 cairo_push_group(context);
88
89 // Paint the gray mask over the original image.
90 cairo_set_source_rgba(context, 0, 0, 0, 0.66);
91 cairo_paint(context);
92
93 // Paint transparent rectangles over the mask to show the repainted regions.
94 cairo_set_source_rgba(context, 0, 0, 0, 0);
95 cairo_set_operator(context, CAIRO_OPERATOR_SOURCE);
96 size_t count = WKArrayGetSize(repaintRects);
97 for (size_t i = 0; i < count; ++i) {
98 WKRect rect = WKRectGetValue(static_cast<WKRectRef>(WKArrayGetItemAtIndex(repaintRects, i)));
99 cairo_rectangle(context, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
100 cairo_fill(context);
101 }
102
103 cairo_pop_group_to_source(context);
104 cairo_paint(context);
105
106 cairo_destroy(context);
107}
108
109void TestInvocation::dumpPixelsAndCompareWithExpected(SnapshotResultType snapshotType, WKArrayRef repaintRects, WKImageRef image)
110{
111 cairo_surface_t* surface = nullptr;
112 switch (snapshotType) {
113 case SnapshotResultType::WebContents:
114 surface = WKImageCreateCairoSurface(image);
115 break;
116 case SnapshotResultType::WebView:
117 surface = TestController::singleton().mainWebView()->windowSnapshotImage();
118 break;
119 }
120
121 if (repaintRects)
122 paintRepaintRectOverlay(surface, repaintRects);
123
124 char actualHash[33];
125 computeMD5HashStringForCairoSurface(surface, actualHash);
126 if (!compareActualHashToExpectedAndDumpResults(actualHash))
127 dumpBitmap(surface, actualHash);
128
129 cairo_surface_destroy(surface);
130}
131
132} // namespace WTR
133