3 #include "SkColorPriv.h"
4 #include "SkCommandLineFlags.h"
6 #include "SkPictureRecorder.h"
8 DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1",
9 "Matrix to apply to the canvas before drawing.");
13 void CanvasPreflight(SkCanvas* canvas) {
14 if (FLAGS_matrix.count() == 9) {
16 for (int i = 0; i < 9; i++) {
17 m[i] = (SkScalar)atof(FLAGS_matrix[i]);
23 SkString UnderJoin(const char* a, const char* b) {
25 s.appendf("%s_%s", a, b);
29 SkString FileToTaskName(SkString filename) {
30 for (size_t i = 0; i < filename.size(); i++) {
31 if ('_' == filename[i]) { filename[i] = '-'; }
32 if ('.' == filename[i]) { filename[i] = '_'; }
37 SkPicture* RecordPicture(skiagm::GM* gm, SkBBHFactory* factory, bool skr) {
38 const SkScalar w = SkIntToScalar(gm->getISize().width()),
39 h = SkIntToScalar(gm->getISize().height());
40 SkPictureRecorder recorder;
42 SkCanvas* canvas = skr ? recorder.EXPERIMENTAL_beginRecording(w, h, factory)
43 : recorder. DEPRECATED_beginRecording(w, h, factory);
44 CanvasPreflight(canvas);
45 canvas->concat(gm->getInitialTransform());
48 return recorder.endRecording();
51 void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
52 bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType));
53 bitmap->eraseColor(0x00000000);
56 void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
57 AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
60 void DrawPicture(const SkPicture& picture, SkBitmap* bitmap) {
61 SkASSERT(bitmap != NULL);
62 SkCanvas canvas(*bitmap);
63 canvas.drawPicture(&picture);
67 static void unpack_565(uint16_t pixel, unsigned* r, unsigned* g, unsigned* b) {
68 *r = SkGetPackedR16(pixel);
69 *g = SkGetPackedG16(pixel);
70 *b = SkGetPackedB16(pixel);
74 static unsigned abs_diff(unsigned a, unsigned b) {
75 return a > b ? a - b : b - a;
78 unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) {
79 if (a.info() != b.info()) {
80 SkFAIL("Can't compare bitmaps of different shapes.");
85 const SkAutoLockPixels lockA(a), lockB(b);
86 if (a.info().colorType() == kRGB_565_SkColorType) {
87 // 565 is special/annoying because its 3 components straddle 2 bytes.
88 const uint16_t* aPixels = (const uint16_t*)a.getPixels();
89 const uint16_t* bPixels = (const uint16_t*)b.getPixels();
90 for (size_t i = 0; i < a.getSize() / 2; i++) {
93 unpack_565(aPixels[i], &ar, &ag, &ab);
94 unpack_565(bPixels[i], &br, &bg, &bb);
95 max = SkTMax(max, abs_diff(ar, br));
96 max = SkTMax(max, abs_diff(ag, bg));
97 max = SkTMax(max, abs_diff(ab, bb));
100 // Everything else we produce is byte aligned, so max component diff == max byte diff.
101 const uint8_t* aBytes = (const uint8_t*)a.getPixels();
102 const uint8_t* bBytes = (const uint8_t*)b.getPixels();
103 for (size_t i = 0; i < a.getSize(); i++) {
104 max = SkTMax(max, abs_diff(aBytes[i], bBytes[i]));
111 bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
112 if (a.info() != b.info()) {
115 const SkAutoLockPixels lockA(a), lockB(b);
116 return 0 == memcmp(a.getPixels(), b.getPixels(), a.getSize());