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
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include "TestInvocation.h"
31 #include "PixelDumpSupport.h"
32 #include <WebKit2/WKImageCairo.h>
33 #include <cairo/cairo.h>
35 #include <wtf/Assertions.h>
37 #include <wtf/StringExtras.h>
41 void computeMD5HashStringForCairoSurface(cairo_surface_t* surface, char hashString[33])
43 ASSERT(cairo_image_surface_get_format(surface) == CAIRO_FORMAT_ARGB32); // ImageDiff assumes 32 bit RGBA, we must as well.
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);
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;
55 Vector<uint8_t, 16> hash;
56 md5Context.checksum(hash);
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]);
63 static cairo_status_t writeFunction(void* closure, const unsigned char* data, unsigned int length)
65 Vector<unsigned char>* in = reinterpret_cast<Vector<unsigned char>*>(closure);
66 in->append(data, length);
67 return CAIRO_STATUS_SUCCESS;
70 static void dumpBitmap(cairo_surface_t* surface, const char* checksum)
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();
77 printPNG(data, dataLength, checksum);
80 void TestInvocation::dumpPixelsAndCompareWithExpected(WKImageRef wkImage)
82 cairo_surface_t* surface = WKImageCreateCairoSurface(wkImage);
85 computeMD5HashStringForCairoSurface(surface, actualHash);
86 if (!compareActualHashToExpectedAndDumpResults(actualHash))
87 dumpBitmap(surface, actualHash);
89 cairo_surface_destroy(surface);