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" |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 4 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 5 | #include "SkPictureRecorder.h" |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 6 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 7 | namespace DM { |
| 8 | |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 9 | SkString UnderJoin(const char* a, const char* b) { |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 10 | SkString s; |
| 11 | s.appendf("%s_%s", a, b); |
| 12 | return s; |
| 13 | } |
| 14 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 15 | SkPicture* RecordPicture(skiagm::GM* gm, uint32_t recordFlags, SkBBHFactory* factory) { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 16 | const SkISize size = gm->getISize(); |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 17 | SkPictureRecorder recorder; |
| 18 | SkCanvas* canvas = recorder.beginRecording(size.width(), size.height(), factory, recordFlags); |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 19 | canvas->concat(gm->getInitialTransform()); |
| 20 | gm->draw(canvas); |
| 21 | canvas->flush(); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 22 | return recorder.endRecording(); |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 23 | } |
| 24 | |
commit-bot@chromium.org | 2664207 | 2014-05-15 17:33:31 +0000 | [diff] [blame] | 25 | void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) { |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 26 | bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType)); |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 27 | bitmap->eraseColor(0x00000000); |
| 28 | } |
| 29 | |
commit-bot@chromium.org | 2664207 | 2014-05-15 17:33:31 +0000 | [diff] [blame] | 30 | void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) { |
| 31 | AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap); |
commit-bot@chromium.org | 90b5a2a | 2014-05-14 17:55:32 +0000 | [diff] [blame] | 32 | } |
| 33 | |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 34 | void 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.org | 69031a4 | 2014-05-16 13:03:46 +0000 | [diff] [blame] | 42 | static 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|. |
| 49 | static unsigned abs_diff(unsigned a, unsigned b) { |
| 50 | return a > b ? a - b : b - a; |
| 51 | } |
| 52 | |
| 53 | unsigned 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.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 60 | const SkAutoLockPixels lockA(a), lockB(b); |
commit-bot@chromium.org | 69031a4 | 2014-05-16 13:03:46 +0000 | [diff] [blame] | 61 | 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 | |
| 86 | bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) { |
| 87 | return a.info() == b.info() && 0 == MaxComponentDifference(a, b); |
commit-bot@chromium.org | 192cbf6 | 2013-10-21 18:40:25 +0000 | [diff] [blame] | 88 | } |
| 89 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 90 | } // namespace DM |