initial import
[vuplus_webkit] / Tools / WebKitTestRunner / cairo / TestInvocationCairo.cpp
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  *
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  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
19  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25  * THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "config.h"
29 #include "TestInvocation.h"
30
31 #include "PixelDumpSupport.h"
32 #include <WebKit2/WKImageCairo.h>
33 #include <cairo/cairo.h>
34 #include <cstdio>
35 #include <wtf/Assertions.h>
36 #include <wtf/MD5.h>
37 #include <wtf/StringExtras.h>
38
39 namespace WTR {
40
41 void computeMD5HashStringForCairoSurface(cairo_surface_t* surface, char hashString[33])
42 {
43     ASSERT(cairo_image_surface_get_format(surface) == CAIRO_FORMAT_ARGB32); // ImageDiff assumes 32 bit RGBA, we must as well.
44
45     size_t pixelsHigh = cairo_image_surface_get_height(surface);
46     size_t pixelsWide = cairo_image_surface_get_width(surface);
47     size_t bytesPerRow = cairo_image_surface_get_stride(surface);
48
49     MD5 md5Context;
50     unsigned char* bitmapData = static_cast<unsigned char*>(cairo_image_surface_get_data(surface));
51     for (size_t row = 0; row < pixelsHigh; ++row) {
52         md5Context.addBytes(bitmapData, 4 * pixelsWide);
53         bitmapData += bytesPerRow;
54     }
55     Vector<uint8_t, 16> hash;
56     md5Context.checksum(hash);
57
58     snprintf(hashString, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
59         hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7],
60         hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]);
61 }
62
63 static cairo_status_t writeFunction(void* closure, const unsigned char* data, unsigned int length)
64 {
65     Vector<unsigned char>* in = reinterpret_cast<Vector<unsigned char>*>(closure);
66     in->append(data, length);
67     return CAIRO_STATUS_SUCCESS;
68 }
69
70 static void dumpBitmap(cairo_surface_t* surface, const char* checksum)
71 {
72     Vector<unsigned char> pixelData;
73     cairo_surface_write_to_png_stream(surface, writeFunction, &pixelData);
74     const size_t dataLength = pixelData.size();
75     const unsigned char* data = pixelData.data();
76
77     printPNG(data, dataLength, checksum);
78 }
79
80 void TestInvocation::dumpPixelsAndCompareWithExpected(WKImageRef wkImage)
81 {
82     cairo_surface_t* surface = WKImageCreateCairoSurface(wkImage);
83
84     char actualHash[33];
85     computeMD5HashStringForCairoSurface(surface, actualHash);
86     if (!compareActualHashToExpectedAndDumpResults(actualHash))
87         dumpBitmap(surface, actualHash);
88
89     cairo_surface_destroy(surface);
90 }
91
92 } // namespace WTR