Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / dm / DMUtil.cpp
1 #include "DMUtil.h"
2
3 #include "SkColorPriv.h"
4 #include "SkPicture.h"
5 #include "SkPictureRecorder.h"
6
7 namespace DM {
8
9 SkString UnderJoin(const char* a, const char* b) {
10     SkString s;
11     s.appendf("%s_%s", a, b);
12     return s;
13 }
14
15 SkString 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
23 SkPicture* RecordPicture(skiagm::GM* gm, SkBBHFactory* factory, bool skr) {
24     const int w = gm->getISize().width(),
25               h = gm->getISize().height();
26     SkPictureRecorder recorder;
27
28     SkCanvas* canvas = skr ? recorder.EXPERIMENTAL_beginRecording(w, h, factory)
29                            : recorder.             beginRecording(w, h, factory);
30     canvas->concat(gm->getInitialTransform());
31     gm->draw(canvas);
32     canvas->flush();
33     return recorder.endRecording();
34 }
35
36 void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
37     bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType));
38     bitmap->eraseColor(0x00000000);
39 }
40
41 void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
42     AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
43 }
44
45 void DrawPicture(const SkPicture& picture, SkBitmap* bitmap) {
46     SkASSERT(bitmap != NULL);
47     SkCanvas canvas(*bitmap);
48     canvas.drawPicture(&picture);
49     canvas.flush();
50 }
51
52 static 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|.
59 static unsigned abs_diff(unsigned a, unsigned b) {
60     return a > b ? a - b : b - a;
61 }
62
63 unsigned 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
70     const SkAutoLockPixels lockA(a), lockB(b);
71     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
96 bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
97     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());
102 }
103
104 }  // namespace DM