blob: c3363a627c200b03681fd23dc17f987368ef3420 [file] [log] [blame]
mtklein@google.comd36522d2013-10-16 13:02:15 +00001#include "DMUtil.h"
2
commit-bot@chromium.org69031a42014-05-16 13:03:46 +00003#include "SkColorPriv.h"
mtklein0b36e6b2014-09-11 12:30:12 -07004#include "SkCommandLineFlags.h"
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +00005#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +00006#include "SkPictureRecorder.h"
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +00007
mtklein0b36e6b2014-09-11 12:30:12 -07008DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1",
9 "Matrix to apply to the canvas before drawing.");
10
mtklein@google.comd36522d2013-10-16 13:02:15 +000011namespace DM {
12
mtklein0b36e6b2014-09-11 12:30:12 -070013void CanvasPreflight(SkCanvas* canvas) {
14 if (FLAGS_matrix.count() == 9) {
15 SkMatrix m;
16 for (int i = 0; i < 9; i++) {
17 m[i] = (SkScalar)atof(FLAGS_matrix[i]);
18 }
19 canvas->concat(m);
20 }
21}
22
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000023SkString UnderJoin(const char* a, const char* b) {
mtklein@google.comd36522d2013-10-16 13:02:15 +000024 SkString s;
25 s.appendf("%s_%s", a, b);
26 return s;
27}
28
mtkleine4d3e602014-06-06 09:28:43 -070029SkString 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] = '_'; }
33 }
34 return filename;
35}
36
mtkleina9ceaf52014-09-29 08:44:46 -070037SkPicture* RecordPicture(skiagm::GM* gm, SkBBHFactory* factory) {
robertphillipsa8d7f0b2014-08-29 08:03:56 -070038 const SkScalar w = SkIntToScalar(gm->getISize().width()),
39 h = SkIntToScalar(gm->getISize().height());
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000040 SkPictureRecorder recorder;
mtklein73734562014-06-24 12:28:34 -070041
mtkleina9ceaf52014-09-29 08:44:46 -070042 SkCanvas* canvas = recorder.beginRecording(w, h, factory);
mtklein0b36e6b2014-09-11 12:30:12 -070043 CanvasPreflight(canvas);
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000044 canvas->concat(gm->getInitialTransform());
45 gm->draw(canvas);
46 canvas->flush();
robertphillips@google.com84b18c72014-04-13 19:09:42 +000047 return recorder.endRecording();
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000048}
49
commit-bot@chromium.org26642072014-05-15 17:33:31 +000050void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000051 bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType));
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000052 bitmap->eraseColor(0x00000000);
53}
54
commit-bot@chromium.org26642072014-05-15 17:33:31 +000055void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
56 AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
commit-bot@chromium.org90b5a2a2014-05-14 17:55:32 +000057}
58
mtklein73734562014-06-24 12:28:34 -070059void DrawPicture(const SkPicture& picture, SkBitmap* bitmap) {
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000060 SkASSERT(bitmap != NULL);
61 SkCanvas canvas(*bitmap);
mtklein73734562014-06-24 12:28:34 -070062 canvas.drawPicture(&picture);
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000063 canvas.flush();
64}
65
commit-bot@chromium.org69031a42014-05-16 13:03:46 +000066static void unpack_565(uint16_t pixel, unsigned* r, unsigned* g, unsigned* b) {
67 *r = SkGetPackedR16(pixel);
68 *g = SkGetPackedG16(pixel);
69 *b = SkGetPackedB16(pixel);
70}
71
72// Returns |a-b|.
73static unsigned abs_diff(unsigned a, unsigned b) {
74 return a > b ? a - b : b - a;
75}
76
77unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) {
78 if (a.info() != b.info()) {
79 SkFAIL("Can't compare bitmaps of different shapes.");
80 }
81
82 unsigned max = 0;
83
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000084 const SkAutoLockPixels lockA(a), lockB(b);
commit-bot@chromium.org69031a42014-05-16 13:03:46 +000085 if (a.info().colorType() == kRGB_565_SkColorType) {
86 // 565 is special/annoying because its 3 components straddle 2 bytes.
87 const uint16_t* aPixels = (const uint16_t*)a.getPixels();
88 const uint16_t* bPixels = (const uint16_t*)b.getPixels();
89 for (size_t i = 0; i < a.getSize() / 2; i++) {
90 unsigned ar, ag, ab,
91 br, bg, bb;
92 unpack_565(aPixels[i], &ar, &ag, &ab);
93 unpack_565(bPixels[i], &br, &bg, &bb);
94 max = SkTMax(max, abs_diff(ar, br));
95 max = SkTMax(max, abs_diff(ag, bg));
96 max = SkTMax(max, abs_diff(ab, bb));
97 }
98 } else {
99 // Everything else we produce is byte aligned, so max component diff == max byte diff.
100 const uint8_t* aBytes = (const uint8_t*)a.getPixels();
101 const uint8_t* bBytes = (const uint8_t*)b.getPixels();
102 for (size_t i = 0; i < a.getSize(); i++) {
103 max = SkTMax(max, abs_diff(aBytes[i], bBytes[i]));
104 }
105 }
106
107 return max;
108}
109
110bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
mtklein9c4ff802014-06-03 13:56:54 -0700111 if (a.info() != b.info()) {
112 return false;
113 }
114 const SkAutoLockPixels lockA(a), lockB(b);
115 return 0 == memcmp(a.getPixels(), b.getPixels(), a.getSize());
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +0000116}
117
mtklein@google.comd36522d2013-10-16 13:02:15 +0000118} // namespace DM