Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(349)

Side by Side Diff: dm/DMUtil.cpp

Issue 788243008: Sketch DM refactor. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: better failure messages, disable a few things temporarily to try to go green Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « dm/DMUtil.h ('k') | dm/DMWriteTask.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "DMUtil.h"
2
3 #include "SkColorPriv.h"
4 #include "SkCommandLineFlags.h"
5 #include "SkPicture.h"
6 #include "SkPictureRecorder.h"
7
8 DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1",
9 "Matrix to apply to the canvas before drawing.");
10
11 namespace DM {
12
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
23 SkString UnderJoin(const char* a, const char* b) {
24 SkString s;
25 s.appendf("%s_%s", a, b);
26 return s;
27 }
28
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
37 SkPicture* RecordPicture(skiagm::GM* gm, SkBBHFactory* factory) {
38 const SkScalar w = SkIntToScalar(gm->getISize().width()),
39 h = SkIntToScalar(gm->getISize().height());
40 SkPictureRecorder recorder;
41
42 SkCanvas* canvas = recorder.beginRecording(w, h, factory);
43 CanvasPreflight(canvas);
44 canvas->concat(gm->getInitialTransform());
45 gm->draw(canvas);
46 canvas->flush();
47 return recorder.endRecording();
48 }
49
50 void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
51 bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType ));
52 bitmap->eraseColor(0x00000000);
53 }
54
55 void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
56 AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
57 }
58
59 void DrawPicture(const SkPicture& picture, SkBitmap* bitmap) {
60 SkASSERT(bitmap != NULL);
61 SkCanvas canvas(*bitmap);
62 canvas.drawPicture(&picture);
63 canvas.flush();
64 }
65
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
84 const SkAutoLockPixels lockA(a), lockB(b);
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) {
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());
116 }
117
118 } // namespace DM
OLDNEW
« no previous file with comments | « dm/DMUtil.h ('k') | dm/DMWriteTask.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698