ClipStackTest.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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/SkCanvas.h"
  8. #include "include/core/SkClipOp.h"
  9. #include "include/core/SkImageInfo.h"
  10. #include "include/core/SkMatrix.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRRect.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkRegion.h"
  17. #include "include/core/SkScalar.h"
  18. #include "include/core/SkSize.h"
  19. #include "include/core/SkString.h"
  20. #include "include/core/SkSurface.h"
  21. #include "include/core/SkTypes.h"
  22. #include "include/gpu/GrConfig.h"
  23. #include "include/gpu/GrContext.h"
  24. #include "include/gpu/GrTexture.h"
  25. #include "include/private/GrResourceKey.h"
  26. #include "include/private/SkTemplates.h"
  27. #include "include/utils/SkRandom.h"
  28. #include "src/core/SkClipOpPriv.h"
  29. #include "src/core/SkClipStack.h"
  30. #include "src/core/SkTLList.h"
  31. #include "src/gpu/GrClip.h"
  32. #include "src/gpu/GrClipStackClip.h"
  33. #include "src/gpu/GrContextPriv.h"
  34. #include "src/gpu/GrReducedClip.h"
  35. #include "src/gpu/GrResourceCache.h"
  36. #include "src/gpu/GrTextureProxy.h"
  37. #include "tests/Test.h"
  38. #include "tools/gpu/GrContextFactory.h"
  39. #include <cstring>
  40. #include <initializer_list>
  41. #include <new>
  42. class GrCaps;
  43. typedef GrReducedClip::ElementList ElementList;
  44. typedef GrReducedClip::InitialState InitialState;
  45. static void test_assign_and_comparison(skiatest::Reporter* reporter) {
  46. SkClipStack s;
  47. bool doAA = false;
  48. REPORTER_ASSERT(reporter, 0 == s.getSaveCount());
  49. // Build up a clip stack with a path, an empty clip, and a rect.
  50. s.save();
  51. REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
  52. SkPath p;
  53. p.moveTo(5, 6);
  54. p.lineTo(7, 8);
  55. p.lineTo(5, 9);
  56. p.close();
  57. s.clipPath(p, SkMatrix::I(), kIntersect_SkClipOp, doAA);
  58. s.save();
  59. REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
  60. SkRect r = SkRect::MakeLTRB(1, 2, 3, 4);
  61. s.clipRect(r, SkMatrix::I(), kIntersect_SkClipOp, doAA);
  62. r = SkRect::MakeLTRB(10, 11, 12, 13);
  63. s.clipRect(r, SkMatrix::I(), kIntersect_SkClipOp, doAA);
  64. s.save();
  65. REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
  66. r = SkRect::MakeLTRB(14, 15, 16, 17);
  67. s.clipRect(r, SkMatrix::I(), kUnion_SkClipOp, doAA);
  68. // Test that assignment works.
  69. SkClipStack copy = s;
  70. REPORTER_ASSERT(reporter, s == copy);
  71. // Test that different save levels triggers not equal.
  72. s.restore();
  73. REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
  74. REPORTER_ASSERT(reporter, s != copy);
  75. // Test that an equal, but not copied version is equal.
  76. s.save();
  77. REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
  78. r = SkRect::MakeLTRB(14, 15, 16, 17);
  79. s.clipRect(r, SkMatrix::I(), kUnion_SkClipOp, doAA);
  80. REPORTER_ASSERT(reporter, s == copy);
  81. // Test that a different op on one level triggers not equal.
  82. s.restore();
  83. REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
  84. s.save();
  85. REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
  86. r = SkRect::MakeLTRB(14, 15, 16, 17);
  87. s.clipRect(r, SkMatrix::I(), kIntersect_SkClipOp, doAA);
  88. REPORTER_ASSERT(reporter, s != copy);
  89. // Test that version constructed with rect-path rather than a rect is still considered equal.
  90. s.restore();
  91. s.save();
  92. SkPath rp;
  93. rp.addRect(r);
  94. s.clipPath(rp, SkMatrix::I(), kUnion_SkClipOp, doAA);
  95. REPORTER_ASSERT(reporter, s == copy);
  96. // Test that different rects triggers not equal.
  97. s.restore();
  98. REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
  99. s.save();
  100. REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
  101. r = SkRect::MakeLTRB(24, 25, 26, 27);
  102. s.clipRect(r, SkMatrix::I(), kUnion_SkClipOp, doAA);
  103. REPORTER_ASSERT(reporter, s != copy);
  104. // Sanity check
  105. s.restore();
  106. REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
  107. copy.restore();
  108. REPORTER_ASSERT(reporter, 2 == copy.getSaveCount());
  109. REPORTER_ASSERT(reporter, s == copy);
  110. s.restore();
  111. REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
  112. copy.restore();
  113. REPORTER_ASSERT(reporter, 1 == copy.getSaveCount());
  114. REPORTER_ASSERT(reporter, s == copy);
  115. // Test that different paths triggers not equal.
  116. s.restore();
  117. REPORTER_ASSERT(reporter, 0 == s.getSaveCount());
  118. s.save();
  119. REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
  120. p.addRect(r);
  121. s.clipPath(p, SkMatrix::I(), kIntersect_SkClipOp, doAA);
  122. REPORTER_ASSERT(reporter, s != copy);
  123. }
  124. static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack,
  125. int count) {
  126. SkClipStack::B2TIter iter(stack);
  127. int counter = 0;
  128. while (iter.next()) {
  129. counter += 1;
  130. }
  131. REPORTER_ASSERT(reporter, count == counter);
  132. }
  133. // Exercise the SkClipStack's bottom to top and bidirectional iterators
  134. // (including the skipToTopmost functionality)
  135. static void test_iterators(skiatest::Reporter* reporter) {
  136. SkClipStack stack;
  137. static const SkRect gRects[] = {
  138. { 0, 0, 40, 40 },
  139. { 60, 0, 100, 40 },
  140. { 0, 60, 40, 100 },
  141. { 60, 60, 100, 100 }
  142. };
  143. for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
  144. // the union op will prevent these from being fused together
  145. stack.clipRect(gRects[i], SkMatrix::I(), kUnion_SkClipOp, false);
  146. }
  147. assert_count(reporter, stack, 4);
  148. // bottom to top iteration
  149. {
  150. const SkClipStack::Element* element = nullptr;
  151. SkClipStack::B2TIter iter(stack);
  152. int i;
  153. for (i = 0, element = iter.next(); element; ++i, element = iter.next()) {
  154. REPORTER_ASSERT(reporter, SkClipStack::Element::DeviceSpaceType::kRect ==
  155. element->getDeviceSpaceType());
  156. REPORTER_ASSERT(reporter, element->getDeviceSpaceRect() == gRects[i]);
  157. }
  158. SkASSERT(i == 4);
  159. }
  160. // top to bottom iteration
  161. {
  162. const SkClipStack::Element* element = nullptr;
  163. SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
  164. int i;
  165. for (i = 3, element = iter.prev(); element; --i, element = iter.prev()) {
  166. REPORTER_ASSERT(reporter, SkClipStack::Element::DeviceSpaceType::kRect ==
  167. element->getDeviceSpaceType());
  168. REPORTER_ASSERT(reporter, element->getDeviceSpaceRect() == gRects[i]);
  169. }
  170. SkASSERT(i == -1);
  171. }
  172. // skipToTopmost
  173. {
  174. const SkClipStack::Element* element = nullptr;
  175. SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
  176. element = iter.skipToTopmost(kUnion_SkClipOp);
  177. REPORTER_ASSERT(reporter, SkClipStack::Element::DeviceSpaceType::kRect ==
  178. element->getDeviceSpaceType());
  179. REPORTER_ASSERT(reporter, element->getDeviceSpaceRect() == gRects[3]);
  180. }
  181. }
  182. // Exercise the SkClipStack's getConservativeBounds computation
  183. static void test_bounds(skiatest::Reporter* reporter,
  184. SkClipStack::Element::DeviceSpaceType primType) {
  185. static const int gNumCases = 20;
  186. static const SkRect gAnswerRectsBW[gNumCases] = {
  187. // A op B
  188. { 40, 40, 50, 50 },
  189. { 10, 10, 50, 50 },
  190. { 10, 10, 80, 80 },
  191. { 10, 10, 80, 80 },
  192. { 40, 40, 80, 80 },
  193. // invA op B
  194. { 40, 40, 80, 80 },
  195. { 0, 0, 100, 100 },
  196. { 0, 0, 100, 100 },
  197. { 0, 0, 100, 100 },
  198. { 40, 40, 50, 50 },
  199. // A op invB
  200. { 10, 10, 50, 50 },
  201. { 40, 40, 50, 50 },
  202. { 0, 0, 100, 100 },
  203. { 0, 0, 100, 100 },
  204. { 0, 0, 100, 100 },
  205. // invA op invB
  206. { 0, 0, 100, 100 },
  207. { 40, 40, 80, 80 },
  208. { 0, 0, 100, 100 },
  209. { 10, 10, 80, 80 },
  210. { 10, 10, 50, 50 },
  211. };
  212. static const SkClipOp gOps[] = {
  213. kIntersect_SkClipOp,
  214. kDifference_SkClipOp,
  215. kUnion_SkClipOp,
  216. kXOR_SkClipOp,
  217. kReverseDifference_SkClipOp
  218. };
  219. SkRect rectA, rectB;
  220. rectA.iset(10, 10, 50, 50);
  221. rectB.iset(40, 40, 80, 80);
  222. SkRRect rrectA, rrectB;
  223. rrectA.setOval(rectA);
  224. rrectB.setRectXY(rectB, SkIntToScalar(1), SkIntToScalar(2));
  225. SkPath pathA, pathB;
  226. pathA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
  227. pathB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
  228. SkClipStack stack;
  229. SkRect devClipBound;
  230. bool isIntersectionOfRects = false;
  231. int testCase = 0;
  232. int numBitTests = SkClipStack::Element::DeviceSpaceType::kPath == primType ? 4 : 1;
  233. for (int invBits = 0; invBits < numBitTests; ++invBits) {
  234. for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
  235. stack.save();
  236. bool doInvA = SkToBool(invBits & 1);
  237. bool doInvB = SkToBool(invBits & 2);
  238. pathA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
  239. SkPath::kEvenOdd_FillType);
  240. pathB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
  241. SkPath::kEvenOdd_FillType);
  242. switch (primType) {
  243. case SkClipStack::Element::DeviceSpaceType::kEmpty:
  244. SkDEBUGFAIL("Don't call this with kEmpty.");
  245. break;
  246. case SkClipStack::Element::DeviceSpaceType::kRect:
  247. stack.clipRect(rectA, SkMatrix::I(), kIntersect_SkClipOp, false);
  248. stack.clipRect(rectB, SkMatrix::I(), gOps[op], false);
  249. break;
  250. case SkClipStack::Element::DeviceSpaceType::kRRect:
  251. stack.clipRRect(rrectA, SkMatrix::I(), kIntersect_SkClipOp, false);
  252. stack.clipRRect(rrectB, SkMatrix::I(), gOps[op], false);
  253. break;
  254. case SkClipStack::Element::DeviceSpaceType::kPath:
  255. stack.clipPath(pathA, SkMatrix::I(), kIntersect_SkClipOp, false);
  256. stack.clipPath(pathB, SkMatrix::I(), gOps[op], false);
  257. break;
  258. }
  259. REPORTER_ASSERT(reporter, !stack.isWideOpen());
  260. REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID != stack.getTopmostGenID());
  261. stack.getConservativeBounds(0, 0, 100, 100, &devClipBound,
  262. &isIntersectionOfRects);
  263. if (SkClipStack::Element::DeviceSpaceType::kRect == primType) {
  264. REPORTER_ASSERT(reporter, isIntersectionOfRects ==
  265. (gOps[op] == kIntersect_SkClipOp));
  266. } else {
  267. REPORTER_ASSERT(reporter, !isIntersectionOfRects);
  268. }
  269. SkASSERT(testCase < gNumCases);
  270. REPORTER_ASSERT(reporter, devClipBound == gAnswerRectsBW[testCase]);
  271. ++testCase;
  272. stack.restore();
  273. }
  274. }
  275. }
  276. // Test out 'isWideOpen' entry point
  277. static void test_isWideOpen(skiatest::Reporter* reporter) {
  278. {
  279. // Empty stack is wide open. Wide open stack means that gen id is wide open.
  280. SkClipStack stack;
  281. REPORTER_ASSERT(reporter, stack.isWideOpen());
  282. REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
  283. }
  284. SkRect rectA, rectB;
  285. rectA.iset(10, 10, 40, 40);
  286. rectB.iset(50, 50, 80, 80);
  287. // Stack should initially be wide open
  288. {
  289. SkClipStack stack;
  290. REPORTER_ASSERT(reporter, stack.isWideOpen());
  291. REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
  292. }
  293. // Test out case where the user specifies a union that includes everything
  294. {
  295. SkClipStack stack;
  296. SkPath clipA, clipB;
  297. clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
  298. clipA.setFillType(SkPath::kInverseEvenOdd_FillType);
  299. clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
  300. clipB.setFillType(SkPath::kInverseEvenOdd_FillType);
  301. stack.clipPath(clipA, SkMatrix::I(), kReplace_SkClipOp, false);
  302. stack.clipPath(clipB, SkMatrix::I(), kUnion_SkClipOp, false);
  303. REPORTER_ASSERT(reporter, stack.isWideOpen());
  304. REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
  305. }
  306. // Test out union w/ a wide open clip
  307. {
  308. SkClipStack stack;
  309. stack.clipRect(rectA, SkMatrix::I(), kUnion_SkClipOp, false);
  310. REPORTER_ASSERT(reporter, stack.isWideOpen());
  311. REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
  312. }
  313. // Test out empty difference from a wide open clip
  314. {
  315. SkClipStack stack;
  316. SkRect emptyRect;
  317. emptyRect.setEmpty();
  318. stack.clipRect(emptyRect, SkMatrix::I(), kDifference_SkClipOp, false);
  319. REPORTER_ASSERT(reporter, stack.isWideOpen());
  320. REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
  321. }
  322. // Test out return to wide open
  323. {
  324. SkClipStack stack;
  325. stack.save();
  326. stack.clipRect(rectA, SkMatrix::I(), kReplace_SkClipOp, false);
  327. REPORTER_ASSERT(reporter, !stack.isWideOpen());
  328. REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID != stack.getTopmostGenID());
  329. stack.restore();
  330. REPORTER_ASSERT(reporter, stack.isWideOpen());
  331. REPORTER_ASSERT(reporter, SkClipStack::kWideOpenGenID == stack.getTopmostGenID());
  332. }
  333. }
  334. static int count(const SkClipStack& stack) {
  335. SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
  336. const SkClipStack::Element* element = nullptr;
  337. int count = 0;
  338. for (element = iter.prev(); element; element = iter.prev(), ++count) {
  339. }
  340. return count;
  341. }
  342. static void test_rect_inverse_fill(skiatest::Reporter* reporter) {
  343. // non-intersecting rectangles
  344. SkRect rect = SkRect::MakeLTRB(0, 0, 10, 10);
  345. SkPath path;
  346. path.addRect(rect);
  347. path.toggleInverseFillType();
  348. SkClipStack stack;
  349. stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false);
  350. SkRect bounds;
  351. SkClipStack::BoundsType boundsType;
  352. stack.getBounds(&bounds, &boundsType);
  353. REPORTER_ASSERT(reporter, SkClipStack::kInsideOut_BoundsType == boundsType);
  354. REPORTER_ASSERT(reporter, bounds == rect);
  355. }
  356. static void test_rect_replace(skiatest::Reporter* reporter) {
  357. SkRect rect = SkRect::MakeWH(100, 100);
  358. SkRect rect2 = SkRect::MakeXYWH(50, 50, 100, 100);
  359. SkRect bound;
  360. SkClipStack::BoundsType type;
  361. bool isIntersectionOfRects;
  362. // Adding a new rect with the replace operator should not increase
  363. // the stack depth. BW replacing BW.
  364. {
  365. SkClipStack stack;
  366. REPORTER_ASSERT(reporter, 0 == count(stack));
  367. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  368. REPORTER_ASSERT(reporter, 1 == count(stack));
  369. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  370. REPORTER_ASSERT(reporter, 1 == count(stack));
  371. }
  372. // Adding a new rect with the replace operator should not increase
  373. // the stack depth. AA replacing AA.
  374. {
  375. SkClipStack stack;
  376. REPORTER_ASSERT(reporter, 0 == count(stack));
  377. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, true);
  378. REPORTER_ASSERT(reporter, 1 == count(stack));
  379. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, true);
  380. REPORTER_ASSERT(reporter, 1 == count(stack));
  381. }
  382. // Adding a new rect with the replace operator should not increase
  383. // the stack depth. BW replacing AA replacing BW.
  384. {
  385. SkClipStack stack;
  386. REPORTER_ASSERT(reporter, 0 == count(stack));
  387. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  388. REPORTER_ASSERT(reporter, 1 == count(stack));
  389. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, true);
  390. REPORTER_ASSERT(reporter, 1 == count(stack));
  391. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  392. REPORTER_ASSERT(reporter, 1 == count(stack));
  393. }
  394. // Make sure replace clip rects don't collapse too much.
  395. {
  396. SkClipStack stack;
  397. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  398. stack.clipRect(rect2, SkMatrix::I(), kIntersect_SkClipOp, false);
  399. REPORTER_ASSERT(reporter, 1 == count(stack));
  400. stack.save();
  401. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  402. REPORTER_ASSERT(reporter, 2 == count(stack));
  403. stack.getBounds(&bound, &type, &isIntersectionOfRects);
  404. REPORTER_ASSERT(reporter, bound == rect);
  405. stack.restore();
  406. REPORTER_ASSERT(reporter, 1 == count(stack));
  407. stack.save();
  408. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  409. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  410. REPORTER_ASSERT(reporter, 2 == count(stack));
  411. stack.restore();
  412. REPORTER_ASSERT(reporter, 1 == count(stack));
  413. stack.save();
  414. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  415. stack.clipRect(rect2, SkMatrix::I(), kIntersect_SkClipOp, false);
  416. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, false);
  417. REPORTER_ASSERT(reporter, 2 == count(stack));
  418. stack.restore();
  419. REPORTER_ASSERT(reporter, 1 == count(stack));
  420. }
  421. }
  422. // Simplified path-based version of test_rect_replace.
  423. static void test_path_replace(skiatest::Reporter* reporter) {
  424. SkRect rect = SkRect::MakeWH(100, 100);
  425. SkPath path;
  426. path.addCircle(50, 50, 50);
  427. // Replace operation doesn't grow the stack.
  428. {
  429. SkClipStack stack;
  430. REPORTER_ASSERT(reporter, 0 == count(stack));
  431. stack.clipPath(path, SkMatrix::I(), kReplace_SkClipOp, false);
  432. REPORTER_ASSERT(reporter, 1 == count(stack));
  433. stack.clipPath(path, SkMatrix::I(), kReplace_SkClipOp, false);
  434. REPORTER_ASSERT(reporter, 1 == count(stack));
  435. }
  436. // Replacing rect with path.
  437. {
  438. SkClipStack stack;
  439. stack.clipRect(rect, SkMatrix::I(), kReplace_SkClipOp, true);
  440. REPORTER_ASSERT(reporter, 1 == count(stack));
  441. stack.clipPath(path, SkMatrix::I(), kReplace_SkClipOp, true);
  442. REPORTER_ASSERT(reporter, 1 == count(stack));
  443. }
  444. }
  445. // Test out SkClipStack's merging of rect clips. In particular exercise
  446. // merging of aa vs. bw rects.
  447. static void test_rect_merging(skiatest::Reporter* reporter) {
  448. SkRect overlapLeft = SkRect::MakeLTRB(10, 10, 50, 50);
  449. SkRect overlapRight = SkRect::MakeLTRB(40, 40, 80, 80);
  450. SkRect nestedParent = SkRect::MakeLTRB(10, 10, 90, 90);
  451. SkRect nestedChild = SkRect::MakeLTRB(40, 40, 60, 60);
  452. SkRect bound;
  453. SkClipStack::BoundsType type;
  454. bool isIntersectionOfRects;
  455. // all bw overlapping - should merge
  456. {
  457. SkClipStack stack;
  458. stack.clipRect(overlapLeft, SkMatrix::I(), kReplace_SkClipOp, false);
  459. stack.clipRect(overlapRight, SkMatrix::I(), kIntersect_SkClipOp, false);
  460. REPORTER_ASSERT(reporter, 1 == count(stack));
  461. stack.getBounds(&bound, &type, &isIntersectionOfRects);
  462. REPORTER_ASSERT(reporter, isIntersectionOfRects);
  463. }
  464. // all aa overlapping - should merge
  465. {
  466. SkClipStack stack;
  467. stack.clipRect(overlapLeft, SkMatrix::I(), kReplace_SkClipOp, true);
  468. stack.clipRect(overlapRight, SkMatrix::I(), kIntersect_SkClipOp, true);
  469. REPORTER_ASSERT(reporter, 1 == count(stack));
  470. stack.getBounds(&bound, &type, &isIntersectionOfRects);
  471. REPORTER_ASSERT(reporter, isIntersectionOfRects);
  472. }
  473. // mixed overlapping - should _not_ merge
  474. {
  475. SkClipStack stack;
  476. stack.clipRect(overlapLeft, SkMatrix::I(), kReplace_SkClipOp, true);
  477. stack.clipRect(overlapRight, SkMatrix::I(), kIntersect_SkClipOp, false);
  478. REPORTER_ASSERT(reporter, 2 == count(stack));
  479. stack.getBounds(&bound, &type, &isIntersectionOfRects);
  480. REPORTER_ASSERT(reporter, !isIntersectionOfRects);
  481. }
  482. // mixed nested (bw inside aa) - should merge
  483. {
  484. SkClipStack stack;
  485. stack.clipRect(nestedParent, SkMatrix::I(), kReplace_SkClipOp, true);
  486. stack.clipRect(nestedChild, SkMatrix::I(), kIntersect_SkClipOp, false);
  487. REPORTER_ASSERT(reporter, 1 == count(stack));
  488. stack.getBounds(&bound, &type, &isIntersectionOfRects);
  489. REPORTER_ASSERT(reporter, isIntersectionOfRects);
  490. }
  491. // mixed nested (aa inside bw) - should merge
  492. {
  493. SkClipStack stack;
  494. stack.clipRect(nestedParent, SkMatrix::I(), kReplace_SkClipOp, false);
  495. stack.clipRect(nestedChild, SkMatrix::I(), kIntersect_SkClipOp, true);
  496. REPORTER_ASSERT(reporter, 1 == count(stack));
  497. stack.getBounds(&bound, &type, &isIntersectionOfRects);
  498. REPORTER_ASSERT(reporter, isIntersectionOfRects);
  499. }
  500. // reverse nested (aa inside bw) - should _not_ merge
  501. {
  502. SkClipStack stack;
  503. stack.clipRect(nestedChild, SkMatrix::I(), kReplace_SkClipOp, false);
  504. stack.clipRect(nestedParent, SkMatrix::I(), kIntersect_SkClipOp, true);
  505. REPORTER_ASSERT(reporter, 2 == count(stack));
  506. stack.getBounds(&bound, &type, &isIntersectionOfRects);
  507. REPORTER_ASSERT(reporter, !isIntersectionOfRects);
  508. }
  509. }
  510. static void test_quickContains(skiatest::Reporter* reporter) {
  511. SkRect testRect = SkRect::MakeLTRB(10, 10, 40, 40);
  512. SkRect insideRect = SkRect::MakeLTRB(20, 20, 30, 30);
  513. SkRect intersectingRect = SkRect::MakeLTRB(25, 25, 50, 50);
  514. SkRect outsideRect = SkRect::MakeLTRB(0, 0, 50, 50);
  515. SkRect nonIntersectingRect = SkRect::MakeLTRB(100, 100, 110, 110);
  516. SkPath insideCircle;
  517. insideCircle.addCircle(25, 25, 5);
  518. SkPath intersectingCircle;
  519. intersectingCircle.addCircle(25, 40, 10);
  520. SkPath outsideCircle;
  521. outsideCircle.addCircle(25, 25, 50);
  522. SkPath nonIntersectingCircle;
  523. nonIntersectingCircle.addCircle(100, 100, 5);
  524. {
  525. SkClipStack stack;
  526. stack.clipRect(outsideRect, SkMatrix::I(), kDifference_SkClipOp, false);
  527. // return false because quickContains currently does not care for kDifference_SkClipOp
  528. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  529. }
  530. // Replace Op tests
  531. {
  532. SkClipStack stack;
  533. stack.clipRect(outsideRect, SkMatrix::I(), kReplace_SkClipOp, false);
  534. REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
  535. }
  536. {
  537. SkClipStack stack;
  538. stack.clipRect(insideRect, SkMatrix::I(), kIntersect_SkClipOp, false);
  539. stack.save(); // To prevent in-place substitution by replace OP
  540. stack.clipRect(outsideRect, SkMatrix::I(), kReplace_SkClipOp, false);
  541. REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
  542. stack.restore();
  543. }
  544. {
  545. SkClipStack stack;
  546. stack.clipRect(outsideRect, SkMatrix::I(), kIntersect_SkClipOp, false);
  547. stack.save(); // To prevent in-place substitution by replace OP
  548. stack.clipRect(insideRect, SkMatrix::I(), kReplace_SkClipOp, false);
  549. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  550. stack.restore();
  551. }
  552. // Verify proper traversal of multi-element clip
  553. {
  554. SkClipStack stack;
  555. stack.clipRect(insideRect, SkMatrix::I(), kIntersect_SkClipOp, false);
  556. // Use a path for second clip to prevent in-place intersection
  557. stack.clipPath(outsideCircle, SkMatrix::I(), kIntersect_SkClipOp, false);
  558. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  559. }
  560. // Intersect Op tests with rectangles
  561. {
  562. SkClipStack stack;
  563. stack.clipRect(outsideRect, SkMatrix::I(), kIntersect_SkClipOp, false);
  564. REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
  565. }
  566. {
  567. SkClipStack stack;
  568. stack.clipRect(insideRect, SkMatrix::I(), kIntersect_SkClipOp, false);
  569. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  570. }
  571. {
  572. SkClipStack stack;
  573. stack.clipRect(intersectingRect, SkMatrix::I(), kIntersect_SkClipOp, false);
  574. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  575. }
  576. {
  577. SkClipStack stack;
  578. stack.clipRect(nonIntersectingRect, SkMatrix::I(), kIntersect_SkClipOp, false);
  579. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  580. }
  581. // Intersect Op tests with circle paths
  582. {
  583. SkClipStack stack;
  584. stack.clipPath(outsideCircle, SkMatrix::I(), kIntersect_SkClipOp, false);
  585. REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
  586. }
  587. {
  588. SkClipStack stack;
  589. stack.clipPath(insideCircle, SkMatrix::I(), kIntersect_SkClipOp, false);
  590. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  591. }
  592. {
  593. SkClipStack stack;
  594. stack.clipPath(intersectingCircle, SkMatrix::I(), kIntersect_SkClipOp, false);
  595. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  596. }
  597. {
  598. SkClipStack stack;
  599. stack.clipPath(nonIntersectingCircle, SkMatrix::I(), kIntersect_SkClipOp, false);
  600. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  601. }
  602. // Intersect Op tests with inverse filled rectangles
  603. {
  604. SkClipStack stack;
  605. SkPath path;
  606. path.addRect(outsideRect);
  607. path.toggleInverseFillType();
  608. stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false);
  609. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  610. }
  611. {
  612. SkClipStack stack;
  613. SkPath path;
  614. path.addRect(insideRect);
  615. path.toggleInverseFillType();
  616. stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false);
  617. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  618. }
  619. {
  620. SkClipStack stack;
  621. SkPath path;
  622. path.addRect(intersectingRect);
  623. path.toggleInverseFillType();
  624. stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false);
  625. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  626. }
  627. {
  628. SkClipStack stack;
  629. SkPath path;
  630. path.addRect(nonIntersectingRect);
  631. path.toggleInverseFillType();
  632. stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false);
  633. REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
  634. }
  635. // Intersect Op tests with inverse filled circles
  636. {
  637. SkClipStack stack;
  638. SkPath path = outsideCircle;
  639. path.toggleInverseFillType();
  640. stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false);
  641. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  642. }
  643. {
  644. SkClipStack stack;
  645. SkPath path = insideCircle;
  646. path.toggleInverseFillType();
  647. stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false);
  648. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  649. }
  650. {
  651. SkClipStack stack;
  652. SkPath path = intersectingCircle;
  653. path.toggleInverseFillType();
  654. stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false);
  655. REPORTER_ASSERT(reporter, false == stack.quickContains(testRect));
  656. }
  657. {
  658. SkClipStack stack;
  659. SkPath path = nonIntersectingCircle;
  660. path.toggleInverseFillType();
  661. stack.clipPath(path, SkMatrix::I(), kIntersect_SkClipOp, false);
  662. REPORTER_ASSERT(reporter, true == stack.quickContains(testRect));
  663. }
  664. }
  665. static void set_region_to_stack(const SkClipStack& stack, const SkIRect& bounds, SkRegion* region) {
  666. region->setRect(bounds);
  667. SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
  668. while (const SkClipStack::Element *element = iter.next()) {
  669. SkRegion elemRegion;
  670. SkRegion boundsRgn(bounds);
  671. SkPath path;
  672. switch (element->getDeviceSpaceType()) {
  673. case SkClipStack::Element::DeviceSpaceType::kEmpty:
  674. elemRegion.setEmpty();
  675. break;
  676. default:
  677. element->asDeviceSpacePath(&path);
  678. elemRegion.setPath(path, boundsRgn);
  679. break;
  680. }
  681. region->op(elemRegion, (SkRegion::Op)element->getOp());
  682. }
  683. }
  684. static void test_invfill_diff_bug(skiatest::Reporter* reporter) {
  685. SkClipStack stack;
  686. stack.clipRect({10, 10, 20, 20}, SkMatrix::I(), kIntersect_SkClipOp, false);
  687. SkPath path;
  688. path.addRect({30, 10, 40, 20});
  689. path.setFillType(SkPath::kInverseWinding_FillType);
  690. stack.clipPath(path, SkMatrix::I(), kDifference_SkClipOp, false);
  691. REPORTER_ASSERT(reporter, SkClipStack::kEmptyGenID == stack.getTopmostGenID());
  692. SkRect stackBounds;
  693. SkClipStack::BoundsType stackBoundsType;
  694. stack.getBounds(&stackBounds, &stackBoundsType);
  695. REPORTER_ASSERT(reporter, stackBounds.isEmpty());
  696. REPORTER_ASSERT(reporter, SkClipStack::kNormal_BoundsType == stackBoundsType);
  697. SkRegion region;
  698. set_region_to_stack(stack, {0, 0, 50, 30}, &region);
  699. REPORTER_ASSERT(reporter, region.isEmpty());
  700. }
  701. ///////////////////////////////////////////////////////////////////////////////////////////////////
  702. // Functions that add a shape to the clip stack. The shape is computed from a rectangle.
  703. // AA is always disabled since the clip stack reducer can cause changes in aa rasterization of the
  704. // stack. A fractional edge repeated in different elements may be rasterized fewer times using the
  705. // reduced stack.
  706. typedef void (*AddElementFunc) (const SkRect& rect,
  707. bool invert,
  708. SkClipOp op,
  709. SkClipStack* stack,
  710. bool doAA);
  711. static void add_round_rect(const SkRect& rect, bool invert, SkClipOp op, SkClipStack* stack,
  712. bool doAA) {
  713. SkScalar rx = rect.width() / 10;
  714. SkScalar ry = rect.height() / 20;
  715. if (invert) {
  716. SkPath path;
  717. path.addRoundRect(rect, rx, ry);
  718. path.setFillType(SkPath::kInverseWinding_FillType);
  719. stack->clipPath(path, SkMatrix::I(), op, doAA);
  720. } else {
  721. SkRRect rrect;
  722. rrect.setRectXY(rect, rx, ry);
  723. stack->clipRRect(rrect, SkMatrix::I(), op, doAA);
  724. }
  725. };
  726. static void add_rect(const SkRect& rect, bool invert, SkClipOp op, SkClipStack* stack,
  727. bool doAA) {
  728. if (invert) {
  729. SkPath path;
  730. path.addRect(rect);
  731. path.setFillType(SkPath::kInverseWinding_FillType);
  732. stack->clipPath(path, SkMatrix::I(), op, doAA);
  733. } else {
  734. stack->clipRect(rect, SkMatrix::I(), op, doAA);
  735. }
  736. };
  737. static void add_oval(const SkRect& rect, bool invert, SkClipOp op, SkClipStack* stack,
  738. bool doAA) {
  739. SkPath path;
  740. path.addOval(rect);
  741. if (invert) {
  742. path.setFillType(SkPath::kInverseWinding_FillType);
  743. }
  744. stack->clipPath(path, SkMatrix::I(), op, doAA);
  745. };
  746. static void add_elem_to_stack(const SkClipStack::Element& element, SkClipStack* stack) {
  747. switch (element.getDeviceSpaceType()) {
  748. case SkClipStack::Element::DeviceSpaceType::kRect:
  749. stack->clipRect(element.getDeviceSpaceRect(), SkMatrix::I(), element.getOp(),
  750. element.isAA());
  751. break;
  752. case SkClipStack::Element::DeviceSpaceType::kRRect:
  753. stack->clipRRect(element.getDeviceSpaceRRect(), SkMatrix::I(), element.getOp(),
  754. element.isAA());
  755. break;
  756. case SkClipStack::Element::DeviceSpaceType::kPath:
  757. stack->clipPath(element.getDeviceSpacePath(), SkMatrix::I(), element.getOp(),
  758. element.isAA());
  759. break;
  760. case SkClipStack::Element::DeviceSpaceType::kEmpty:
  761. SkDEBUGFAIL("Why did the reducer produce an explicit empty.");
  762. stack->clipEmpty();
  763. break;
  764. }
  765. }
  766. static void test_reduced_clip_stack(skiatest::Reporter* reporter) {
  767. // We construct random clip stacks, reduce them, and then rasterize both versions to verify that
  768. // they are equal.
  769. // All the clip elements will be contained within these bounds.
  770. static const SkIRect kIBounds = SkIRect::MakeWH(100, 100);
  771. static const SkRect kBounds = SkRect::Make(kIBounds);
  772. enum {
  773. kNumTests = 250,
  774. kMinElemsPerTest = 1,
  775. kMaxElemsPerTest = 50,
  776. };
  777. // min/max size of a clip element as a fraction of kBounds.
  778. static const SkScalar kMinElemSizeFrac = SK_Scalar1 / 5;
  779. static const SkScalar kMaxElemSizeFrac = SK_Scalar1;
  780. static const SkClipOp kOps[] = {
  781. kDifference_SkClipOp,
  782. kIntersect_SkClipOp,
  783. kUnion_SkClipOp,
  784. kXOR_SkClipOp,
  785. kReverseDifference_SkClipOp,
  786. kReplace_SkClipOp,
  787. };
  788. // Replace operations short-circuit the optimizer. We want to make sure that we test this code
  789. // path a little bit but we don't want it to prevent us from testing many longer traversals in
  790. // the optimizer.
  791. static const int kReplaceDiv = 4 * kMaxElemsPerTest;
  792. // We want to test inverse fills. However, they are quite rare in practice so don't over do it.
  793. static const SkScalar kFractionInverted = SK_Scalar1 / kMaxElemsPerTest;
  794. static const SkScalar kFractionAntialiased = 0.25;
  795. static const AddElementFunc kElementFuncs[] = {
  796. add_rect,
  797. add_round_rect,
  798. add_oval,
  799. };
  800. SkRandom r;
  801. for (int i = 0; i < kNumTests; ++i) {
  802. SkString testCase;
  803. testCase.printf("Iteration %d", i);
  804. // Randomly generate a clip stack.
  805. SkClipStack stack;
  806. int numElems = r.nextRangeU(kMinElemsPerTest, kMaxElemsPerTest);
  807. bool doAA = r.nextBiasedBool(kFractionAntialiased);
  808. for (int e = 0; e < numElems; ++e) {
  809. SkClipOp op = kOps[r.nextULessThan(SK_ARRAY_COUNT(kOps))];
  810. if (op == kReplace_SkClipOp) {
  811. if (r.nextU() % kReplaceDiv) {
  812. --e;
  813. continue;
  814. }
  815. }
  816. // saves can change the clip stack behavior when an element is added.
  817. bool doSave = r.nextBool();
  818. SkSize size = SkSize::Make(
  819. kBounds.width() * r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac),
  820. kBounds.height() * r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac));
  821. SkPoint xy = {r.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth),
  822. r.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight)};
  823. SkRect rect;
  824. if (doAA) {
  825. rect.setXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight);
  826. if (GrClip::IsPixelAligned(rect)) {
  827. // Don't create an element that may accidentally become not antialiased.
  828. rect.outset(0.5f, 0.5f);
  829. }
  830. SkASSERT(!GrClip::IsPixelAligned(rect));
  831. } else {
  832. rect.setXYWH(SkScalarFloorToScalar(xy.fX),
  833. SkScalarFloorToScalar(xy.fY),
  834. SkScalarCeilToScalar(size.fWidth),
  835. SkScalarCeilToScalar(size.fHeight));
  836. }
  837. bool invert = r.nextBiasedBool(kFractionInverted);
  838. kElementFuncs[r.nextULessThan(SK_ARRAY_COUNT(kElementFuncs))](rect, invert, op, &stack,
  839. doAA);
  840. if (doSave) {
  841. stack.save();
  842. }
  843. }
  844. auto context = GrContext::MakeMock(nullptr);
  845. const GrCaps* caps = context->priv().caps();
  846. // Zero the memory we will new the GrReducedClip into. This ensures the elements gen ID
  847. // will be kInvalidGenID if left uninitialized.
  848. SkAlignedSTStorage<1, GrReducedClip> storage;
  849. memset(storage.get(), 0, sizeof(GrReducedClip));
  850. GR_STATIC_ASSERT(0 == SkClipStack::kInvalidGenID);
  851. // Get the reduced version of the stack.
  852. SkRect queryBounds = kBounds;
  853. queryBounds.outset(kBounds.width() / 2, kBounds.height() / 2);
  854. const GrReducedClip* reduced = new (storage.get()) GrReducedClip(stack, queryBounds, caps);
  855. REPORTER_ASSERT(reporter,
  856. reduced->maskElements().isEmpty() ||
  857. SkClipStack::kInvalidGenID != reduced->maskGenID(),
  858. testCase.c_str());
  859. if (!reduced->maskElements().isEmpty()) {
  860. REPORTER_ASSERT(reporter, reduced->hasScissor(), testCase.c_str());
  861. SkRect stackBounds;
  862. SkClipStack::BoundsType stackBoundsType;
  863. stack.getBounds(&stackBounds, &stackBoundsType);
  864. REPORTER_ASSERT(reporter, reduced->maskRequiresAA() == doAA, testCase.c_str());
  865. }
  866. // Build a new clip stack based on the reduced clip elements
  867. SkClipStack reducedStack;
  868. if (GrReducedClip::InitialState::kAllOut == reduced->initialState()) {
  869. // whether the result is bounded or not, the whole plane should start outside the clip.
  870. reducedStack.clipEmpty();
  871. }
  872. for (ElementList::Iter iter(reduced->maskElements()); iter.get(); iter.next()) {
  873. add_elem_to_stack(*iter.get(), &reducedStack);
  874. }
  875. SkIRect scissor = reduced->hasScissor() ? reduced->scissor() : kIBounds;
  876. // GrReducedClipStack assumes that the final result is clipped to the returned bounds
  877. reducedStack.clipDevRect(scissor, kIntersect_SkClipOp);
  878. stack.clipDevRect(scissor, kIntersect_SkClipOp);
  879. // convert both the original stack and reduced stack to SkRegions and see if they're equal
  880. SkRegion region;
  881. set_region_to_stack(stack, scissor, &region);
  882. SkRegion reducedRegion;
  883. set_region_to_stack(reducedStack, scissor, &reducedRegion);
  884. REPORTER_ASSERT(reporter, region == reducedRegion, testCase.c_str());
  885. reduced->~GrReducedClip();
  886. }
  887. }
  888. #ifdef SK_BUILD_FOR_WIN
  889. #define SUPPRESS_VISIBILITY_WARNING
  890. #else
  891. #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
  892. #endif
  893. static void test_reduced_clip_stack_genid(skiatest::Reporter* reporter) {
  894. {
  895. SkClipStack stack;
  896. stack.clipRect(SkRect::MakeXYWH(0, 0, 100, 100), SkMatrix::I(), kReplace_SkClipOp,
  897. true);
  898. stack.clipRect(SkRect::MakeXYWH(0, 0, SkScalar(50.3), SkScalar(50.3)), SkMatrix::I(),
  899. kReplace_SkClipOp, true);
  900. SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100);
  901. auto context = GrContext::MakeMock(nullptr);
  902. const GrCaps* caps = context->priv().caps();
  903. SkAlignedSTStorage<1, GrReducedClip> storage;
  904. memset(storage.get(), 0, sizeof(GrReducedClip));
  905. GR_STATIC_ASSERT(0 == SkClipStack::kInvalidGenID);
  906. const GrReducedClip* reduced = new (storage.get()) GrReducedClip(stack, bounds, caps);
  907. REPORTER_ASSERT(reporter, reduced->maskElements().count() == 1);
  908. // Clips will be cached based on the generation id. Make sure the gen id is valid.
  909. REPORTER_ASSERT(reporter, SkClipStack::kInvalidGenID != reduced->maskGenID());
  910. reduced->~GrReducedClip();
  911. }
  912. {
  913. SkClipStack stack;
  914. // Create a clip with following 25.3, 25.3 boxes which are 25 apart:
  915. // A B
  916. // C D
  917. stack.clipRect(SkRect::MakeXYWH(0, 0, SkScalar(25.3), SkScalar(25.3)), SkMatrix::I(),
  918. kReplace_SkClipOp, true);
  919. uint32_t genIDA = stack.getTopmostGenID();
  920. stack.clipRect(SkRect::MakeXYWH(50, 0, SkScalar(25.3), SkScalar(25.3)), SkMatrix::I(),
  921. kUnion_SkClipOp, true);
  922. uint32_t genIDB = stack.getTopmostGenID();
  923. stack.clipRect(SkRect::MakeXYWH(0, 50, SkScalar(25.3), SkScalar(25.3)), SkMatrix::I(),
  924. kUnion_SkClipOp, true);
  925. uint32_t genIDC = stack.getTopmostGenID();
  926. stack.clipRect(SkRect::MakeXYWH(50, 50, SkScalar(25.3), SkScalar(25.3)), SkMatrix::I(),
  927. kUnion_SkClipOp, true);
  928. uint32_t genIDD = stack.getTopmostGenID();
  929. #define IXYWH SkIRect::MakeXYWH
  930. #define XYWH SkRect::MakeXYWH
  931. SkIRect stackBounds = IXYWH(0, 0, 76, 76);
  932. // The base test is to test each rect in two ways:
  933. // 1) The box dimensions. (Should reduce to "all in", no elements).
  934. // 2) A bit over the box dimensions.
  935. // In the case 2, test that the generation id is what is expected.
  936. // The rects are of fractional size so that case 2 never gets optimized to an empty element
  937. // list.
  938. // Not passing in tighter bounds is tested for consistency.
  939. static const struct SUPPRESS_VISIBILITY_WARNING {
  940. SkRect testBounds;
  941. int reducedClipCount;
  942. uint32_t reducedGenID;
  943. InitialState initialState;
  944. SkIRect clipIRect;
  945. // parameter.
  946. } testCases[] = {
  947. // Rect A.
  948. { XYWH(0, 0, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(0, 0, 25, 25) },
  949. { XYWH(0.1f, 0.1f, 25.1f, 25.1f), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(0, 0, 26, 26) },
  950. { XYWH(0, 0, 27, 27), 1, genIDA, GrReducedClip::InitialState::kAllOut, IXYWH(0, 0, 26, 26)},
  951. // Rect B.
  952. { XYWH(50, 0, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(50, 0, 25, 25) },
  953. { XYWH(50, 0, 25.3f, 25.3f), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(50, 0, 26, 26) },
  954. { XYWH(50, 0, 27, 27), 1, genIDB, GrReducedClip::InitialState::kAllOut, IXYWH(50, 0, 26, 27) },
  955. // Rect C.
  956. { XYWH(0, 50, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(0, 50, 25, 25) },
  957. { XYWH(0.2f, 50.1f, 25.1f, 25.2f), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(0, 50, 26, 26) },
  958. { XYWH(0, 50, 27, 27), 1, genIDC, GrReducedClip::InitialState::kAllOut, IXYWH(0, 50, 27, 26) },
  959. // Rect D.
  960. { XYWH(50, 50, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(50, 50, 25, 25)},
  961. { XYWH(50.3f, 50.3f, 25, 25), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllIn, IXYWH(50, 50, 26, 26)},
  962. { XYWH(50, 50, 27, 27), 1, genIDD, GrReducedClip::InitialState::kAllOut, IXYWH(50, 50, 26, 26)},
  963. // Other tests:
  964. { XYWH(0, 0, 100, 100), 4, genIDD, GrReducedClip::InitialState::kAllOut, stackBounds },
  965. // Rect in the middle, touches none.
  966. { XYWH(26, 26, 24, 24), 0, SkClipStack::kInvalidGenID, GrReducedClip::InitialState::kAllOut, IXYWH(26, 26, 24, 24) },
  967. // Rect in the middle, touches all the rects. GenID is the last rect.
  968. { XYWH(24, 24, 27, 27), 4, genIDD, GrReducedClip::InitialState::kAllOut, IXYWH(24, 24, 27, 27) },
  969. };
  970. #undef XYWH
  971. #undef IXYWH
  972. auto context = GrContext::MakeMock(nullptr);
  973. const GrCaps* caps = context->priv().caps();
  974. for (size_t i = 0; i < SK_ARRAY_COUNT(testCases); ++i) {
  975. const GrReducedClip reduced(stack, testCases[i].testBounds, caps);
  976. REPORTER_ASSERT(reporter, reduced.maskElements().count() ==
  977. testCases[i].reducedClipCount);
  978. SkASSERT(reduced.maskElements().count() == testCases[i].reducedClipCount);
  979. if (reduced.maskElements().count()) {
  980. REPORTER_ASSERT(reporter, reduced.maskGenID() == testCases[i].reducedGenID);
  981. SkASSERT(reduced.maskGenID() == testCases[i].reducedGenID);
  982. }
  983. REPORTER_ASSERT(reporter, reduced.initialState() == testCases[i].initialState);
  984. SkASSERT(reduced.initialState() == testCases[i].initialState);
  985. REPORTER_ASSERT(reporter, reduced.hasScissor());
  986. SkASSERT(reduced.hasScissor());
  987. REPORTER_ASSERT(reporter, reduced.scissor() == testCases[i].clipIRect);
  988. SkASSERT(reduced.scissor() == testCases[i].clipIRect);
  989. }
  990. }
  991. }
  992. static void test_reduced_clip_stack_no_aa_crash(skiatest::Reporter* reporter) {
  993. SkClipStack stack;
  994. stack.clipDevRect(SkIRect::MakeXYWH(0, 0, 100, 100), kReplace_SkClipOp);
  995. stack.clipDevRect(SkIRect::MakeXYWH(0, 0, 50, 50), kReplace_SkClipOp);
  996. SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100);
  997. auto context = GrContext::MakeMock(nullptr);
  998. const GrCaps* caps = context->priv().caps();
  999. // At the time, this would crash.
  1000. const GrReducedClip reduced(stack, bounds, caps);
  1001. REPORTER_ASSERT(reporter, reduced.maskElements().isEmpty());
  1002. }
  1003. enum class ClipMethod {
  1004. kSkipDraw,
  1005. kIgnoreClip,
  1006. kScissor,
  1007. kAAElements
  1008. };
  1009. static void test_aa_query(skiatest::Reporter* reporter, const SkString& testName,
  1010. const SkClipStack& stack, const SkMatrix& queryXform,
  1011. const SkRect& preXformQuery, ClipMethod expectedMethod,
  1012. int numExpectedElems = 0) {
  1013. auto context = GrContext::MakeMock(nullptr);
  1014. const GrCaps* caps = context->priv().caps();
  1015. SkRect queryBounds;
  1016. queryXform.mapRect(&queryBounds, preXformQuery);
  1017. const GrReducedClip reduced(stack, queryBounds, caps);
  1018. SkClipStack::BoundsType stackBoundsType;
  1019. SkRect stackBounds;
  1020. stack.getBounds(&stackBounds, &stackBoundsType);
  1021. switch (expectedMethod) {
  1022. case ClipMethod::kSkipDraw:
  1023. SkASSERT(0 == numExpectedElems);
  1024. REPORTER_ASSERT(reporter, reduced.maskElements().isEmpty(), testName.c_str());
  1025. REPORTER_ASSERT(reporter,
  1026. GrReducedClip::InitialState::kAllOut == reduced.initialState(),
  1027. testName.c_str());
  1028. return;
  1029. case ClipMethod::kIgnoreClip:
  1030. SkASSERT(0 == numExpectedElems);
  1031. REPORTER_ASSERT(
  1032. reporter,
  1033. !reduced.hasScissor() || GrClip::IsInsideClip(reduced.scissor(), queryBounds),
  1034. testName.c_str());
  1035. REPORTER_ASSERT(reporter, reduced.maskElements().isEmpty(), testName.c_str());
  1036. REPORTER_ASSERT(reporter,
  1037. GrReducedClip::InitialState::kAllIn == reduced.initialState(),
  1038. testName.c_str());
  1039. return;
  1040. case ClipMethod::kScissor: {
  1041. SkASSERT(SkClipStack::kNormal_BoundsType == stackBoundsType);
  1042. SkASSERT(0 == numExpectedElems);
  1043. SkIRect expectedScissor;
  1044. stackBounds.round(&expectedScissor);
  1045. REPORTER_ASSERT(reporter, reduced.maskElements().isEmpty(), testName.c_str());
  1046. REPORTER_ASSERT(reporter, reduced.hasScissor(), testName.c_str());
  1047. REPORTER_ASSERT(reporter, expectedScissor == reduced.scissor(), testName.c_str());
  1048. REPORTER_ASSERT(reporter,
  1049. GrReducedClip::InitialState::kAllIn == reduced.initialState(),
  1050. testName.c_str());
  1051. return;
  1052. }
  1053. case ClipMethod::kAAElements: {
  1054. SkIRect expectedClipIBounds = GrClip::GetPixelIBounds(queryBounds);
  1055. if (SkClipStack::kNormal_BoundsType == stackBoundsType) {
  1056. SkAssertResult(expectedClipIBounds.intersect(GrClip::GetPixelIBounds(stackBounds)));
  1057. }
  1058. REPORTER_ASSERT(reporter, numExpectedElems == reduced.maskElements().count(),
  1059. testName.c_str());
  1060. REPORTER_ASSERT(reporter, reduced.hasScissor(), testName.c_str());
  1061. REPORTER_ASSERT(reporter, expectedClipIBounds == reduced.scissor(), testName.c_str());
  1062. REPORTER_ASSERT(reporter,
  1063. reduced.maskElements().isEmpty() || reduced.maskRequiresAA(),
  1064. testName.c_str());
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. static void test_reduced_clip_stack_aa(skiatest::Reporter* reporter) {
  1070. constexpr SkScalar IL = 2, IT = 1, IR = 6, IB = 7; // Pixel aligned rect.
  1071. constexpr SkScalar L = 2.2f, T = 1.7f, R = 5.8f, B = 7.3f; // Generic rect.
  1072. constexpr SkScalar l = 3.3f, t = 2.8f, r = 4.7f, b = 6.2f; // Small rect contained in R.
  1073. SkRect alignedRect = {IL, IT, IR, IB};
  1074. SkRect rect = {L, T, R, B};
  1075. SkRect innerRect = {l, t, r, b};
  1076. SkMatrix m;
  1077. m.setIdentity();
  1078. constexpr SkScalar kMinScale = 2.0001f;
  1079. constexpr SkScalar kMaxScale = 3;
  1080. constexpr int kNumIters = 8;
  1081. SkString name;
  1082. SkRandom rand;
  1083. for (int i = 0; i < kNumIters; ++i) {
  1084. // Pixel-aligned rect (iior=true).
  1085. name.printf("Pixel-aligned rect test, iter %i", i);
  1086. SkClipStack stack;
  1087. stack.clipRect(alignedRect, SkMatrix::I(), kIntersect_SkClipOp, true);
  1088. test_aa_query(reporter, name, stack, m, {IL, IT, IR, IB}, ClipMethod::kIgnoreClip);
  1089. test_aa_query(reporter, name, stack, m, {IL, IT-1, IR, IT}, ClipMethod::kSkipDraw);
  1090. test_aa_query(reporter, name, stack, m, {IL, IT-2, IR, IB}, ClipMethod::kScissor);
  1091. // Rect (iior=true).
  1092. name.printf("Rect test, iter %i", i);
  1093. stack.reset();
  1094. stack.clipRect(rect, SkMatrix::I(), kIntersect_SkClipOp, true);
  1095. test_aa_query(reporter, name, stack, m, {L, T, R, B}, ClipMethod::kIgnoreClip);
  1096. test_aa_query(reporter, name, stack, m, {L-.1f, T, L, B}, ClipMethod::kSkipDraw);
  1097. test_aa_query(reporter, name, stack, m, {L-.1f, T, L+.1f, B}, ClipMethod::kAAElements, 1);
  1098. // Difference rect (iior=false, inside-out bounds).
  1099. name.printf("Difference rect test, iter %i", i);
  1100. stack.reset();
  1101. stack.clipRect(rect, SkMatrix::I(), kDifference_SkClipOp, true);
  1102. test_aa_query(reporter, name, stack, m, {L, T, R, B}, ClipMethod::kSkipDraw);
  1103. test_aa_query(reporter, name, stack, m, {L, T-.1f, R, T}, ClipMethod::kIgnoreClip);
  1104. test_aa_query(reporter, name, stack, m, {L, T-.1f, R, T+.1f}, ClipMethod::kAAElements, 1);
  1105. // Complex clip (iior=false, normal bounds).
  1106. name.printf("Complex clip test, iter %i", i);
  1107. stack.reset();
  1108. stack.clipRect(rect, SkMatrix::I(), kIntersect_SkClipOp, true);
  1109. stack.clipRect(innerRect, SkMatrix::I(), kXOR_SkClipOp, true);
  1110. test_aa_query(reporter, name, stack, m, {l, t, r, b}, ClipMethod::kSkipDraw);
  1111. test_aa_query(reporter, name, stack, m, {r-.1f, t, R, b}, ClipMethod::kAAElements, 1);
  1112. test_aa_query(reporter, name, stack, m, {r-.1f, t, R+.1f, b}, ClipMethod::kAAElements, 2);
  1113. test_aa_query(reporter, name, stack, m, {r, t, R+.1f, b}, ClipMethod::kAAElements, 1);
  1114. test_aa_query(reporter, name, stack, m, {r, t, R, b}, ClipMethod::kIgnoreClip);
  1115. test_aa_query(reporter, name, stack, m, {R, T, R+.1f, B}, ClipMethod::kSkipDraw);
  1116. // Complex clip where outer rect is pixel aligned (iior=false, normal bounds).
  1117. name.printf("Aligned Complex clip test, iter %i", i);
  1118. stack.reset();
  1119. stack.clipRect(alignedRect, SkMatrix::I(), kIntersect_SkClipOp, true);
  1120. stack.clipRect(innerRect, SkMatrix::I(), kXOR_SkClipOp, true);
  1121. test_aa_query(reporter, name, stack, m, {l, t, r, b}, ClipMethod::kSkipDraw);
  1122. test_aa_query(reporter, name, stack, m, {l, b-.1f, r, IB}, ClipMethod::kAAElements, 1);
  1123. test_aa_query(reporter, name, stack, m, {l, b-.1f, r, IB+.1f}, ClipMethod::kAAElements, 1);
  1124. test_aa_query(reporter, name, stack, m, {l, b, r, IB+.1f}, ClipMethod::kAAElements, 0);
  1125. test_aa_query(reporter, name, stack, m, {l, b, r, IB}, ClipMethod::kIgnoreClip);
  1126. test_aa_query(reporter, name, stack, m, {IL, IB, IR, IB+.1f}, ClipMethod::kSkipDraw);
  1127. // Apply random transforms and try again. This ensures the clip stack reduction is hardened
  1128. // against FP rounding error.
  1129. SkScalar sx = rand.nextRangeScalar(kMinScale, kMaxScale);
  1130. sx = SkScalarFloorToScalar(sx * alignedRect.width()) / alignedRect.width();
  1131. SkScalar sy = rand.nextRangeScalar(kMinScale, kMaxScale);
  1132. sy = SkScalarFloorToScalar(sy * alignedRect.height()) / alignedRect.height();
  1133. SkScalar tx = SkScalarRoundToScalar(sx * alignedRect.x()) - sx * alignedRect.x();
  1134. SkScalar ty = SkScalarRoundToScalar(sy * alignedRect.y()) - sy * alignedRect.y();
  1135. SkMatrix xform = SkMatrix::MakeScale(sx, sy);
  1136. xform.postTranslate(tx, ty);
  1137. xform.mapRect(&alignedRect);
  1138. xform.mapRect(&rect);
  1139. xform.mapRect(&innerRect);
  1140. m.postConcat(xform);
  1141. }
  1142. }
  1143. static void test_tiny_query_bounds_assertion_bug(skiatest::Reporter* reporter) {
  1144. // https://bugs.chromium.org/p/skia/issues/detail?id=5990
  1145. const SkRect clipBounds = SkRect::MakeXYWH(1.5f, 100, 1000, 1000);
  1146. SkClipStack rectStack;
  1147. rectStack.clipRect(clipBounds, SkMatrix::I(), kIntersect_SkClipOp, true);
  1148. SkPath clipPath;
  1149. clipPath.moveTo(clipBounds.left(), clipBounds.top());
  1150. clipPath.quadTo(clipBounds.right(), clipBounds.top(),
  1151. clipBounds.right(), clipBounds.bottom());
  1152. clipPath.quadTo(clipBounds.left(), clipBounds.bottom(),
  1153. clipBounds.left(), clipBounds.top());
  1154. SkClipStack pathStack;
  1155. pathStack.clipPath(clipPath, SkMatrix::I(), kIntersect_SkClipOp, true);
  1156. auto context = GrContext::MakeMock(nullptr);
  1157. const GrCaps* caps = context->priv().caps();
  1158. for (const SkClipStack& stack : {rectStack, pathStack}) {
  1159. for (SkRect queryBounds : {SkRect::MakeXYWH(53, 60, GrClip::kBoundsTolerance, 1000),
  1160. SkRect::MakeXYWH(53, 60, GrClip::kBoundsTolerance/2, 1000),
  1161. SkRect::MakeXYWH(53, 160, 1000, GrClip::kBoundsTolerance),
  1162. SkRect::MakeXYWH(53, 160, 1000, GrClip::kBoundsTolerance/2)}) {
  1163. const GrReducedClip reduced(stack, queryBounds, caps);
  1164. REPORTER_ASSERT(reporter, !reduced.hasScissor());
  1165. REPORTER_ASSERT(reporter, reduced.maskElements().isEmpty());
  1166. REPORTER_ASSERT(reporter,
  1167. GrReducedClip::InitialState::kAllOut == reduced.initialState());
  1168. }
  1169. }
  1170. }
  1171. static void test_is_rrect_deep_rect_stack(skiatest::Reporter* reporter) {
  1172. static constexpr SkRect kTargetBounds = SkRect::MakeWH(1000, 500);
  1173. // All antialiased or all not antialiased.
  1174. for (bool aa : {false, true}) {
  1175. SkClipStack stack;
  1176. for (int i = 0; i <= 100; ++i) {
  1177. stack.save();
  1178. stack.clipRect(SkRect::MakeLTRB(i, 0.5, kTargetBounds.width(), kTargetBounds.height()),
  1179. SkMatrix::I(), SkClipOp::kIntersect, aa);
  1180. }
  1181. SkRRect rrect;
  1182. bool isAA;
  1183. SkRRect expected = SkRRect::MakeRect(
  1184. SkRect::MakeLTRB(100, 0.5, kTargetBounds.width(), kTargetBounds.height()));
  1185. if (stack.isRRect(kTargetBounds, &rrect, &isAA)) {
  1186. REPORTER_ASSERT(reporter, rrect == expected);
  1187. REPORTER_ASSERT(reporter, aa == isAA);
  1188. } else {
  1189. ERRORF(reporter, "Expected to be an rrect.");
  1190. }
  1191. }
  1192. // Mixed AA and non-AA without simple containment.
  1193. SkClipStack stack;
  1194. for (int i = 0; i <= 100; ++i) {
  1195. bool aa = i & 0b1;
  1196. int j = 100 - i;
  1197. stack.save();
  1198. stack.clipRect(SkRect::MakeLTRB(i, j + 0.5, kTargetBounds.width(), kTargetBounds.height()),
  1199. SkMatrix::I(), SkClipOp::kIntersect, aa);
  1200. }
  1201. SkRRect rrect;
  1202. bool isAA;
  1203. REPORTER_ASSERT(reporter, !stack.isRRect(kTargetBounds, &rrect, &isAA));
  1204. }
  1205. DEF_TEST(ClipStack, reporter) {
  1206. SkClipStack stack;
  1207. REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
  1208. assert_count(reporter, stack, 0);
  1209. static const SkIRect gRects[] = {
  1210. { 0, 0, 100, 100 },
  1211. { 25, 25, 125, 125 },
  1212. { 0, 0, 1000, 1000 },
  1213. { 0, 0, 75, 75 }
  1214. };
  1215. for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
  1216. stack.clipDevRect(gRects[i], kIntersect_SkClipOp);
  1217. }
  1218. // all of the above rects should have been intersected, leaving only 1 rect
  1219. SkClipStack::B2TIter iter(stack);
  1220. const SkClipStack::Element* element = iter.next();
  1221. SkRect answer;
  1222. answer.iset(25, 25, 75, 75);
  1223. REPORTER_ASSERT(reporter, element);
  1224. REPORTER_ASSERT(reporter,
  1225. SkClipStack::Element::DeviceSpaceType::kRect == element->getDeviceSpaceType());
  1226. REPORTER_ASSERT(reporter, kIntersect_SkClipOp == element->getOp());
  1227. REPORTER_ASSERT(reporter, element->getDeviceSpaceRect() == answer);
  1228. // now check that we only had one in our iterator
  1229. REPORTER_ASSERT(reporter, !iter.next());
  1230. stack.reset();
  1231. REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
  1232. assert_count(reporter, stack, 0);
  1233. test_assign_and_comparison(reporter);
  1234. test_iterators(reporter);
  1235. test_bounds(reporter, SkClipStack::Element::DeviceSpaceType::kRect);
  1236. test_bounds(reporter, SkClipStack::Element::DeviceSpaceType::kRRect);
  1237. test_bounds(reporter, SkClipStack::Element::DeviceSpaceType::kPath);
  1238. test_isWideOpen(reporter);
  1239. test_rect_merging(reporter);
  1240. test_rect_replace(reporter);
  1241. test_rect_inverse_fill(reporter);
  1242. test_path_replace(reporter);
  1243. test_quickContains(reporter);
  1244. test_invfill_diff_bug(reporter);
  1245. test_reduced_clip_stack(reporter);
  1246. test_reduced_clip_stack_genid(reporter);
  1247. test_reduced_clip_stack_no_aa_crash(reporter);
  1248. test_reduced_clip_stack_aa(reporter);
  1249. test_tiny_query_bounds_assertion_bug(reporter);
  1250. test_is_rrect_deep_rect_stack(reporter);
  1251. }
  1252. //////////////////////////////////////////////////////////////////////////////
  1253. sk_sp<GrTextureProxy> GrClipStackClip::testingOnly_createClipMask(GrContext* context) const {
  1254. const GrReducedClip reducedClip(*fStack, SkRect::MakeWH(512, 512), 0);
  1255. return this->createSoftwareClipMask(context, reducedClip, nullptr);
  1256. }
  1257. // Verify that clip masks are freed up when the clip state that generated them goes away.
  1258. DEF_GPUTEST_FOR_ALL_CONTEXTS(ClipMaskCache, reporter, ctxInfo) {
  1259. // This test uses resource key tags which only function in debug builds.
  1260. #ifdef SK_DEBUG
  1261. GrContext* context = ctxInfo.grContext();
  1262. SkClipStack stack;
  1263. SkPath path;
  1264. path.addCircle(10, 10, 8);
  1265. path.addCircle(15, 15, 8);
  1266. path.setFillType(SkPath::kEvenOdd_FillType);
  1267. static const char* kTag = GrClipStackClip::kMaskTestTag;
  1268. GrResourceCache* cache = context->priv().getResourceCache();
  1269. static constexpr int kN = 5;
  1270. for (int i = 0; i < kN; ++i) {
  1271. SkMatrix m;
  1272. m.setTranslate(0.5, 0.5);
  1273. stack.save();
  1274. stack.clipPath(path, m, SkClipOp::kIntersect, true);
  1275. sk_sp<GrTextureProxy> mask = GrClipStackClip(&stack).testingOnly_createClipMask(context);
  1276. mask->instantiate(context->priv().resourceProvider());
  1277. GrTexture* tex = mask->peekTexture();
  1278. REPORTER_ASSERT(reporter, 0 == strcmp(tex->getUniqueKey().tag(), kTag));
  1279. // Make sure mask isn't pinned in cache.
  1280. mask.reset(nullptr);
  1281. context->flush();
  1282. REPORTER_ASSERT(reporter, i + 1 == cache->countUniqueKeysWithTag(kTag));
  1283. }
  1284. for (int i = 0; i < kN; ++i) {
  1285. stack.restore();
  1286. cache->purgeAsNeeded();
  1287. REPORTER_ASSERT(reporter, kN - (i + 1) == cache->countUniqueKeysWithTag(kTag));
  1288. }
  1289. #endif
  1290. }
  1291. DEF_GPUTEST_FOR_ALL_CONTEXTS(canvas_private_clipRgn, reporter, ctxInfo) {
  1292. GrContext* context = ctxInfo.grContext();
  1293. const int w = 10;
  1294. const int h = 10;
  1295. SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
  1296. sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
  1297. SkCanvas* canvas = surf->getCanvas();
  1298. SkRegion rgn;
  1299. canvas->temporary_internal_getRgnClip(&rgn);
  1300. REPORTER_ASSERT(reporter, rgn.isRect());
  1301. REPORTER_ASSERT(reporter, rgn.getBounds() == SkIRect::MakeWH(w, h));
  1302. canvas->save();
  1303. canvas->clipRect(SkRect::MakeWH(5, 5), kDifference_SkClipOp);
  1304. canvas->temporary_internal_getRgnClip(&rgn);
  1305. REPORTER_ASSERT(reporter, rgn.isComplex());
  1306. REPORTER_ASSERT(reporter, rgn.getBounds() == SkIRect::MakeWH(w, h));
  1307. canvas->restore();
  1308. canvas->save();
  1309. canvas->clipRRect(SkRRect::MakeOval(SkRect::MakeLTRB(3, 3, 7, 7)));
  1310. canvas->temporary_internal_getRgnClip(&rgn);
  1311. REPORTER_ASSERT(reporter, rgn.isComplex());
  1312. REPORTER_ASSERT(reporter, rgn.getBounds() == SkIRect::MakeLTRB(3, 3, 7, 7));
  1313. canvas->restore();
  1314. }