blob: 19b1c0f397f99e9cee8f75c41a4c0b21e243e95e [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"
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +00004#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +00005#include "SkPictureRecorder.h"
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +00006
mtklein@google.comd36522d2013-10-16 13:02:15 +00007namespace DM {
8
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +00009SkString UnderJoin(const char* a, const char* b) {
mtklein@google.comd36522d2013-10-16 13:02:15 +000010 SkString s;
11 s.appendf("%s_%s", a, b);
12 return s;
13}
14
mtkleine4d3e602014-06-06 09:28:43 -070015SkString 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] = '_'; }
19 }
20 return filename;
21}
22
mtklein73734562014-06-24 12:28:34 -070023SkPicture* RecordPicture(skiagm::GM* gm, SkBBHFactory* factory, bool skr) {
robertphillipsa8d7f0b2014-08-29 08:03:56 -070024 const SkScalar w = SkIntToScalar(gm->getISize().width()),
25 h = SkIntToScalar(gm->getISize().height());
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000026 SkPictureRecorder recorder;
mtklein73734562014-06-24 12:28:34 -070027
28 SkCanvas* canvas = skr ? recorder.EXPERIMENTAL_beginRecording(w, h, factory)
mtkleinc92e5502014-08-21 13:07:27 -070029 : recorder. DEPRECATED_beginRecording(w, h, factory);
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000030 canvas->concat(gm->getInitialTransform());
31 gm->draw(canvas);
32 canvas->flush();
robertphillips@google.com84b18c72014-04-13 19:09:42 +000033 return recorder.endRecording();
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000034}
35
commit-bot@chromium.org26642072014-05-15 17:33:31 +000036void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000037 bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType));
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000038 bitmap->eraseColor(0x00000000);
39}
40
commit-bot@chromium.org26642072014-05-15 17:33:31 +000041void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
42 AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
commit-bot@chromium.org90b5a2a2014-05-14 17:55:32 +000043}
44
mtklein73734562014-06-24 12:28:34 -070045void DrawPicture(const SkPicture& picture, SkBitmap* bitmap) {
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000046 SkASSERT(bitmap != NULL);
47 SkCanvas canvas(*bitmap);
mtklein73734562014-06-24 12:28:34 -070048 canvas.drawPicture(&picture);
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000049 canvas.flush();
50}
51
commit-bot@chromium.org69031a42014-05-16 13:03:46 +000052static void unpack_565(uint16_t pixel, unsigned* r, unsigned* g, unsigned* b) {
53 *r = SkGetPackedR16(pixel);
54 *g = SkGetPackedG16(pixel);
55 *b = SkGetPackedB16(pixel);
56}
57
58// Returns |a-b|.
59static unsigned abs_diff(unsigned a, unsigned b) {
60 return a > b ? a - b : b - a;
61}
62
63unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) {
64 if (a.info() != b.info()) {
65 SkFAIL("Can't compare bitmaps of different shapes.");
66 }
67
68 unsigned max = 0;
69
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000070 const SkAutoLockPixels lockA(a), lockB(b);
commit-bot@chromium.org69031a42014-05-16 13:03:46 +000071 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++) {
76 unsigned ar, ag, ab,
77 br, bg, bb;
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));
83 }
84 } else {
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]));
90 }
91 }
92
93 return max;
94}
95
96bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
mtklein9c4ff802014-06-03 13:56:54 -070097 if (a.info() != b.info()) {
98 return false;
99 }
100 const SkAutoLockPixels lockA(a), lockB(b);
101 return 0 == memcmp(a.getPixels(), b.getPixels(), a.getSize());
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +0000102}
103
mtklein@google.comd36522d2013-10-16 13:02:15 +0000104} // namespace DM