mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 1 | #include "DMUtil.h" |
| 2 | |
commit-bot@chromium.org | 69031a4 | 2014-05-16 13:03:46 +0000 | [diff] [blame] | 3 | #include "SkColorPriv.h" |
mtklein | 0b36e6b | 2014-09-11 12:30:12 -0700 | [diff] [blame] | 4 | #include "SkCommandLineFlags.h" |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 5 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 6 | #include "SkPictureRecorder.h" |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 7 | |
mtklein | 0b36e6b | 2014-09-11 12:30:12 -0700 | [diff] [blame] | 8 | DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1", |
| 9 | "Matrix to apply to the canvas before drawing."); |
| 10 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 11 | namespace DM { |
| 12 | |
mtklein | 0b36e6b | 2014-09-11 12:30:12 -0700 | [diff] [blame] | 13 | void 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.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 23 | SkString UnderJoin(const char* a, const char* b) { |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 24 | SkString s; |
| 25 | s.appendf("%s_%s", a, b); |
| 26 | return s; |
| 27 | } |
| 28 | |
mtklein | e4d3e60 | 2014-06-06 09:28:43 -0700 | [diff] [blame] | 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] = '_'; } |
| 33 | } |
| 34 | return filename; |
| 35 | } |
| 36 | |
mtklein | a9ceaf5 | 2014-09-29 08:44:46 -0700 | [diff] [blame] | 37 | SkPicture* RecordPicture(skiagm::GM* gm, SkBBHFactory* factory) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 38 | const SkScalar w = SkIntToScalar(gm->getISize().width()), |
| 39 | h = SkIntToScalar(gm->getISize().height()); |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 40 | SkPictureRecorder recorder; |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 41 | |
mtklein | a9ceaf5 | 2014-09-29 08:44:46 -0700 | [diff] [blame] | 42 | SkCanvas* canvas = recorder.beginRecording(w, h, factory); |
mtklein | 0b36e6b | 2014-09-11 12:30:12 -0700 | [diff] [blame] | 43 | CanvasPreflight(canvas); |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 44 | canvas->concat(gm->getInitialTransform()); |
| 45 | gm->draw(canvas); |
| 46 | canvas->flush(); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 47 | return recorder.endRecording(); |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 48 | } |
| 49 | |
commit-bot@chromium.org | 2664207 | 2014-05-15 17:33:31 +0000 | [diff] [blame] | 50 | void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) { |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 51 | bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType)); |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 52 | bitmap->eraseColor(0x00000000); |
| 53 | } |
| 54 | |
commit-bot@chromium.org | 2664207 | 2014-05-15 17:33:31 +0000 | [diff] [blame] | 55 | void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) { |
| 56 | AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap); |
commit-bot@chromium.org | 90b5a2a | 2014-05-14 17:55:32 +0000 | [diff] [blame] | 57 | } |
| 58 | |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 59 | void DrawPicture(const SkPicture& picture, SkBitmap* bitmap) { |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 60 | SkASSERT(bitmap != NULL); |
| 61 | SkCanvas canvas(*bitmap); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 62 | canvas.drawPicture(&picture); |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 63 | canvas.flush(); |
| 64 | } |
| 65 | |
commit-bot@chromium.org | 69031a4 | 2014-05-16 13:03:46 +0000 | [diff] [blame] | 66 | static 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|. |
| 73 | static unsigned abs_diff(unsigned a, unsigned b) { |
| 74 | return a > b ? a - b : b - a; |
| 75 | } |
| 76 | |
| 77 | unsigned 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.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 84 | const SkAutoLockPixels lockA(a), lockB(b); |
commit-bot@chromium.org | 69031a4 | 2014-05-16 13:03:46 +0000 | [diff] [blame] | 85 | 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 | |
| 110 | bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) { |
mtklein | 9c4ff80 | 2014-06-03 13:56:54 -0700 | [diff] [blame] | 111 | 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.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 116 | } |
| 117 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 118 | } // namespace DM |