3 #include "SkColorPriv.h"
5 #include "SkPictureRecorder.h"
9 SkString UnderJoin(const char* a, const char* b) {
11 s.appendf("%s_%s", a, b);
15 SkString FileToTaskName(SkString filename) {
16 for (size_t i = 0; i < filename.size(); i++) {
17 if ('_' == filename[i]) { filename[i] = '-'; }
18 if ('.' == filename[i]) { filename[i] = '_'; }
23 SkPicture* RecordPicture(skiagm::GM* gm, SkBBHFactory* factory, bool skr) {
24 const int w = gm->getISize().width(),
25 h = gm->getISize().height();
26 SkPictureRecorder recorder;
28 SkCanvas* canvas = skr ? recorder.EXPERIMENTAL_beginRecording(w, h, factory)
29 : recorder. beginRecording(w, h, factory);
30 canvas->concat(gm->getInitialTransform());
33 return recorder.endRecording();
36 void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
37 bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType));
38 bitmap->eraseColor(0x00000000);
41 void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
42 AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
45 void DrawPicture(const SkPicture& picture, SkBitmap* bitmap) {
46 SkASSERT(bitmap != NULL);
47 SkCanvas canvas(*bitmap);
48 canvas.drawPicture(&picture);
52 static void unpack_565(uint16_t pixel, unsigned* r, unsigned* g, unsigned* b) {
53 *r = SkGetPackedR16(pixel);
54 *g = SkGetPackedG16(pixel);
55 *b = SkGetPackedB16(pixel);
59 static unsigned abs_diff(unsigned a, unsigned b) {
60 return a > b ? a - b : b - a;
63 unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) {
64 if (a.info() != b.info()) {
65 SkFAIL("Can't compare bitmaps of different shapes.");
70 const SkAutoLockPixels lockA(a), lockB(b);
71 if (a.info().colorType() == kRGB_565_SkColorType) {
72 // 565 is special/annoying because its 3 components straddle 2 bytes.
73 const uint16_t* aPixels = (const uint16_t*)a.getPixels();
74 const uint16_t* bPixels = (const uint16_t*)b.getPixels();
75 for (size_t i = 0; i < a.getSize() / 2; i++) {
78 unpack_565(aPixels[i], &ar, &ag, &ab);
79 unpack_565(bPixels[i], &br, &bg, &bb);
80 max = SkTMax(max, abs_diff(ar, br));
81 max = SkTMax(max, abs_diff(ag, bg));
82 max = SkTMax(max, abs_diff(ab, bb));
85 // Everything else we produce is byte aligned, so max component diff == max byte diff.
86 const uint8_t* aBytes = (const uint8_t*)a.getPixels();
87 const uint8_t* bBytes = (const uint8_t*)b.getPixels();
88 for (size_t i = 0; i < a.getSize(); i++) {
89 max = SkTMax(max, abs_diff(aBytes[i], bBytes[i]));
96 bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
97 if (a.info() != b.info()) {
100 const SkAutoLockPixels lockA(a), lockB(b);
101 return 0 == memcmp(a.getPixels(), b.getPixels(), a.getSize());