blob: d7d6691550b2113b4991d157c68d9155f8a7e453 [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
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000015SkPicture* RecordPicture(skiagm::GM* gm, uint32_t recordFlags, SkBBHFactory* factory) {
rmistry@google.comd6bab022013-12-02 13:50:38 +000016 const SkISize size = gm->getISize();
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000017 SkPictureRecorder recorder;
18 SkCanvas* canvas = recorder.beginRecording(size.width(), size.height(), factory, recordFlags);
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000019 canvas->concat(gm->getInitialTransform());
20 gm->draw(canvas);
21 canvas->flush();
robertphillips@google.com84b18c72014-04-13 19:09:42 +000022 return recorder.endRecording();
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000023}
24
commit-bot@chromium.org26642072014-05-15 17:33:31 +000025void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000026 bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType));
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000027 bitmap->eraseColor(0x00000000);
28}
29
commit-bot@chromium.org26642072014-05-15 17:33:31 +000030void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
31 AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
commit-bot@chromium.org90b5a2a2014-05-14 17:55:32 +000032}
33
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000034void DrawPicture(SkPicture* picture, SkBitmap* bitmap) {
35 SkASSERT(picture != NULL);
36 SkASSERT(bitmap != NULL);
37 SkCanvas canvas(*bitmap);
38 canvas.drawPicture(*picture);
39 canvas.flush();
40}
41
commit-bot@chromium.org69031a42014-05-16 13:03:46 +000042static void unpack_565(uint16_t pixel, unsigned* r, unsigned* g, unsigned* b) {
43 *r = SkGetPackedR16(pixel);
44 *g = SkGetPackedG16(pixel);
45 *b = SkGetPackedB16(pixel);
46}
47
48// Returns |a-b|.
49static unsigned abs_diff(unsigned a, unsigned b) {
50 return a > b ? a - b : b - a;
51}
52
53unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) {
54 if (a.info() != b.info()) {
55 SkFAIL("Can't compare bitmaps of different shapes.");
56 }
57
58 unsigned max = 0;
59
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000060 const SkAutoLockPixels lockA(a), lockB(b);
commit-bot@chromium.org69031a42014-05-16 13:03:46 +000061 if (a.info().colorType() == kRGB_565_SkColorType) {
62 // 565 is special/annoying because its 3 components straddle 2 bytes.
63 const uint16_t* aPixels = (const uint16_t*)a.getPixels();
64 const uint16_t* bPixels = (const uint16_t*)b.getPixels();
65 for (size_t i = 0; i < a.getSize() / 2; i++) {
66 unsigned ar, ag, ab,
67 br, bg, bb;
68 unpack_565(aPixels[i], &ar, &ag, &ab);
69 unpack_565(bPixels[i], &br, &bg, &bb);
70 max = SkTMax(max, abs_diff(ar, br));
71 max = SkTMax(max, abs_diff(ag, bg));
72 max = SkTMax(max, abs_diff(ab, bb));
73 }
74 } else {
75 // Everything else we produce is byte aligned, so max component diff == max byte diff.
76 const uint8_t* aBytes = (const uint8_t*)a.getPixels();
77 const uint8_t* bBytes = (const uint8_t*)b.getPixels();
78 for (size_t i = 0; i < a.getSize(); i++) {
79 max = SkTMax(max, abs_diff(aBytes[i], bBytes[i]));
80 }
81 }
82
83 return max;
84}
85
86bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
87 return a.info() == b.info() && 0 == MaxComponentDifference(a, b);
commit-bot@chromium.org192cbf62013-10-21 18:40:25 +000088}
89
mtklein@google.comd36522d2013-10-16 13:02:15 +000090} // namespace DM