1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkAutoPixmapStorage.h"
#include "SkImage.h"
#include "SkPath.h"
#include "SkSurface.h"
namespace skiagm {
static void draw_diff(SkCanvas* canvas, SkImage* imgA, SkImage* imgB) {
SkASSERT(imgA->dimensions() == imgB->dimensions());
int w = imgA->width(), h = imgA->height();
// First, draw the two images faintly overlaid
SkPaint paint;
paint.setAlpha(64);
paint.setBlendMode(SkBlendMode::kPlus);
canvas->drawImage(imgA, 0, 0, &paint);
canvas->drawImage(imgB, 0, 0, &paint);
// Next, read the pixels back, figure out if there are any differences
SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
SkAutoPixmapStorage pmapA;
SkAutoPixmapStorage pmapB;
pmapA.alloc(info);
pmapB.alloc(info);
if (!imgA->readPixels(pmapA, 0, 0) || !imgB->readPixels(pmapB, 0, 0)) {
return;
}
int maxDiffX = 0, maxDiffY = 0, maxDiff = 0;
SkBitmap highlight;
highlight.allocN32Pixels(w, h);
highlight.eraseColor(SK_ColorTRANSPARENT);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
uint32_t pixelA = *pmapA.addr32(x, y);
uint32_t pixelB = *pmapB.addr32(x, y);
if (pixelA != pixelB) {
int diff =
SkTAbs((int)(SkColorGetR(pixelA) - SkColorGetR(pixelB))) +
SkTAbs((int)(SkColorGetG(pixelA) - SkColorGetG(pixelB))) +
SkTAbs((int)(SkColorGetB(pixelA) - SkColorGetB(pixelB))) +
SkTAbs((int)(SkColorGetA(pixelA) - SkColorGetA(pixelB)));
if (diff > maxDiff) {
maxDiffX = x;
maxDiffY = y;
maxDiff = diff;
}
*highlight.getAddr32(x, y) = SkPackARGB32(0xA0, 0xA0, 0x00, 0x00);
}
}
}
SkPaint outline;
outline.setStyle(SkPaint::kStroke_Style);
outline.setColor(maxDiff == 0 ? 0xFF007F00 : 0xFF7F0000);
if (maxDiff > 0) {
// Call extra attention to the region we're going to zoom
SkPMColor yellow = SkPackARGB32(0xFF, 0xFF, 0xFF, 0x00);
*highlight.getAddr32(maxDiffX, maxDiffY) = yellow;
*highlight.getAddr32(SkTMax(maxDiffX - 1, 0), maxDiffY) = yellow;
*highlight.getAddr32(maxDiffX, SkTMax(maxDiffY - 1, 0)) = yellow;
*highlight.getAddr32(SkTMin(maxDiffX + 1, w - 1), maxDiffY) = yellow;
*highlight.getAddr32(maxDiffX, SkTMin(maxDiffY + 1, h - 1)) = yellow;
// Draw the overlay
canvas->drawBitmap(highlight, 0, 0);
// Draw zoom of largest pixel diff
SkBitmap bmpA, bmpB;
SkAssertResult(bmpA.installPixels(pmapA));
SkAssertResult(bmpB.installPixels(pmapB));
canvas->drawBitmapRect(bmpA, SkRect::MakeXYWH(maxDiffX - 5, maxDiffY - 5, 10, 10),
SkRect::MakeXYWH(w, 0, w, h), nullptr);
canvas->drawBitmapRect(bmpB, SkRect::MakeXYWH(maxDiffX - 5, maxDiffY - 5, 10, 10),
SkRect::MakeXYWH(2 * w, 0, w, h), nullptr);
// Add lines to separate zoom boxes
canvas->drawLine(w, 0, w, h, outline);
canvas->drawLine(2 * w, 0, 2 * w, h, outline);
}
// Draw outline of whole test region
canvas->drawRect(SkRect::MakeWH(3 * w, h), outline);
}
namespace {
typedef std::function<void(SkCanvas*, const SkRect&, const SkPaint&)> ShapeDrawFunc;
}
/**
* Iterates over a variety of rect shapes, paint parameters, and matrices, calling two different
* user-supplied draw callbacks. Produces a grid clearly showing if the two callbacks produce the
* same visual results in all cases.
*/
static void draw_rect_geom_diff_grid(SkCanvas* canvas, ShapeDrawFunc f1, ShapeDrawFunc f2) {
// Variables:
// - Fill, hairline, wide stroke
// - Axis aligned, rotated, scaled, scaled negative, perspective
// - Source geometry (normal, collapsed, inverted)
//
// Things not (yet?) tested:
// - AntiAlias on/off
// - StrokeAndFill
// - Cap/join
// - Anything even more elaborate...
const SkRect kRects[] = {
SkRect::MakeXYWH(10, 10, 30, 30), // Normal
SkRect::MakeXYWH(10, 25, 30, 0), // Collapsed
SkRect::MakeXYWH(10, 40, 30, -30), // Inverted
};
const struct { SkPaint::Style fStyle; SkScalar fStrokeWidth; } kStyles[] = {
{ SkPaint::kFill_Style, 0 }, // Filled
{ SkPaint::kStroke_Style, 0 }, // Hairline
{ SkPaint::kStroke_Style, 5 }, // Wide stroke
};
SkMatrix mI = SkMatrix::I();
SkMatrix mRot;
mRot.setRotate(30, 25, 25);
SkMatrix mScale;
mScale.setScaleTranslate(0.5f, 1, 12.5f, 0);
SkMatrix mFlipX;
mFlipX.setScaleTranslate(-1, 1, 50, 0);
SkMatrix mFlipY;
mFlipY.setScaleTranslate(1, -1, 0, 50);
SkMatrix mFlipXY;
mFlipXY.setScaleTranslate(-1, -1, 50, 50);
SkMatrix mPersp;
mPersp.setIdentity();
mPersp.setPerspY(0.002f);
const SkMatrix* kMatrices[] = { &mI, &mRot, &mScale, &mFlipX, &mFlipY, &mFlipXY, &mPersp, };
canvas->translate(10, 10);
SkImageInfo info = canvas->imageInfo().makeWH(50, 50);
auto surface = canvas->makeSurface(info);
if (!surface) {
surface = SkSurface::MakeRasterN32Premul(50, 50);
}
for (const SkRect& rect : kRects) {
for (const auto& style : kStyles) {
canvas->save();
for (const SkMatrix* mat : kMatrices) {
SkPaint paint;
paint.setColor(SK_ColorWHITE);
paint.setAntiAlias(true);
paint.setStyle(style.fStyle);
paint.setStrokeWidth(style.fStrokeWidth);
// Do first draw
surface->getCanvas()->clear(SK_ColorBLACK);
surface->getCanvas()->save();
surface->getCanvas()->concat(*mat);
f1(surface->getCanvas(), rect, paint);
surface->getCanvas()->restore();
auto imgA = surface->makeImageSnapshot();
// Do second draw
surface->getCanvas()->clear(SK_ColorBLACK);
surface->getCanvas()->save();
surface->getCanvas()->concat(*mat);
f2(surface->getCanvas(), rect, paint);
surface->getCanvas()->restore();
auto imgB = surface->makeImageSnapshot();
draw_diff(canvas, imgA.get(), imgB.get());
canvas->translate(160, 0);
}
canvas->restore();
canvas->translate(0, 60);
}
}
}
static const int kNumRows = 9;
static const int kNumColumns = 7;
static const int kTotalWidth = kNumColumns * 160 + 10;
static const int kTotalHeight = kNumRows * 60 + 10;
DEF_SIMPLE_GM_BG(rects_as_paths, canvas, kTotalWidth, kTotalHeight, SK_ColorBLACK) {
// Drawing a rect vs. adding it to a path and drawing the path, should produce same results.
auto rectDrawFunc = [](SkCanvas* canvas, const SkRect& rect, const SkPaint& paint) {
canvas->drawRect(rect, paint);
};
auto pathDrawFunc = [](SkCanvas* canvas, const SkRect& rect, const SkPaint& paint) {
SkPath path;
path.addRect(rect);
canvas->drawPath(path, paint);
};
draw_rect_geom_diff_grid(canvas, rectDrawFunc, pathDrawFunc);
}
DEF_SIMPLE_GM_BG(ovals_as_paths, canvas, kTotalWidth, kTotalHeight, SK_ColorBLACK) {
// Drawing an oval vs. adding it to a path and drawing the path, should produce same results.
auto ovalDrawFunc = [](SkCanvas* canvas, const SkRect& rect, const SkPaint& paint) {
canvas->drawOval(rect, paint);
};
auto pathDrawFunc = [](SkCanvas* canvas, const SkRect& rect, const SkPaint& paint) {
SkPath path;
path.addOval(rect);
canvas->drawPath(path, paint);
};
draw_rect_geom_diff_grid(canvas, ovalDrawFunc, pathDrawFunc);
}
DEF_SIMPLE_GM_BG(arcs_as_paths, canvas, kTotalWidth, kTotalHeight, SK_ColorBLACK) {
// Drawing an arc vs. adding it to a path and drawing the path, should produce same results.
auto arcDrawFunc = [](SkCanvas* canvas, const SkRect& rect, const SkPaint& paint) {
canvas->drawArc(rect, 10, 200, false, paint);
};
auto pathDrawFunc = [](SkCanvas* canvas, const SkRect& rect, const SkPaint& paint) {
SkPath path;
path.addArc(rect, 10, 200);
canvas->drawPath(path, paint);
};
draw_rect_geom_diff_grid(canvas, arcDrawFunc, pathDrawFunc);
}
}
|