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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
#include "SkAAClip.h"
#include "SkCanvas.h"
#include "SkMask.h"
#include "SkPath.h"
#include "SkRandom.h"
static bool operator==(const SkMask& a, const SkMask& b) {
if (a.fFormat != b.fFormat || a.fBounds != b.fBounds) {
return false;
}
if (!a.fImage && !b.fImage) {
return true;
}
if (!a.fImage || !b.fImage) {
return false;
}
size_t wbytes = a.fBounds.width();
switch (a.fFormat) {
case SkMask::kBW_Format:
wbytes = (wbytes + 7) >> 3;
break;
case SkMask::kA8_Format:
case SkMask::k3D_Format:
break;
case SkMask::kLCD16_Format:
wbytes <<= 1;
break;
case SkMask::kLCD32_Format:
case SkMask::kARGB32_Format:
wbytes <<= 2;
break;
default:
SkASSERT(!"unknown mask format");
return false;
}
const int h = a.fBounds.height();
const char* aptr = (const char*)a.fImage;
const char* bptr = (const char*)b.fImage;
for (int y = 0; y < h; ++y) {
if (memcmp(aptr, bptr, wbytes)) {
return false;
}
aptr += wbytes;
bptr += wbytes;
}
return true;
}
static void copyToMask(const SkRegion& rgn, SkMask* mask) {
mask->fFormat = SkMask::kA8_Format;
if (rgn.isEmpty()) {
mask->fBounds.setEmpty();
mask->fRowBytes = 0;
mask->fImage = NULL;
return;
}
mask->fBounds = rgn.getBounds();
mask->fRowBytes = mask->fBounds.width();
mask->fImage = SkMask::AllocImage(mask->computeImageSize());
sk_bzero(mask->fImage, mask->computeImageSize());
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kA8_Config, mask->fBounds.width(),
mask->fBounds.height(), mask->fRowBytes);
bitmap.setPixels(mask->fImage);
// canvas expects its coordinate system to always be 0,0 in the top/left
// so we translate the rgn to match that before drawing into the mask.
//
SkRegion tmpRgn(rgn);
tmpRgn.translate(-rgn.getBounds().fLeft, -rgn.getBounds().fTop);
SkCanvas canvas(bitmap);
canvas.clipRegion(tmpRgn);
canvas.drawColor(SK_ColorBLACK);
}
static SkIRect rand_rect(SkRandom& rand, int n) {
int x = rand.nextS() % n;
int y = rand.nextS() % n;
int w = rand.nextU() % n;
int h = rand.nextU() % n;
return SkIRect::MakeXYWH(x, y, w, h);
}
static void make_rand_rgn(SkRegion* rgn, SkRandom& rand) {
int count = rand.nextU() % 20;
for (int i = 0; i < count; ++i) {
rgn->op(rand_rect(rand, 100), SkRegion::kXOR_Op);
}
}
static bool operator==(const SkRegion& rgn, const SkAAClip& aaclip) {
SkMask mask0, mask1;
copyToMask(rgn, &mask0);
aaclip.copyToMask(&mask1);
bool eq = (mask0 == mask1);
SkMask::FreeImage(mask0.fImage);
SkMask::FreeImage(mask1.fImage);
return eq;
}
static bool equalsAAClip(const SkRegion& rgn) {
SkAAClip aaclip;
aaclip.setRegion(rgn);
return rgn == aaclip;
}
static void setRgnToPath(SkRegion* rgn, const SkPath& path) {
SkIRect ir;
path.getBounds().round(&ir);
rgn->setPath(path, SkRegion(ir));
}
// aaclip.setRegion should create idential masks to the region
static void test_rgn(skiatest::Reporter* reporter) {
SkRandom rand;
for (int i = 0; i < 1000; i++) {
SkRegion rgn;
make_rand_rgn(&rgn, rand);
REPORTER_ASSERT(reporter, equalsAAClip(rgn));
}
{
SkRegion rgn;
SkPath path;
path.addCircle(0, 0, SkIntToScalar(30));
setRgnToPath(&rgn, path);
REPORTER_ASSERT(reporter, equalsAAClip(rgn));
path.reset();
path.moveTo(0, 0);
path.lineTo(SkIntToScalar(100), 0);
path.lineTo(SkIntToScalar(100 - 20), SkIntToScalar(20));
path.lineTo(SkIntToScalar(20), SkIntToScalar(20));
setRgnToPath(&rgn, path);
REPORTER_ASSERT(reporter, equalsAAClip(rgn));
}
}
static const SkRegion::Op gRgnOps[] = {
SkRegion::kDifference_Op,
SkRegion::kIntersect_Op,
SkRegion::kUnion_Op,
SkRegion::kXOR_Op,
SkRegion::kReverseDifference_Op,
SkRegion::kReplace_Op
};
static const char* gRgnOpNames[] = {
"DIFF", "INTERSECT", "UNION", "XOR", "REVERSE_DIFF", "REPLACE"
};
static void imoveTo(SkPath& path, int x, int y) {
path.moveTo(SkIntToScalar(x), SkIntToScalar(y));
}
static void icubicTo(SkPath& path, int x0, int y0, int x1, int y1, int x2, int y2) {
path.cubicTo(SkIntToScalar(x0), SkIntToScalar(y0),
SkIntToScalar(x1), SkIntToScalar(y1),
SkIntToScalar(x2), SkIntToScalar(y2));
}
static void test_path_bounds(skiatest::Reporter* reporter) {
SkPath path;
SkAAClip clip;
const int height = 40;
const SkScalar sheight = SkIntToScalar(height);
path.addOval(SkRect::MakeWH(sheight, sheight));
REPORTER_ASSERT(reporter, sheight == path.getBounds().height());
clip.setPath(path, NULL, true);
REPORTER_ASSERT(reporter, height == clip.getBounds().height());
// this is the trimmed height of this cubic (with aa). The critical thing
// for this test is that it is less than height, which represents just
// the bounds of the path's control-points.
//
// This used to fail until we tracked the MinY in the BuilderBlitter.
//
const int teardrop_height = 12;
path.reset();
imoveTo(path, 0, 20);
icubicTo(path, 40, 40, 40, 0, 0, 20);
REPORTER_ASSERT(reporter, sheight == path.getBounds().height());
clip.setPath(path, NULL, true);
REPORTER_ASSERT(reporter, teardrop_height == clip.getBounds().height());
}
static void test_empty(skiatest::Reporter* reporter) {
SkAAClip clip0, clip1;
REPORTER_ASSERT(reporter, clip0.isEmpty());
REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty());
REPORTER_ASSERT(reporter, clip1 == clip0);
clip0.translate(10, 10); // should have no effect on empty
REPORTER_ASSERT(reporter, clip0.isEmpty());
REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty());
REPORTER_ASSERT(reporter, clip1 == clip0);
SkIRect r = { 10, 10, 40, 50 };
clip0.setRect(r);
REPORTER_ASSERT(reporter, !clip0.isEmpty());
REPORTER_ASSERT(reporter, !clip0.getBounds().isEmpty());
REPORTER_ASSERT(reporter, clip0 != clip1);
REPORTER_ASSERT(reporter, clip0.getBounds() == r);
clip0.setEmpty();
REPORTER_ASSERT(reporter, clip0.isEmpty());
REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty());
REPORTER_ASSERT(reporter, clip1 == clip0);
SkMask mask;
mask.fImage = NULL;
clip0.copyToMask(&mask);
REPORTER_ASSERT(reporter, NULL == mask.fImage);
REPORTER_ASSERT(reporter, mask.fBounds.isEmpty());
}
static void rand_irect(SkIRect* r, int N, SkRandom& rand) {
r->setXYWH(0, 0, rand.nextU() % N, rand.nextU() % N);
int dx = rand.nextU() % (2*N);
int dy = rand.nextU() % (2*N);
// use int dx,dy to make the subtract be signed
r->offset(N - dx, N - dy);
}
static void test_irect(skiatest::Reporter* reporter) {
SkRandom rand;
for (int i = 0; i < 10000; i++) {
SkAAClip clip0, clip1;
SkRegion rgn0, rgn1;
SkIRect r0, r1;
rand_irect(&r0, 10, rand);
rand_irect(&r1, 10, rand);
clip0.setRect(r0);
clip1.setRect(r1);
rgn0.setRect(r0);
rgn1.setRect(r1);
for (size_t j = 0; j < SK_ARRAY_COUNT(gRgnOps); ++j) {
SkRegion::Op op = gRgnOps[j];
SkAAClip clip2;
SkRegion rgn2;
bool nonEmptyAA = clip2.op(clip0, clip1, op);
bool nonEmptyBW = rgn2.op(rgn0, rgn1, op);
if (nonEmptyAA != nonEmptyBW || clip2.getBounds() != rgn2.getBounds()) {
SkDebugf("[%d %d %d %d] %s [%d %d %d %d] = BW:[%d %d %d %d] AA:[%d %d %d %d]\n",
r0.fLeft, r0.fTop, r0.right(), r0.bottom(),
gRgnOpNames[j],
r1.fLeft, r1.fTop, r1.right(), r1.bottom(),
rgn2.getBounds().fLeft, rgn2.getBounds().fTop,
rgn2.getBounds().right(), rgn2.getBounds().bottom(),
clip2.getBounds().fLeft, clip2.getBounds().fTop,
clip2.getBounds().right(), clip2.getBounds().bottom());
}
REPORTER_ASSERT(reporter, nonEmptyAA == nonEmptyBW);
REPORTER_ASSERT(reporter, clip2.getBounds() == rgn2.getBounds());
SkMask maskBW, maskAA;
copyToMask(rgn2, &maskBW);
clip2.copyToMask(&maskAA);
SkAutoMaskFreeImage freeBW(maskBW.fImage);
SkAutoMaskFreeImage freeAA(maskAA.fImage);
REPORTER_ASSERT(reporter, maskBW == maskAA);
}
}
}
static void test_path_with_hole(skiatest::Reporter* reporter) {
static const uint8_t gExpectedImage[] = {
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
};
SkMask expected;
expected.fBounds.set(0, 0, 4, 6);
expected.fRowBytes = 4;
expected.fFormat = SkMask::kA8_Format;
expected.fImage = (uint8_t*)gExpectedImage;
SkPath path;
path.addRect(SkRect::MakeXYWH(0, 0,
SkIntToScalar(4), SkIntToScalar(2)));
path.addRect(SkRect::MakeXYWH(0, SkIntToScalar(4),
SkIntToScalar(4), SkIntToScalar(2)));
for (int i = 0; i < 2; ++i) {
SkAAClip clip;
clip.setPath(path, NULL, 1 == i);
SkMask mask;
clip.copyToMask(&mask);
SkAutoMaskFreeImage freeM(mask.fImage);
REPORTER_ASSERT(reporter, expected == mask);
}
}
#include "SkRasterClip.h"
static void copyToMask(const SkRasterClip& rc, SkMask* mask) {
if (rc.isAA()) {
rc.aaRgn().copyToMask(mask);
} else {
copyToMask(rc.bwRgn(), mask);
}
}
static bool operator==(const SkRasterClip& a, const SkRasterClip& b) {
if (a.isEmpty()) {
return b.isEmpty();
}
if (b.isEmpty()) {
return false;
}
SkMask ma, mb;
copyToMask(a, &ma);
copyToMask(b, &mb);
SkAutoMaskFreeImage aCleanUp(ma.fImage);
SkAutoMaskFreeImage bCleanUp(mb.fImage);
return ma == mb;
}
static void did_dx_affect(skiatest::Reporter* reporter, const SkScalar dx[],
size_t count, bool changed) {
SkIRect ir = { 0, 0, 10, 10 };
for (size_t i = 0; i < count; ++i) {
SkRect r;
r.set(ir);
SkRasterClip rc0(ir);
SkRasterClip rc1(ir);
SkRasterClip rc2(ir);
rc0.op(r, SkRegion::kIntersect_Op, false);
r.offset(dx[i], 0);
rc1.op(r, SkRegion::kIntersect_Op, true);
r.offset(-2*dx[i], 0);
rc2.op(r, SkRegion::kIntersect_Op, true);
REPORTER_ASSERT(reporter, changed != (rc0 == rc1));
REPORTER_ASSERT(reporter, changed != (rc0 == rc2));
}
}
static void test_nearly_integral(skiatest::Reporter* reporter) {
// All of these should generate equivalent rasterclips
static const SkScalar gSafeX[] = {
0, SK_Scalar1/1000, SK_Scalar1/100, SK_Scalar1/10,
};
did_dx_affect(reporter, gSafeX, SK_ARRAY_COUNT(gSafeX), false);
static const SkScalar gUnsafeX[] = {
SK_Scalar1/4, SK_Scalar1/3,
};
did_dx_affect(reporter, gUnsafeX, SK_ARRAY_COUNT(gUnsafeX), true);
}
static void test_regressions(skiatest::Reporter* reporter) {
// these should not assert in the debug build
// bug was introduced in rev. 3209
{
SkAAClip clip;
SkRect r;
r.fLeft = SkFloatToScalar(129.892181f);
r.fTop = SkFloatToScalar(10.3999996f);
r.fRight = SkFloatToScalar(130.892181f);
r.fBottom = SkFloatToScalar(20.3999996f);
clip.setRect(r, true);
}
}
static void TestAAClip(skiatest::Reporter* reporter) {
test_empty(reporter);
test_path_bounds(reporter);
test_irect(reporter);
test_rgn(reporter);
test_path_with_hole(reporter);
test_regressions(reporter);
test_nearly_integral(reporter);
}
#include "TestClassDef.h"
DEFINE_TESTCLASS("AAClip", AAClipTestClass, TestAAClip)
|