AAClipTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkImageInfo.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/core/SkRRect.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkRegion.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkTypes.h"
  18. #include "include/private/SkMalloc.h"
  19. #include "include/utils/SkRandom.h"
  20. #include "src/core/SkAAClip.h"
  21. #include "src/core/SkMask.h"
  22. #include "src/core/SkRasterClip.h"
  23. #include "tests/Test.h"
  24. #include <string.h>
  25. static bool operator==(const SkMask& a, const SkMask& b) {
  26. if (a.fFormat != b.fFormat || a.fBounds != b.fBounds) {
  27. return false;
  28. }
  29. if (!a.fImage && !b.fImage) {
  30. return true;
  31. }
  32. if (!a.fImage || !b.fImage) {
  33. return false;
  34. }
  35. size_t wbytes = a.fBounds.width();
  36. switch (a.fFormat) {
  37. case SkMask::kBW_Format:
  38. wbytes = (wbytes + 7) >> 3;
  39. break;
  40. case SkMask::kA8_Format:
  41. case SkMask::k3D_Format:
  42. break;
  43. case SkMask::kLCD16_Format:
  44. wbytes <<= 1;
  45. break;
  46. case SkMask::kARGB32_Format:
  47. wbytes <<= 2;
  48. break;
  49. default:
  50. SkDEBUGFAIL("unknown mask format");
  51. return false;
  52. }
  53. const int h = a.fBounds.height();
  54. const char* aptr = (const char*)a.fImage;
  55. const char* bptr = (const char*)b.fImage;
  56. for (int y = 0; y < h; ++y) {
  57. if (memcmp(aptr, bptr, wbytes)) {
  58. return false;
  59. }
  60. aptr += wbytes;
  61. bptr += wbytes;
  62. }
  63. return true;
  64. }
  65. static void copyToMask(const SkRegion& rgn, SkMask* mask) {
  66. mask->fFormat = SkMask::kA8_Format;
  67. if (rgn.isEmpty()) {
  68. mask->fBounds.setEmpty();
  69. mask->fRowBytes = 0;
  70. mask->fImage = nullptr;
  71. return;
  72. }
  73. mask->fBounds = rgn.getBounds();
  74. mask->fRowBytes = mask->fBounds.width();
  75. mask->fImage = SkMask::AllocImage(mask->computeImageSize());
  76. sk_bzero(mask->fImage, mask->computeImageSize());
  77. SkImageInfo info = SkImageInfo::Make(mask->fBounds.width(),
  78. mask->fBounds.height(),
  79. kAlpha_8_SkColorType,
  80. kPremul_SkAlphaType);
  81. SkBitmap bitmap;
  82. bitmap.installPixels(info, mask->fImage, mask->fRowBytes);
  83. // canvas expects its coordinate system to always be 0,0 in the top/left
  84. // so we translate the rgn to match that before drawing into the mask.
  85. //
  86. SkRegion tmpRgn(rgn);
  87. tmpRgn.translate(-rgn.getBounds().fLeft, -rgn.getBounds().fTop);
  88. SkCanvas canvas(bitmap);
  89. canvas.clipRegion(tmpRgn);
  90. canvas.drawColor(SK_ColorBLACK);
  91. }
  92. static SkIRect rand_rect(SkRandom& rand, int n) {
  93. int x = rand.nextS() % n;
  94. int y = rand.nextS() % n;
  95. int w = rand.nextU() % n;
  96. int h = rand.nextU() % n;
  97. return SkIRect::MakeXYWH(x, y, w, h);
  98. }
  99. static void make_rand_rgn(SkRegion* rgn, SkRandom& rand) {
  100. int count = rand.nextU() % 20;
  101. for (int i = 0; i < count; ++i) {
  102. rgn->op(rand_rect(rand, 100), SkRegion::kXOR_Op);
  103. }
  104. }
  105. static bool operator==(const SkRegion& rgn, const SkAAClip& aaclip) {
  106. SkMask mask0, mask1;
  107. copyToMask(rgn, &mask0);
  108. aaclip.copyToMask(&mask1);
  109. bool eq = (mask0 == mask1);
  110. SkMask::FreeImage(mask0.fImage);
  111. SkMask::FreeImage(mask1.fImage);
  112. return eq;
  113. }
  114. static bool equalsAAClip(const SkRegion& rgn) {
  115. SkAAClip aaclip;
  116. aaclip.setRegion(rgn);
  117. return rgn == aaclip;
  118. }
  119. static void setRgnToPath(SkRegion* rgn, const SkPath& path) {
  120. SkIRect ir;
  121. path.getBounds().round(&ir);
  122. rgn->setPath(path, SkRegion(ir));
  123. }
  124. // aaclip.setRegion should create idential masks to the region
  125. static void test_rgn(skiatest::Reporter* reporter) {
  126. SkRandom rand;
  127. for (int i = 0; i < 1000; i++) {
  128. SkRegion rgn;
  129. make_rand_rgn(&rgn, rand);
  130. REPORTER_ASSERT(reporter, equalsAAClip(rgn));
  131. }
  132. {
  133. SkRegion rgn;
  134. SkPath path;
  135. path.addCircle(0, 0, SkIntToScalar(30));
  136. setRgnToPath(&rgn, path);
  137. REPORTER_ASSERT(reporter, equalsAAClip(rgn));
  138. path.reset();
  139. path.moveTo(0, 0);
  140. path.lineTo(SkIntToScalar(100), 0);
  141. path.lineTo(SkIntToScalar(100 - 20), SkIntToScalar(20));
  142. path.lineTo(SkIntToScalar(20), SkIntToScalar(20));
  143. setRgnToPath(&rgn, path);
  144. REPORTER_ASSERT(reporter, equalsAAClip(rgn));
  145. }
  146. }
  147. static const SkRegion::Op gRgnOps[] = {
  148. SkRegion::kDifference_Op,
  149. SkRegion::kIntersect_Op,
  150. SkRegion::kUnion_Op,
  151. SkRegion::kXOR_Op,
  152. SkRegion::kReverseDifference_Op,
  153. SkRegion::kReplace_Op
  154. };
  155. static const char* gRgnOpNames[] = {
  156. "DIFF", "INTERSECT", "UNION", "XOR", "REVERSE_DIFF", "REPLACE"
  157. };
  158. static void imoveTo(SkPath& path, int x, int y) {
  159. path.moveTo(SkIntToScalar(x), SkIntToScalar(y));
  160. }
  161. static void icubicTo(SkPath& path, int x0, int y0, int x1, int y1, int x2, int y2) {
  162. path.cubicTo(SkIntToScalar(x0), SkIntToScalar(y0),
  163. SkIntToScalar(x1), SkIntToScalar(y1),
  164. SkIntToScalar(x2), SkIntToScalar(y2));
  165. }
  166. static void test_path_bounds(skiatest::Reporter* reporter) {
  167. SkPath path;
  168. SkAAClip clip;
  169. const int height = 40;
  170. const SkScalar sheight = SkIntToScalar(height);
  171. path.addOval(SkRect::MakeWH(sheight, sheight));
  172. REPORTER_ASSERT(reporter, sheight == path.getBounds().height());
  173. clip.setPath(path, nullptr, true);
  174. REPORTER_ASSERT(reporter, height == clip.getBounds().height());
  175. // this is the trimmed height of this cubic (with aa). The critical thing
  176. // for this test is that it is less than height, which represents just
  177. // the bounds of the path's control-points.
  178. //
  179. // This used to fail until we tracked the MinY in the BuilderBlitter.
  180. //
  181. const int teardrop_height = 12;
  182. path.reset();
  183. imoveTo(path, 0, 20);
  184. icubicTo(path, 40, 40, 40, 0, 0, 20);
  185. REPORTER_ASSERT(reporter, sheight == path.getBounds().height());
  186. clip.setPath(path, nullptr, true);
  187. REPORTER_ASSERT(reporter, teardrop_height == clip.getBounds().height());
  188. }
  189. static void test_empty(skiatest::Reporter* reporter) {
  190. SkAAClip clip0, clip1;
  191. REPORTER_ASSERT(reporter, clip0.isEmpty());
  192. REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty());
  193. REPORTER_ASSERT(reporter, clip1 == clip0);
  194. clip0.translate(10, 10); // should have no effect on empty
  195. REPORTER_ASSERT(reporter, clip0.isEmpty());
  196. REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty());
  197. REPORTER_ASSERT(reporter, clip1 == clip0);
  198. SkIRect r = { 10, 10, 40, 50 };
  199. clip0.setRect(r);
  200. REPORTER_ASSERT(reporter, !clip0.isEmpty());
  201. REPORTER_ASSERT(reporter, !clip0.getBounds().isEmpty());
  202. REPORTER_ASSERT(reporter, clip0 != clip1);
  203. REPORTER_ASSERT(reporter, clip0.getBounds() == r);
  204. clip0.setEmpty();
  205. REPORTER_ASSERT(reporter, clip0.isEmpty());
  206. REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty());
  207. REPORTER_ASSERT(reporter, clip1 == clip0);
  208. SkMask mask;
  209. clip0.copyToMask(&mask);
  210. REPORTER_ASSERT(reporter, nullptr == mask.fImage);
  211. REPORTER_ASSERT(reporter, mask.fBounds.isEmpty());
  212. }
  213. static void rand_irect(SkIRect* r, int N, SkRandom& rand) {
  214. r->setXYWH(0, 0, rand.nextU() % N, rand.nextU() % N);
  215. int dx = rand.nextU() % (2*N);
  216. int dy = rand.nextU() % (2*N);
  217. // use int dx,dy to make the subtract be signed
  218. r->offset(N - dx, N - dy);
  219. }
  220. static void test_irect(skiatest::Reporter* reporter) {
  221. SkRandom rand;
  222. for (int i = 0; i < 10000; i++) {
  223. SkAAClip clip0, clip1;
  224. SkRegion rgn0, rgn1;
  225. SkIRect r0, r1;
  226. rand_irect(&r0, 10, rand);
  227. rand_irect(&r1, 10, rand);
  228. clip0.setRect(r0);
  229. clip1.setRect(r1);
  230. rgn0.setRect(r0);
  231. rgn1.setRect(r1);
  232. for (size_t j = 0; j < SK_ARRAY_COUNT(gRgnOps); ++j) {
  233. SkRegion::Op op = gRgnOps[j];
  234. SkAAClip clip2;
  235. SkRegion rgn2;
  236. bool nonEmptyAA = clip2.op(clip0, clip1, op);
  237. bool nonEmptyBW = rgn2.op(rgn0, rgn1, op);
  238. if (nonEmptyAA != nonEmptyBW || clip2.getBounds() != rgn2.getBounds()) {
  239. ERRORF(reporter, "%s %s "
  240. "[%d %d %d %d] %s [%d %d %d %d] = BW:[%d %d %d %d] AA:[%d %d %d %d]\n",
  241. nonEmptyAA == nonEmptyBW ? "true" : "false",
  242. clip2.getBounds() == rgn2.getBounds() ? "true" : "false",
  243. r0.fLeft, r0.fTop, r0.right(), r0.bottom(),
  244. gRgnOpNames[j],
  245. r1.fLeft, r1.fTop, r1.right(), r1.bottom(),
  246. rgn2.getBounds().fLeft, rgn2.getBounds().fTop,
  247. rgn2.getBounds().right(), rgn2.getBounds().bottom(),
  248. clip2.getBounds().fLeft, clip2.getBounds().fTop,
  249. clip2.getBounds().right(), clip2.getBounds().bottom());
  250. }
  251. SkMask maskBW, maskAA;
  252. copyToMask(rgn2, &maskBW);
  253. clip2.copyToMask(&maskAA);
  254. SkAutoMaskFreeImage freeBW(maskBW.fImage);
  255. SkAutoMaskFreeImage freeAA(maskAA.fImage);
  256. REPORTER_ASSERT(reporter, maskBW == maskAA);
  257. }
  258. }
  259. }
  260. static void test_path_with_hole(skiatest::Reporter* reporter) {
  261. static const uint8_t gExpectedImage[] = {
  262. 0xFF, 0xFF, 0xFF, 0xFF,
  263. 0xFF, 0xFF, 0xFF, 0xFF,
  264. 0x00, 0x00, 0x00, 0x00,
  265. 0x00, 0x00, 0x00, 0x00,
  266. 0xFF, 0xFF, 0xFF, 0xFF,
  267. 0xFF, 0xFF, 0xFF, 0xFF,
  268. };
  269. SkMask expected;
  270. expected.fBounds.set(0, 0, 4, 6);
  271. expected.fRowBytes = 4;
  272. expected.fFormat = SkMask::kA8_Format;
  273. expected.fImage = (uint8_t*)gExpectedImage;
  274. SkPath path;
  275. path.addRect(SkRect::MakeXYWH(0, 0,
  276. SkIntToScalar(4), SkIntToScalar(2)));
  277. path.addRect(SkRect::MakeXYWH(0, SkIntToScalar(4),
  278. SkIntToScalar(4), SkIntToScalar(2)));
  279. for (int i = 0; i < 2; ++i) {
  280. SkAAClip clip;
  281. clip.setPath(path, nullptr, 1 == i);
  282. SkMask mask;
  283. clip.copyToMask(&mask);
  284. SkAutoMaskFreeImage freeM(mask.fImage);
  285. REPORTER_ASSERT(reporter, expected == mask);
  286. }
  287. }
  288. static void test_really_a_rect(skiatest::Reporter* reporter) {
  289. SkRRect rrect;
  290. rrect.setRectXY(SkRect::MakeWH(100, 100), 5, 5);
  291. SkPath path;
  292. path.addRRect(rrect);
  293. SkAAClip clip;
  294. clip.setPath(path);
  295. REPORTER_ASSERT(reporter, clip.getBounds() == SkIRect::MakeWH(100, 100));
  296. REPORTER_ASSERT(reporter, !clip.isRect());
  297. // This rect should intersect the clip, but slice-out all of the "soft" parts,
  298. // leaving just a rect.
  299. const SkIRect ir = SkIRect::MakeLTRB(10, -10, 50, 90);
  300. clip.op(ir, SkRegion::kIntersect_Op);
  301. REPORTER_ASSERT(reporter, clip.getBounds() == SkIRect::MakeLTRB(10, 0, 50, 90));
  302. // the clip recognized that that it is just a rect!
  303. REPORTER_ASSERT(reporter, clip.isRect());
  304. }
  305. static void did_dx_affect(skiatest::Reporter* reporter, const SkScalar dx[],
  306. size_t count, bool changed) {
  307. const SkIRect baseBounds = SkIRect::MakeXYWH(0, 0, 10, 10);
  308. SkIRect ir = { 0, 0, 10, 10 };
  309. for (size_t i = 0; i < count; ++i) {
  310. SkRect r;
  311. r.set(ir);
  312. SkRasterClip rc0(ir);
  313. SkRasterClip rc1(ir);
  314. SkRasterClip rc2(ir);
  315. rc0.op(r, SkMatrix::I(), baseBounds, SkRegion::kIntersect_Op, false);
  316. r.offset(dx[i], 0);
  317. rc1.op(r, SkMatrix::I(), baseBounds, SkRegion::kIntersect_Op, true);
  318. r.offset(-2*dx[i], 0);
  319. rc2.op(r, SkMatrix::I(), baseBounds, SkRegion::kIntersect_Op, true);
  320. REPORTER_ASSERT(reporter, changed != (rc0 == rc1));
  321. REPORTER_ASSERT(reporter, changed != (rc0 == rc2));
  322. }
  323. }
  324. static void test_nearly_integral(skiatest::Reporter* reporter) {
  325. // All of these should generate equivalent rasterclips
  326. static const SkScalar gSafeX[] = {
  327. 0, SK_Scalar1/1000, SK_Scalar1/100, SK_Scalar1/10,
  328. };
  329. did_dx_affect(reporter, gSafeX, SK_ARRAY_COUNT(gSafeX), false);
  330. static const SkScalar gUnsafeX[] = {
  331. SK_Scalar1/4, SK_Scalar1/3,
  332. };
  333. did_dx_affect(reporter, gUnsafeX, SK_ARRAY_COUNT(gUnsafeX), true);
  334. }
  335. static void test_regressions() {
  336. // these should not assert in the debug build
  337. // bug was introduced in rev. 3209
  338. {
  339. SkAAClip clip;
  340. SkRect r;
  341. r.fLeft = 129.892181f;
  342. r.fTop = 10.3999996f;
  343. r.fRight = 130.892181f;
  344. r.fBottom = 20.3999996f;
  345. clip.setRect(r, true);
  346. }
  347. }
  348. // Building aaclip meant aa-scan-convert a path into a huge clip.
  349. // the old algorithm sized the supersampler to the size of the clip, which overflowed
  350. // its internal 16bit coordinates. The fix was to intersect the clip+path_bounds before
  351. // sizing the supersampler.
  352. //
  353. // Before the fix, the following code would assert in debug builds.
  354. //
  355. static void test_crbug_422693(skiatest::Reporter* reporter) {
  356. SkRasterClip rc(SkIRect::MakeLTRB(-25000, -25000, 25000, 25000));
  357. SkPath path;
  358. path.addCircle(50, 50, 50);
  359. rc.op(path, SkMatrix::I(), rc.getBounds(), SkRegion::kIntersect_Op, true);
  360. }
  361. static void test_huge(skiatest::Reporter* reporter) {
  362. SkAAClip clip;
  363. int big = 0x70000000;
  364. SkIRect r = { -big, -big, big, big };
  365. SkASSERT(r.width() < 0 && r.height() < 0);
  366. clip.setRect(r);
  367. }
  368. DEF_TEST(AAClip, reporter) {
  369. test_empty(reporter);
  370. test_path_bounds(reporter);
  371. test_irect(reporter);
  372. test_rgn(reporter);
  373. test_path_with_hole(reporter);
  374. test_regressions();
  375. test_nearly_integral(reporter);
  376. test_really_a_rect(reporter);
  377. test_crbug_422693(reporter);
  378. test_huge(reporter);
  379. }