PathTest.cpp 216 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445
  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/SkFont.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/core/SkPathEffect.h"
  11. #include "include/core/SkRRect.h"
  12. #include "include/core/SkSize.h"
  13. #include "include/core/SkStream.h"
  14. #include "include/core/SkStrokeRec.h"
  15. #include "include/core/SkSurface.h"
  16. #include "include/private/SkTo.h"
  17. #include "include/utils/SkNullCanvas.h"
  18. #include "include/utils/SkParse.h"
  19. #include "include/utils/SkParsePath.h"
  20. #include "include/utils/SkRandom.h"
  21. #include "src/core/SkAutoMalloc.h"
  22. #include "src/core/SkGeometry.h"
  23. #include "src/core/SkPathPriv.h"
  24. #include "src/core/SkReader32.h"
  25. #include "src/core/SkWriter32.h"
  26. #include "tests/Test.h"
  27. #include <cmath>
  28. #include <utility>
  29. #include <vector>
  30. static void set_radii(SkVector radii[4], int index, float rad) {
  31. sk_bzero(radii, sizeof(SkVector) * 4);
  32. radii[index].set(rad, rad);
  33. }
  34. static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
  35. const SkVector radii[4]) {
  36. SkRRect rrect;
  37. rrect.setRectRadii(bounds, radii);
  38. REPORTER_ASSERT(reporter, bounds == rrect.rect());
  39. SkPath path;
  40. // this line should not assert in the debug build (from validate)
  41. path.addRRect(rrect);
  42. REPORTER_ASSERT(reporter, bounds == path.getBounds());
  43. }
  44. static void test_skbug_3469(skiatest::Reporter* reporter) {
  45. SkPath path;
  46. path.moveTo(20, 20);
  47. path.quadTo(20, 50, 80, 50);
  48. path.quadTo(20, 50, 20, 80);
  49. REPORTER_ASSERT(reporter, !path.isConvex());
  50. }
  51. static void test_skbug_3239(skiatest::Reporter* reporter) {
  52. const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
  53. const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
  54. const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
  55. const float rad = 33436320;
  56. const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
  57. const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
  58. SkVector radii[4];
  59. for (int i = 0; i < 4; ++i) {
  60. set_radii(radii, i, rad);
  61. test_add_rrect(reporter, rectx, radii);
  62. test_add_rrect(reporter, recty, radii);
  63. }
  64. }
  65. static void make_path_crbug364224(SkPath* path) {
  66. path->reset();
  67. path->moveTo(3.747501373f, 2.724499941f);
  68. path->lineTo(3.747501373f, 3.75f);
  69. path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
  70. path->lineTo(0.7475013733f, 4.0f);
  71. path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
  72. path->lineTo(0.4975013733f, 1.0f);
  73. path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
  74. path->lineTo(3.497501373f, 0.75f);
  75. path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
  76. path->lineTo(3.715001345f, 0.5512499809f);
  77. path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
  78. path->lineTo(0.7475013733f, 0.4999999702f);
  79. path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
  80. path->lineTo(0.2475013733f, 3.75f);
  81. path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
  82. path->lineTo(3.497501373f, 4.25f);
  83. path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
  84. path->lineTo(3.997501373f, 2.474750042f);
  85. path->lineTo(3.747501373f, 2.724499941f);
  86. path->close();
  87. }
  88. static void make_path_crbug364224_simplified(SkPath* path) {
  89. path->moveTo(3.747501373f, 2.724499941f);
  90. path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
  91. path->close();
  92. }
  93. static void test_sect_with_horizontal_needs_pinning() {
  94. // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
  95. // intersection.
  96. SkPath path;
  97. path.reset();
  98. path.moveTo(-540000, -720000);
  99. path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
  100. path.lineTo(1, 1);
  101. // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
  102. SkPaint paint;
  103. SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
  104. }
  105. static void test_path_crbug364224() {
  106. SkPath path;
  107. SkPaint paint;
  108. auto surface(SkSurface::MakeRasterN32Premul(84, 88));
  109. SkCanvas* canvas = surface->getCanvas();
  110. make_path_crbug364224_simplified(&path);
  111. canvas->drawPath(path, paint);
  112. make_path_crbug364224(&path);
  113. canvas->drawPath(path, paint);
  114. }
  115. static void test_draw_AA_path(int width, int height, const SkPath& path) {
  116. auto surface(SkSurface::MakeRasterN32Premul(width, height));
  117. SkCanvas* canvas = surface->getCanvas();
  118. SkPaint paint;
  119. paint.setAntiAlias(true);
  120. canvas->drawPath(path, paint);
  121. }
  122. // this is a unit test instead of a GM because it doesn't draw anything
  123. static void test_fuzz_crbug_638223() {
  124. SkPath path;
  125. path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f
  126. path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
  127. SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
  128. SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
  129. test_draw_AA_path(250, 250, path);
  130. }
  131. static void test_fuzz_crbug_643933() {
  132. SkPath path;
  133. path.moveTo(0, 0);
  134. path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f
  135. SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
  136. SkBits2Float(0x721aee0c)); // 3.0687e+30f
  137. test_draw_AA_path(250, 250, path);
  138. path.reset();
  139. path.moveTo(0, 0);
  140. path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f
  141. SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f
  142. SkBits2Float(0x5d06eff8)); // 6.07704e+17f
  143. test_draw_AA_path(250, 250, path);
  144. }
  145. static void test_fuzz_crbug_647922() {
  146. SkPath path;
  147. path.moveTo(0, 0);
  148. path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff), // 2.05276e-41f, 50.125f
  149. SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9), // 139406, 547.639f
  150. SkBits2Float(0x4d1ade0f)); // 1.6239e+08f
  151. test_draw_AA_path(250, 250, path);
  152. }
  153. static void test_fuzz_crbug_662780() {
  154. auto surface(SkSurface::MakeRasterN32Premul(250, 250));
  155. SkCanvas* canvas = surface->getCanvas();
  156. SkPaint paint;
  157. paint.setAntiAlias(true);
  158. SkPath path;
  159. path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
  160. path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000)); // 8, 120
  161. // 8, 8, 8.00002f, 8, 0.707107f
  162. path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000),
  163. SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
  164. path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000)); // 308, 8
  165. // 308, 8, 308, 8, 0.707107f
  166. path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000),
  167. SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
  168. path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000)); // 308, 158
  169. // 308, 158, 308, 158, 0.707107f
  170. path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000),
  171. SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
  172. path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
  173. // 8, 158, 8, 158, 0.707107f
  174. path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000),
  175. SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
  176. path.close();
  177. canvas->clipPath(path, true);
  178. canvas->drawRect(SkRect::MakeWH(250, 250), paint);
  179. }
  180. static void test_mask_overflow() {
  181. SkPath path;
  182. path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
  183. path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000)); // 444.75f, 341
  184. // 440.47f, 341, 437, 344.47f, 437, 348.75f
  185. path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000),
  186. SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29),
  187. SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000));
  188. path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000)); // 437, 355
  189. path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000)); // 453, 355
  190. path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
  191. test_draw_AA_path(500, 500, path);
  192. }
  193. static void test_fuzz_crbug_668907() {
  194. SkPath path;
  195. path.moveTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
  196. path.quadTo(SkBits2Float(0x41410041), SkBits2Float(0xc1414141), SkBits2Float(0x41414141),
  197. SkBits2Float(0x414100ff)); // 12.0626f, -12.0784f, 12.0784f, 12.0627f
  198. path.lineTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
  199. path.close();
  200. test_draw_AA_path(400, 500, path);
  201. }
  202. /**
  203. * In debug mode, this path was causing an assertion to fail in
  204. * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
  205. */
  206. static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
  207. SkPoint orig, p1, p2, p3;
  208. orig = SkPoint::Make(1.f, 1.f);
  209. p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
  210. p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
  211. p3 = SkPoint::Make(2.f, 2.f);
  212. path->reset();
  213. path->moveTo(orig);
  214. path->cubicTo(p1, p2, p3);
  215. path->close();
  216. }
  217. static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
  218. SkPath path;
  219. make_path_crbugskia2820(&path, reporter);
  220. SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
  221. stroke.setStrokeStyle(2 * SK_Scalar1);
  222. stroke.applyToPath(&path, path);
  223. }
  224. static void test_path_crbugskia5995() {
  225. SkPath path;
  226. path.moveTo(SkBits2Float(0x40303030), SkBits2Float(0x3e303030)); // 2.75294f, 0.172059f
  227. path.quadTo(SkBits2Float(0x41d63030), SkBits2Float(0x30303030), SkBits2Float(0x41013030),
  228. SkBits2Float(0x00000000)); // 26.7735f, 6.40969e-10f, 8.07426f, 0
  229. path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
  230. test_draw_AA_path(500, 500, path);
  231. }
  232. static void make_path0(SkPath* path) {
  233. // from * https://code.google.com/p/skia/issues/detail?id=1706
  234. path->moveTo(146.939f, 1012.84f);
  235. path->lineTo(181.747f, 1009.18f);
  236. path->lineTo(182.165f, 1013.16f);
  237. path->lineTo(147.357f, 1016.82f);
  238. path->lineTo(146.939f, 1012.84f);
  239. path->close();
  240. }
  241. static void make_path1(SkPath* path) {
  242. path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
  243. }
  244. typedef void (*PathProc)(SkPath*);
  245. /*
  246. * Regression test: we used to crash (overwrite internal storage) during
  247. * construction of the region when the path was INVERSE. That is now fixed,
  248. * so test these regions (which used to assert/crash).
  249. *
  250. * https://code.google.com/p/skia/issues/detail?id=1706
  251. */
  252. static void test_path_to_region(skiatest::Reporter* reporter) {
  253. PathProc procs[] = {
  254. make_path0,
  255. make_path1,
  256. };
  257. SkRegion clip;
  258. clip.setRect(0, 0, 1255, 1925);
  259. for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
  260. SkPath path;
  261. procs[i](&path);
  262. SkRegion rgn;
  263. rgn.setPath(path, clip);
  264. path.toggleInverseFillType();
  265. rgn.setPath(path, clip);
  266. }
  267. }
  268. #ifdef SK_BUILD_FOR_WIN
  269. #define SUPPRESS_VISIBILITY_WARNING
  270. #else
  271. #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
  272. #endif
  273. static void test_path_close_issue1474(skiatest::Reporter* reporter) {
  274. // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
  275. // are relative to the point we close to, not relative to the point we close from.
  276. SkPath path;
  277. SkPoint last;
  278. // Test rLineTo().
  279. path.rLineTo(0, 100);
  280. path.rLineTo(100, 0);
  281. path.close(); // Returns us back to 0,0.
  282. path.rLineTo(50, 50); // This should go to 50,50.
  283. path.getLastPt(&last);
  284. REPORTER_ASSERT(reporter, 50 == last.fX);
  285. REPORTER_ASSERT(reporter, 50 == last.fY);
  286. // Test rQuadTo().
  287. path.rewind();
  288. path.rLineTo(0, 100);
  289. path.rLineTo(100, 0);
  290. path.close();
  291. path.rQuadTo(50, 50, 75, 75);
  292. path.getLastPt(&last);
  293. REPORTER_ASSERT(reporter, 75 == last.fX);
  294. REPORTER_ASSERT(reporter, 75 == last.fY);
  295. // Test rConicTo().
  296. path.rewind();
  297. path.rLineTo(0, 100);
  298. path.rLineTo(100, 0);
  299. path.close();
  300. path.rConicTo(50, 50, 85, 85, 2);
  301. path.getLastPt(&last);
  302. REPORTER_ASSERT(reporter, 85 == last.fX);
  303. REPORTER_ASSERT(reporter, 85 == last.fY);
  304. // Test rCubicTo().
  305. path.rewind();
  306. path.rLineTo(0, 100);
  307. path.rLineTo(100, 0);
  308. path.close();
  309. path.rCubicTo(50, 50, 85, 85, 95, 95);
  310. path.getLastPt(&last);
  311. REPORTER_ASSERT(reporter, 95 == last.fX);
  312. REPORTER_ASSERT(reporter, 95 == last.fY);
  313. }
  314. static void test_gen_id(skiatest::Reporter* reporter) {
  315. SkPath a, b;
  316. REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
  317. a.moveTo(0, 0);
  318. const uint32_t z = a.getGenerationID();
  319. REPORTER_ASSERT(reporter, z != b.getGenerationID());
  320. a.reset();
  321. REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
  322. a.moveTo(1, 1);
  323. const uint32_t y = a.getGenerationID();
  324. REPORTER_ASSERT(reporter, z != y);
  325. b.moveTo(2, 2);
  326. const uint32_t x = b.getGenerationID();
  327. REPORTER_ASSERT(reporter, x != y && x != z);
  328. a.swap(b);
  329. REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
  330. b = a;
  331. REPORTER_ASSERT(reporter, b.getGenerationID() == x);
  332. SkPath c(a);
  333. REPORTER_ASSERT(reporter, c.getGenerationID() == x);
  334. c.lineTo(3, 3);
  335. const uint32_t w = c.getGenerationID();
  336. REPORTER_ASSERT(reporter, b.getGenerationID() == x);
  337. REPORTER_ASSERT(reporter, a.getGenerationID() == x);
  338. REPORTER_ASSERT(reporter, w != x);
  339. #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
  340. static bool kExpectGenIDToIgnoreFill = false;
  341. #else
  342. static bool kExpectGenIDToIgnoreFill = true;
  343. #endif
  344. c.toggleInverseFillType();
  345. const uint32_t v = c.getGenerationID();
  346. REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
  347. c.rewind();
  348. REPORTER_ASSERT(reporter, v != c.getGenerationID());
  349. }
  350. // This used to assert in the debug build, as the edges did not all line-up.
  351. static void test_bad_cubic_crbug234190() {
  352. SkPath path;
  353. path.moveTo(13.8509f, 3.16858f);
  354. path.cubicTo(-2.35893e+08f, -4.21044e+08f,
  355. -2.38991e+08f, -4.26573e+08f,
  356. -2.41016e+08f, -4.30188e+08f);
  357. test_draw_AA_path(84, 88, path);
  358. }
  359. static void test_bad_cubic_crbug229478() {
  360. const SkPoint pts[] = {
  361. { 4595.91064f, -11596.9873f },
  362. { 4597.2168f, -11595.9414f },
  363. { 4598.52344f, -11594.8955f },
  364. { 4599.83008f, -11593.8496f },
  365. };
  366. SkPath path;
  367. path.moveTo(pts[0]);
  368. path.cubicTo(pts[1], pts[2], pts[3]);
  369. SkPaint paint;
  370. paint.setStyle(SkPaint::kStroke_Style);
  371. paint.setStrokeWidth(20);
  372. SkPath dst;
  373. // Before the fix, this would infinite-recurse, and run out of stack
  374. // because we would keep trying to subdivide a degenerate cubic segment.
  375. paint.getFillPath(path, &dst, nullptr);
  376. }
  377. static void build_path_170666(SkPath& path) {
  378. path.moveTo(17.9459f, 21.6344f);
  379. path.lineTo(139.545f, -47.8105f);
  380. path.lineTo(139.545f, -47.8105f);
  381. path.lineTo(131.07f, -47.3888f);
  382. path.lineTo(131.07f, -47.3888f);
  383. path.lineTo(122.586f, -46.9532f);
  384. path.lineTo(122.586f, -46.9532f);
  385. path.lineTo(18076.6f, 31390.9f);
  386. path.lineTo(18076.6f, 31390.9f);
  387. path.lineTo(18085.1f, 31390.5f);
  388. path.lineTo(18085.1f, 31390.5f);
  389. path.lineTo(18076.6f, 31390.9f);
  390. path.lineTo(18076.6f, 31390.9f);
  391. path.lineTo(17955, 31460.3f);
  392. path.lineTo(17955, 31460.3f);
  393. path.lineTo(17963.5f, 31459.9f);
  394. path.lineTo(17963.5f, 31459.9f);
  395. path.lineTo(17971.9f, 31459.5f);
  396. path.lineTo(17971.9f, 31459.5f);
  397. path.lineTo(17.9551f, 21.6205f);
  398. path.lineTo(17.9551f, 21.6205f);
  399. path.lineTo(9.47091f, 22.0561f);
  400. path.lineTo(9.47091f, 22.0561f);
  401. path.lineTo(17.9459f, 21.6344f);
  402. path.lineTo(17.9459f, 21.6344f);
  403. path.close();path.moveTo(0.995934f, 22.4779f);
  404. path.lineTo(0.986725f, 22.4918f);
  405. path.lineTo(0.986725f, 22.4918f);
  406. path.lineTo(17955, 31460.4f);
  407. path.lineTo(17955, 31460.4f);
  408. path.lineTo(17971.9f, 31459.5f);
  409. path.lineTo(17971.9f, 31459.5f);
  410. path.lineTo(18093.6f, 31390.1f);
  411. path.lineTo(18093.6f, 31390.1f);
  412. path.lineTo(18093.6f, 31390);
  413. path.lineTo(18093.6f, 31390);
  414. path.lineTo(139.555f, -47.8244f);
  415. path.lineTo(139.555f, -47.8244f);
  416. path.lineTo(122.595f, -46.9671f);
  417. path.lineTo(122.595f, -46.9671f);
  418. path.lineTo(0.995934f, 22.4779f);
  419. path.lineTo(0.995934f, 22.4779f);
  420. path.close();
  421. path.moveTo(5.43941f, 25.5223f);
  422. path.lineTo(798267, -28871.1f);
  423. path.lineTo(798267, -28871.1f);
  424. path.lineTo(3.12512e+06f, -113102);
  425. path.lineTo(3.12512e+06f, -113102);
  426. path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
  427. path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
  428. path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
  429. path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
  430. path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
  431. path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
  432. path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
  433. path.lineTo(2.78271e+08f, -1.00733e+07f);
  434. path.lineTo(2.78271e+08f, -1.00733e+07f);
  435. path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
  436. path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
  437. path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
  438. path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
  439. path.lineTo(2.77473e+08f, -1.00444e+07f);
  440. path.lineTo(2.77473e+08f, -1.00444e+07f);
  441. path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
  442. path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
  443. path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
  444. path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
  445. path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
  446. path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
  447. path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
  448. path.lineTo(798284, -28872);
  449. path.lineTo(798284, -28872);
  450. path.lineTo(22.4044f, 24.6677f);
  451. path.lineTo(22.4044f, 24.6677f);
  452. path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
  453. path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
  454. path.close();
  455. }
  456. static void build_path_simple_170666(SkPath& path) {
  457. path.moveTo(126.677f, 24.1591f);
  458. path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
  459. }
  460. // This used to assert in the SK_DEBUG build, as the clip step would fail with
  461. // too-few interations in our cubic-line intersection code. That code now runs
  462. // 24 interations (instead of 16).
  463. static void test_crbug_170666() {
  464. SkPath path;
  465. build_path_simple_170666(path);
  466. test_draw_AA_path(1000, 1000, path);
  467. build_path_170666(path);
  468. test_draw_AA_path(1000, 1000, path);
  469. }
  470. static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
  471. SkScalar tx, SkScalar ty, SkScalar scale) {
  472. SkPath smallPath;
  473. SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
  474. bool smallConvex = smallPath.isConvex();
  475. SkPath largePath;
  476. SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
  477. SkMatrix matrix;
  478. matrix.reset();
  479. matrix.preTranslate(100, 100);
  480. matrix.preScale(scale, scale);
  481. largePath.transform(matrix);
  482. bool largeConvex = largePath.isConvex();
  483. REPORTER_ASSERT(reporter, smallConvex == largeConvex);
  484. }
  485. static void test_crbug_493450(skiatest::Reporter* reporter) {
  486. const char reducedCase[] =
  487. "M0,0"
  488. "L0.0002, 0"
  489. "L0.0002, 0.0002"
  490. "L0.0001, 0.0001"
  491. "L0,0.0002"
  492. "Z";
  493. test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
  494. const char originalFiddleData[] =
  495. "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
  496. "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
  497. "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
  498. "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
  499. "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
  500. test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
  501. 826357.3384828606f);
  502. }
  503. static void test_crbug_495894(skiatest::Reporter* reporter) {
  504. const char originalFiddleData[] =
  505. "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
  506. "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
  507. "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
  508. "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
  509. "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
  510. "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
  511. "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
  512. "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
  513. "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
  514. "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
  515. "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
  516. "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
  517. "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
  518. "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
  519. "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
  520. "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
  521. "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
  522. "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
  523. "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
  524. "L-0.33993994441916414,-0.11288906492739594Z";
  525. test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
  526. 65536);
  527. }
  528. static void test_crbug_613918() {
  529. SkPath path;
  530. path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
  531. path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
  532. path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
  533. // This call could lead to an assert or uninitialized read due to a failure
  534. // to check the return value from SkCubicClipper::ChopMonoAtY.
  535. path.contains(-1.84817e-08f, 1.15465e-08f);
  536. }
  537. static void test_addrect(skiatest::Reporter* reporter) {
  538. SkPath path;
  539. path.lineTo(0, 0);
  540. path.addRect(SkRect::MakeWH(50, 100));
  541. REPORTER_ASSERT(reporter, path.isRect(nullptr));
  542. path.reset();
  543. path.lineTo(FLT_EPSILON, FLT_EPSILON);
  544. path.addRect(SkRect::MakeWH(50, 100));
  545. REPORTER_ASSERT(reporter, !path.isRect(nullptr));
  546. path.reset();
  547. path.quadTo(0, 0, 0, 0);
  548. path.addRect(SkRect::MakeWH(50, 100));
  549. REPORTER_ASSERT(reporter, !path.isRect(nullptr));
  550. path.reset();
  551. path.conicTo(0, 0, 0, 0, 0.5f);
  552. path.addRect(SkRect::MakeWH(50, 100));
  553. REPORTER_ASSERT(reporter, !path.isRect(nullptr));
  554. path.reset();
  555. path.cubicTo(0, 0, 0, 0, 0, 0);
  556. path.addRect(SkRect::MakeWH(50, 100));
  557. REPORTER_ASSERT(reporter, !path.isRect(nullptr));
  558. }
  559. // Make sure we stay non-finite once we get there (unless we reset or rewind).
  560. static void test_addrect_isfinite(skiatest::Reporter* reporter) {
  561. SkPath path;
  562. path.addRect(SkRect::MakeWH(50, 100));
  563. REPORTER_ASSERT(reporter, path.isFinite());
  564. path.moveTo(0, 0);
  565. path.lineTo(SK_ScalarInfinity, 42);
  566. REPORTER_ASSERT(reporter, !path.isFinite());
  567. path.addRect(SkRect::MakeWH(50, 100));
  568. REPORTER_ASSERT(reporter, !path.isFinite());
  569. path.reset();
  570. REPORTER_ASSERT(reporter, path.isFinite());
  571. path.addRect(SkRect::MakeWH(50, 100));
  572. REPORTER_ASSERT(reporter, path.isFinite());
  573. }
  574. static void build_big_path(SkPath* path, bool reducedCase) {
  575. if (reducedCase) {
  576. path->moveTo(577330, 1971.72f);
  577. path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
  578. } else {
  579. path->moveTo(60.1631f, 7.70567f);
  580. path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
  581. path->lineTo(577379, 1977.77f);
  582. path->quadTo(577364, 1979.57f, 577325, 1980.26f);
  583. path->quadTo(577286, 1980.95f, 577245, 1980.13f);
  584. path->quadTo(577205, 1979.3f, 577187, 1977.45f);
  585. path->quadTo(577168, 1975.6f, 577183, 1973.8f);
  586. path->quadTo(577198, 1972, 577238, 1971.31f);
  587. path->quadTo(577277, 1970.62f, 577317, 1971.45f);
  588. path->quadTo(577330, 1971.72f, 577341, 1972.11f);
  589. path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
  590. path->moveTo(306.718f, -32.912f);
  591. path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
  592. }
  593. }
  594. static void test_clipped_cubic() {
  595. auto surface(SkSurface::MakeRasterN32Premul(640, 480));
  596. // This path used to assert, because our cubic-chopping code incorrectly
  597. // moved control points after the chop. This test should be run in SK_DEBUG
  598. // mode to ensure that we no long assert.
  599. SkPath path;
  600. for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
  601. build_big_path(&path, SkToBool(doReducedCase));
  602. SkPaint paint;
  603. for (int doAA = 0; doAA <= 1; ++doAA) {
  604. paint.setAntiAlias(SkToBool(doAA));
  605. surface->getCanvas()->drawPath(path, paint);
  606. }
  607. }
  608. }
  609. static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
  610. if (expected != bounds) {
  611. ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
  612. bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
  613. expected.left(), expected.top(), expected.right(), expected.bottom());
  614. }
  615. }
  616. static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
  617. SkPath path;
  618. #if 0
  619. // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
  620. // rewritten them to avoid this (compiler-bug?).
  621. REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
  622. path.moveTo(-5, -8);
  623. REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
  624. path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
  625. REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
  626. path.moveTo(1, 2);
  627. REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
  628. #else
  629. dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
  630. path.moveTo(-5, -8); // should set the bounds
  631. dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
  632. path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
  633. dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
  634. path.moveTo(1, 2); // don't expect this to have changed the bounds
  635. dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
  636. #endif
  637. }
  638. #include "include/core/SkSurface.h"
  639. static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
  640. SkPath path;
  641. path.moveTo(0, 0);
  642. path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
  643. test_draw_AA_path(100, 100, path);
  644. }
  645. // Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
  646. // which triggered an assert, from a tricky cubic. This test replicates that
  647. // example, so we can ensure that we handle it (in SkEdge.cpp), and don't
  648. // assert in the SK_DEBUG build.
  649. static void test_tricky_cubic() {
  650. const SkPoint pts[] = {
  651. { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
  652. { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
  653. { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
  654. { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
  655. };
  656. SkPath path;
  657. path.moveTo(pts[0]);
  658. path.cubicTo(pts[1], pts[2], pts[3]);
  659. test_draw_AA_path(19, 130, path);
  660. }
  661. // Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
  662. //
  663. static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
  664. SkPath path;
  665. path.quadTo(157, 366, 286, 208);
  666. path.arcTo(37, 442, 315, 163, 957494590897113.0f);
  667. SkMatrix matrix;
  668. matrix.setScale(1000*1000, 1000*1000);
  669. // Be sure that path::transform correctly updates isFinite and the bounds
  670. // if the transformation overflows. The previous bug was that isFinite was
  671. // set to true in this case, but the bounds were not set to empty (which
  672. // they should be).
  673. while (path.isFinite()) {
  674. REPORTER_ASSERT(reporter, path.getBounds().isFinite());
  675. REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
  676. path.transform(matrix);
  677. }
  678. REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
  679. matrix.setTranslate(SK_Scalar1, SK_Scalar1);
  680. path.transform(matrix);
  681. // we need to still be non-finite
  682. REPORTER_ASSERT(reporter, !path.isFinite());
  683. REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
  684. }
  685. static void add_corner_arc(SkPath* path, const SkRect& rect,
  686. SkScalar xIn, SkScalar yIn,
  687. int startAngle)
  688. {
  689. SkScalar rx = SkMinScalar(rect.width(), xIn);
  690. SkScalar ry = SkMinScalar(rect.height(), yIn);
  691. SkRect arcRect;
  692. arcRect.set(-rx, -ry, rx, ry);
  693. switch (startAngle) {
  694. case 0:
  695. arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
  696. break;
  697. case 90:
  698. arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
  699. break;
  700. case 180:
  701. arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
  702. break;
  703. case 270:
  704. arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
  705. break;
  706. default:
  707. break;
  708. }
  709. path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
  710. }
  711. static void make_arb_round_rect(SkPath* path, const SkRect& r,
  712. SkScalar xCorner, SkScalar yCorner) {
  713. // we are lazy here and use the same x & y for each corner
  714. add_corner_arc(path, r, xCorner, yCorner, 270);
  715. add_corner_arc(path, r, xCorner, yCorner, 0);
  716. add_corner_arc(path, r, xCorner, yCorner, 90);
  717. add_corner_arc(path, r, xCorner, yCorner, 180);
  718. path->close();
  719. }
  720. // Chrome creates its own round rects with each corner possibly being different.
  721. // Performance will suffer if they are not convex.
  722. // Note: PathBench::ArbRoundRectBench performs almost exactly
  723. // the same test (but with drawing)
  724. static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
  725. SkRandom rand;
  726. SkRect r;
  727. for (int i = 0; i < 5000; ++i) {
  728. SkScalar size = rand.nextUScalar1() * 30;
  729. if (size < SK_Scalar1) {
  730. continue;
  731. }
  732. r.fLeft = rand.nextUScalar1() * 300;
  733. r.fTop = rand.nextUScalar1() * 300;
  734. r.fRight = r.fLeft + 2 * size;
  735. r.fBottom = r.fTop + 2 * size;
  736. SkPath temp;
  737. make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
  738. REPORTER_ASSERT(reporter, temp.isConvex());
  739. }
  740. }
  741. // Chrome will sometimes create a 0 radius round rect. The degenerate
  742. // quads prevent the path from being converted to a rect
  743. // Note: PathBench::ArbRoundRectBench performs almost exactly
  744. // the same test (but with drawing)
  745. static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
  746. SkRandom rand;
  747. SkRect r;
  748. for (int i = 0; i < 5000; ++i) {
  749. SkScalar size = rand.nextUScalar1() * 30;
  750. if (size < SK_Scalar1) {
  751. continue;
  752. }
  753. r.fLeft = rand.nextUScalar1() * 300;
  754. r.fTop = rand.nextUScalar1() * 300;
  755. r.fRight = r.fLeft + 2 * size;
  756. r.fBottom = r.fTop + 2 * size;
  757. SkPath temp;
  758. make_arb_round_rect(&temp, r, 0, 0);
  759. SkRect result;
  760. REPORTER_ASSERT(reporter, temp.isRect(&result));
  761. REPORTER_ASSERT(reporter, r == result);
  762. }
  763. }
  764. static void test_rect_isfinite(skiatest::Reporter* reporter) {
  765. const SkScalar inf = SK_ScalarInfinity;
  766. const SkScalar negInf = SK_ScalarNegativeInfinity;
  767. const SkScalar nan = SK_ScalarNaN;
  768. SkRect r;
  769. r.setEmpty();
  770. REPORTER_ASSERT(reporter, r.isFinite());
  771. r.set(0, 0, inf, negInf);
  772. REPORTER_ASSERT(reporter, !r.isFinite());
  773. r.set(0, 0, nan, 0);
  774. REPORTER_ASSERT(reporter, !r.isFinite());
  775. SkPoint pts[] = {
  776. { 0, 0 },
  777. { SK_Scalar1, 0 },
  778. { 0, SK_Scalar1 },
  779. };
  780. bool isFine = r.setBoundsCheck(pts, 3);
  781. REPORTER_ASSERT(reporter, isFine);
  782. REPORTER_ASSERT(reporter, !r.isEmpty());
  783. pts[1].set(inf, 0);
  784. isFine = r.setBoundsCheck(pts, 3);
  785. REPORTER_ASSERT(reporter, !isFine);
  786. REPORTER_ASSERT(reporter, r.isEmpty());
  787. pts[1].set(nan, 0);
  788. isFine = r.setBoundsCheck(pts, 3);
  789. REPORTER_ASSERT(reporter, !isFine);
  790. REPORTER_ASSERT(reporter, r.isEmpty());
  791. }
  792. static void test_path_isfinite(skiatest::Reporter* reporter) {
  793. const SkScalar inf = SK_ScalarInfinity;
  794. const SkScalar negInf = SK_ScalarNegativeInfinity;
  795. const SkScalar nan = SK_ScalarNaN;
  796. SkPath path;
  797. REPORTER_ASSERT(reporter, path.isFinite());
  798. path.reset();
  799. REPORTER_ASSERT(reporter, path.isFinite());
  800. path.reset();
  801. path.moveTo(SK_Scalar1, 0);
  802. REPORTER_ASSERT(reporter, path.isFinite());
  803. path.reset();
  804. path.moveTo(inf, negInf);
  805. REPORTER_ASSERT(reporter, !path.isFinite());
  806. path.reset();
  807. path.moveTo(nan, 0);
  808. REPORTER_ASSERT(reporter, !path.isFinite());
  809. }
  810. static void test_isfinite(skiatest::Reporter* reporter) {
  811. test_rect_isfinite(reporter);
  812. test_path_isfinite(reporter);
  813. }
  814. static void test_islastcontourclosed(skiatest::Reporter* reporter) {
  815. SkPath path;
  816. REPORTER_ASSERT(reporter, !path.isLastContourClosed());
  817. path.moveTo(0, 0);
  818. REPORTER_ASSERT(reporter, !path.isLastContourClosed());
  819. path.close();
  820. REPORTER_ASSERT(reporter, path.isLastContourClosed());
  821. path.lineTo(100, 100);
  822. REPORTER_ASSERT(reporter, !path.isLastContourClosed());
  823. path.moveTo(200, 200);
  824. REPORTER_ASSERT(reporter, !path.isLastContourClosed());
  825. path.close();
  826. REPORTER_ASSERT(reporter, path.isLastContourClosed());
  827. path.moveTo(0, 0);
  828. REPORTER_ASSERT(reporter, !path.isLastContourClosed());
  829. }
  830. // assert that we always
  831. // start with a moveTo
  832. // only have 1 moveTo
  833. // only have Lines after that
  834. // end with a single close
  835. // only have (at most) 1 close
  836. //
  837. static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
  838. const SkPoint srcPts[], bool expectClose) {
  839. SkPath::RawIter iter(path);
  840. SkPoint pts[4];
  841. bool firstTime = true;
  842. bool foundClose = false;
  843. for (;;) {
  844. switch (iter.next(pts)) {
  845. case SkPath::kMove_Verb:
  846. REPORTER_ASSERT(reporter, firstTime);
  847. REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
  848. srcPts++;
  849. firstTime = false;
  850. break;
  851. case SkPath::kLine_Verb:
  852. REPORTER_ASSERT(reporter, !firstTime);
  853. REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
  854. srcPts++;
  855. break;
  856. case SkPath::kQuad_Verb:
  857. REPORTER_ASSERT(reporter, false, "unexpected quad verb");
  858. break;
  859. case SkPath::kConic_Verb:
  860. REPORTER_ASSERT(reporter, false, "unexpected conic verb");
  861. break;
  862. case SkPath::kCubic_Verb:
  863. REPORTER_ASSERT(reporter, false, "unexpected cubic verb");
  864. break;
  865. case SkPath::kClose_Verb:
  866. REPORTER_ASSERT(reporter, !firstTime);
  867. REPORTER_ASSERT(reporter, !foundClose);
  868. REPORTER_ASSERT(reporter, expectClose);
  869. foundClose = true;
  870. break;
  871. case SkPath::kDone_Verb:
  872. goto DONE;
  873. }
  874. }
  875. DONE:
  876. REPORTER_ASSERT(reporter, foundClose == expectClose);
  877. }
  878. static void test_addPoly(skiatest::Reporter* reporter) {
  879. SkPoint pts[32];
  880. SkRandom rand;
  881. for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
  882. pts[i].fX = rand.nextSScalar1();
  883. pts[i].fY = rand.nextSScalar1();
  884. }
  885. for (int doClose = 0; doClose <= 1; ++doClose) {
  886. for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
  887. SkPath path;
  888. path.addPoly(pts, SkToInt(count), SkToBool(doClose));
  889. test_poly(reporter, path, pts, SkToBool(doClose));
  890. }
  891. }
  892. }
  893. static void test_strokerec(skiatest::Reporter* reporter) {
  894. SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
  895. REPORTER_ASSERT(reporter, rec.isFillStyle());
  896. rec.setHairlineStyle();
  897. REPORTER_ASSERT(reporter, rec.isHairlineStyle());
  898. rec.setStrokeStyle(SK_Scalar1, false);
  899. REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
  900. rec.setStrokeStyle(SK_Scalar1, true);
  901. REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
  902. rec.setStrokeStyle(0, false);
  903. REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
  904. rec.setStrokeStyle(0, true);
  905. REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
  906. }
  907. // Set this for paths that don't have a consistent direction such as a bowtie.
  908. // (cheapComputeDirection is not expected to catch these.)
  909. // Legal values are CW (0), CCW (1) and Unknown (2), leaving 3 as a convenient sentinel.
  910. const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(3);
  911. static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
  912. SkPathPriv::FirstDirection expected) {
  913. if (expected == kDontCheckDir) {
  914. return;
  915. }
  916. SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
  917. SkPathPriv::FirstDirection dir;
  918. if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
  919. REPORTER_ASSERT(reporter, dir == expected);
  920. } else {
  921. REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
  922. }
  923. }
  924. static void test_direction(skiatest::Reporter* reporter) {
  925. size_t i;
  926. SkPath path;
  927. REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
  928. REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
  929. REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
  930. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
  931. static const char* gDegen[] = {
  932. "M 10 10",
  933. "M 10 10 M 20 20",
  934. "M 10 10 L 20 20",
  935. "M 10 10 L 10 10 L 10 10",
  936. "M 10 10 Q 10 10 10 10",
  937. "M 10 10 C 10 10 10 10 10 10",
  938. };
  939. for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
  940. path.reset();
  941. bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
  942. REPORTER_ASSERT(reporter, valid);
  943. REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
  944. }
  945. static const char* gCW[] = {
  946. "M 10 10 L 10 10 Q 20 10 20 20",
  947. "M 10 10 C 20 10 20 20 20 20",
  948. "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
  949. // rect with top two corners replaced by cubics with identical middle
  950. // control points
  951. "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
  952. "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
  953. };
  954. for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
  955. path.reset();
  956. bool valid = SkParsePath::FromSVGString(gCW[i], &path);
  957. REPORTER_ASSERT(reporter, valid);
  958. check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
  959. }
  960. static const char* gCCW[] = {
  961. "M 10 10 L 10 10 Q 20 10 20 -20",
  962. "M 10 10 C 20 10 20 -20 20 -20",
  963. "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
  964. // rect with top two corners replaced by cubics with identical middle
  965. // control points
  966. "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
  967. "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
  968. };
  969. for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
  970. path.reset();
  971. bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
  972. REPORTER_ASSERT(reporter, valid);
  973. check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
  974. }
  975. // Test two donuts, each wound a different direction. Only the outer contour
  976. // determines the cheap direction
  977. path.reset();
  978. path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
  979. path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
  980. check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
  981. path.reset();
  982. path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
  983. path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
  984. check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
  985. // triangle with one point really far from the origin.
  986. path.reset();
  987. // the first point is roughly 1.05e10, 1.05e10
  988. path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
  989. path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
  990. path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
  991. check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
  992. path.reset();
  993. path.conicTo(20, 0, 20, 20, 0.5f);
  994. path.close();
  995. check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
  996. path.reset();
  997. path.lineTo(1, 1e7f);
  998. path.lineTo(1e7f, 2e7f);
  999. path.close();
  1000. REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
  1001. check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
  1002. }
  1003. static void add_rect(SkPath* path, const SkRect& r) {
  1004. path->moveTo(r.fLeft, r.fTop);
  1005. path->lineTo(r.fRight, r.fTop);
  1006. path->lineTo(r.fRight, r.fBottom);
  1007. path->lineTo(r.fLeft, r.fBottom);
  1008. path->close();
  1009. }
  1010. static void test_bounds(skiatest::Reporter* reporter) {
  1011. static const SkRect rects[] = {
  1012. { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
  1013. { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
  1014. { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
  1015. { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
  1016. };
  1017. SkPath path0, path1;
  1018. for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
  1019. path0.addRect(rects[i]);
  1020. add_rect(&path1, rects[i]);
  1021. }
  1022. REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
  1023. }
  1024. static void stroke_cubic(const SkPoint pts[4]) {
  1025. SkPath path;
  1026. path.moveTo(pts[0]);
  1027. path.cubicTo(pts[1], pts[2], pts[3]);
  1028. SkPaint paint;
  1029. paint.setStyle(SkPaint::kStroke_Style);
  1030. paint.setStrokeWidth(SK_Scalar1 * 2);
  1031. SkPath fill;
  1032. paint.getFillPath(path, &fill);
  1033. }
  1034. // just ensure this can run w/o any SkASSERTS firing in the debug build
  1035. // we used to assert due to differences in how we determine a degenerate vector
  1036. // but that was fixed with the introduction of SkPoint::CanNormalize
  1037. static void stroke_tiny_cubic() {
  1038. SkPoint p0[] = {
  1039. { 372.0f, 92.0f },
  1040. { 372.0f, 92.0f },
  1041. { 372.0f, 92.0f },
  1042. { 372.0f, 92.0f },
  1043. };
  1044. stroke_cubic(p0);
  1045. SkPoint p1[] = {
  1046. { 372.0f, 92.0f },
  1047. { 372.0007f, 92.000755f },
  1048. { 371.99927f, 92.003922f },
  1049. { 371.99826f, 92.003899f },
  1050. };
  1051. stroke_cubic(p1);
  1052. }
  1053. static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
  1054. for (int i = 0; i < 2; ++i) {
  1055. SkPath::Iter iter(path, SkToBool(i));
  1056. SkPoint mv;
  1057. SkPoint pts[4];
  1058. SkPath::Verb v;
  1059. int nMT = 0;
  1060. int nCL = 0;
  1061. mv.set(0, 0);
  1062. while (SkPath::kDone_Verb != (v = iter.next(pts))) {
  1063. switch (v) {
  1064. case SkPath::kMove_Verb:
  1065. mv = pts[0];
  1066. ++nMT;
  1067. break;
  1068. case SkPath::kClose_Verb:
  1069. REPORTER_ASSERT(reporter, mv == pts[0]);
  1070. ++nCL;
  1071. break;
  1072. default:
  1073. break;
  1074. }
  1075. }
  1076. // if we force a close on the interator we should have a close
  1077. // for every moveTo
  1078. REPORTER_ASSERT(reporter, !i || nMT == nCL);
  1079. }
  1080. }
  1081. static void test_close(skiatest::Reporter* reporter) {
  1082. SkPath closePt;
  1083. closePt.moveTo(0, 0);
  1084. closePt.close();
  1085. check_close(reporter, closePt);
  1086. SkPath openPt;
  1087. openPt.moveTo(0, 0);
  1088. check_close(reporter, openPt);
  1089. SkPath empty;
  1090. check_close(reporter, empty);
  1091. empty.close();
  1092. check_close(reporter, empty);
  1093. SkPath rect;
  1094. rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
  1095. check_close(reporter, rect);
  1096. rect.close();
  1097. check_close(reporter, rect);
  1098. SkPath quad;
  1099. quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
  1100. check_close(reporter, quad);
  1101. quad.close();
  1102. check_close(reporter, quad);
  1103. SkPath cubic;
  1104. quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
  1105. 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
  1106. check_close(reporter, cubic);
  1107. cubic.close();
  1108. check_close(reporter, cubic);
  1109. SkPath line;
  1110. line.moveTo(SK_Scalar1, SK_Scalar1);
  1111. line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
  1112. check_close(reporter, line);
  1113. line.close();
  1114. check_close(reporter, line);
  1115. SkPath rect2;
  1116. rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
  1117. rect2.close();
  1118. rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
  1119. check_close(reporter, rect2);
  1120. rect2.close();
  1121. check_close(reporter, rect2);
  1122. SkPath oval3;
  1123. oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
  1124. oval3.close();
  1125. oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
  1126. check_close(reporter, oval3);
  1127. oval3.close();
  1128. check_close(reporter, oval3);
  1129. SkPath moves;
  1130. moves.moveTo(SK_Scalar1, SK_Scalar1);
  1131. moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
  1132. moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
  1133. moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
  1134. check_close(reporter, moves);
  1135. stroke_tiny_cubic();
  1136. }
  1137. static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
  1138. SkPath::Convexity expected) {
  1139. SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
  1140. SkPath::Convexity c = copy.getConvexity();
  1141. REPORTER_ASSERT(reporter, c == expected);
  1142. #ifndef SK_LEGACY_PATH_CONVEXITY
  1143. // test points-by-array interface
  1144. SkPath::Iter iter(path, true);
  1145. int initialMoves = 0;
  1146. SkPoint pts[4];
  1147. while (SkPath::kMove_Verb == iter.next(pts, false, false)) {
  1148. ++initialMoves;
  1149. }
  1150. if (initialMoves > 0) {
  1151. std::vector<SkPoint> points;
  1152. points.resize(path.getPoints(nullptr, 0));
  1153. (void) path.getPoints(&points.front(), points.size());
  1154. int skip = initialMoves - 1;
  1155. bool isConvex = SkPathPriv::IsConvex(&points.front() + skip, points.size() - skip);
  1156. REPORTER_ASSERT(reporter, isConvex == (SkPath::kConvex_Convexity == expected));
  1157. }
  1158. #endif
  1159. }
  1160. static void test_path_crbug389050(skiatest::Reporter* reporter) {
  1161. SkPath tinyConvexPolygon;
  1162. tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
  1163. tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
  1164. tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
  1165. tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
  1166. tinyConvexPolygon.close();
  1167. tinyConvexPolygon.getConvexity();
  1168. // This is convex, but so small that it fails many of our checks, and the three "backwards"
  1169. // bends convince the checker that it's concave. That's okay though, we draw it correctly.
  1170. check_convexity(reporter, tinyConvexPolygon, SkPath::kConcave_Convexity);
  1171. check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
  1172. SkPath platTriangle;
  1173. platTriangle.moveTo(0, 0);
  1174. platTriangle.lineTo(200, 0);
  1175. platTriangle.lineTo(100, 0.04f);
  1176. platTriangle.close();
  1177. platTriangle.getConvexity();
  1178. check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
  1179. platTriangle.reset();
  1180. platTriangle.moveTo(0, 0);
  1181. platTriangle.lineTo(200, 0);
  1182. platTriangle.lineTo(100, 0.03f);
  1183. platTriangle.close();
  1184. platTriangle.getConvexity();
  1185. check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
  1186. }
  1187. static void test_convexity2(skiatest::Reporter* reporter) {
  1188. SkPath pt;
  1189. pt.moveTo(0, 0);
  1190. pt.close();
  1191. check_convexity(reporter, pt, SkPath::kConvex_Convexity);
  1192. check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
  1193. SkPath line;
  1194. line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
  1195. line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
  1196. line.close();
  1197. check_convexity(reporter, line, SkPath::kConvex_Convexity);
  1198. check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
  1199. SkPath triLeft;
  1200. triLeft.moveTo(0, 0);
  1201. triLeft.lineTo(SK_Scalar1, 0);
  1202. triLeft.lineTo(SK_Scalar1, SK_Scalar1);
  1203. triLeft.close();
  1204. check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
  1205. check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
  1206. SkPath triRight;
  1207. triRight.moveTo(0, 0);
  1208. triRight.lineTo(-SK_Scalar1, 0);
  1209. triRight.lineTo(SK_Scalar1, SK_Scalar1);
  1210. triRight.close();
  1211. check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
  1212. check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
  1213. SkPath square;
  1214. square.moveTo(0, 0);
  1215. square.lineTo(SK_Scalar1, 0);
  1216. square.lineTo(SK_Scalar1, SK_Scalar1);
  1217. square.lineTo(0, SK_Scalar1);
  1218. square.close();
  1219. check_convexity(reporter, square, SkPath::kConvex_Convexity);
  1220. check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
  1221. SkPath redundantSquare;
  1222. redundantSquare.moveTo(0, 0);
  1223. redundantSquare.lineTo(0, 0);
  1224. redundantSquare.lineTo(0, 0);
  1225. redundantSquare.lineTo(SK_Scalar1, 0);
  1226. redundantSquare.lineTo(SK_Scalar1, 0);
  1227. redundantSquare.lineTo(SK_Scalar1, 0);
  1228. redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
  1229. redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
  1230. redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
  1231. redundantSquare.lineTo(0, SK_Scalar1);
  1232. redundantSquare.lineTo(0, SK_Scalar1);
  1233. redundantSquare.lineTo(0, SK_Scalar1);
  1234. redundantSquare.close();
  1235. check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
  1236. check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
  1237. SkPath bowTie;
  1238. bowTie.moveTo(0, 0);
  1239. bowTie.lineTo(0, 0);
  1240. bowTie.lineTo(0, 0);
  1241. bowTie.lineTo(SK_Scalar1, SK_Scalar1);
  1242. bowTie.lineTo(SK_Scalar1, SK_Scalar1);
  1243. bowTie.lineTo(SK_Scalar1, SK_Scalar1);
  1244. bowTie.lineTo(SK_Scalar1, 0);
  1245. bowTie.lineTo(SK_Scalar1, 0);
  1246. bowTie.lineTo(SK_Scalar1, 0);
  1247. bowTie.lineTo(0, SK_Scalar1);
  1248. bowTie.lineTo(0, SK_Scalar1);
  1249. bowTie.lineTo(0, SK_Scalar1);
  1250. bowTie.close();
  1251. check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
  1252. check_direction(reporter, bowTie, kDontCheckDir);
  1253. SkPath spiral;
  1254. spiral.moveTo(0, 0);
  1255. spiral.lineTo(100*SK_Scalar1, 0);
  1256. spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
  1257. spiral.lineTo(0, 100*SK_Scalar1);
  1258. spiral.lineTo(0, 50*SK_Scalar1);
  1259. spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
  1260. spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
  1261. spiral.close();
  1262. check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
  1263. check_direction(reporter, spiral, kDontCheckDir);
  1264. SkPath dent;
  1265. dent.moveTo(0, 0);
  1266. dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
  1267. dent.lineTo(0, 100*SK_Scalar1);
  1268. dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
  1269. dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
  1270. dent.close();
  1271. check_convexity(reporter, dent, SkPath::kConcave_Convexity);
  1272. check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
  1273. // https://bug.skia.org/2235
  1274. SkPath strokedSin;
  1275. for (int i = 0; i < 2000; i++) {
  1276. SkScalar x = SkIntToScalar(i) / 2;
  1277. SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
  1278. if (0 == i) {
  1279. strokedSin.moveTo(x, y);
  1280. } else {
  1281. strokedSin.lineTo(x, y);
  1282. }
  1283. }
  1284. SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
  1285. stroke.setStrokeStyle(2 * SK_Scalar1);
  1286. stroke.applyToPath(&strokedSin, strokedSin);
  1287. check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
  1288. check_direction(reporter, strokedSin, kDontCheckDir);
  1289. // http://crbug.com/412640
  1290. SkPath degenerateConcave;
  1291. degenerateConcave.moveTo(148.67912f, 191.875f);
  1292. degenerateConcave.lineTo(470.37695f, 7.5f);
  1293. degenerateConcave.lineTo(148.67912f, 191.875f);
  1294. degenerateConcave.lineTo(41.446522f, 376.25f);
  1295. degenerateConcave.lineTo(-55.971577f, 460.0f);
  1296. degenerateConcave.lineTo(41.446522f, 376.25f);
  1297. check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
  1298. check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
  1299. // http://crbug.com/433683
  1300. SkPath badFirstVector;
  1301. badFirstVector.moveTo(501.087708f, 319.610352f);
  1302. badFirstVector.lineTo(501.087708f, 319.610352f);
  1303. badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
  1304. badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
  1305. badFirstVector.lineTo(301.557678f, 98.044601f);
  1306. badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
  1307. badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
  1308. badFirstVector.lineTo(504.912292f, 316.389648f);
  1309. badFirstVector.lineTo(501.087708f, 319.610352f);
  1310. badFirstVector.close();
  1311. check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
  1312. }
  1313. static void test_convexity_doubleback(skiatest::Reporter* reporter) {
  1314. SkPath doubleback;
  1315. doubleback.lineTo(1, 1);
  1316. check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
  1317. doubleback.lineTo(2, 2);
  1318. check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
  1319. doubleback.reset();
  1320. doubleback.lineTo(1, 0);
  1321. check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
  1322. doubleback.lineTo(2, 0);
  1323. check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
  1324. doubleback.lineTo(1, 0);
  1325. check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
  1326. doubleback.reset();
  1327. doubleback.quadTo(1, 1, 2, 2);
  1328. check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
  1329. doubleback.reset();
  1330. doubleback.quadTo(1, 0, 2, 0);
  1331. check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
  1332. doubleback.quadTo(1, 0, 0, 0);
  1333. check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
  1334. }
  1335. static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
  1336. const SkRect& bounds) {
  1337. REPORTER_ASSERT(reporter, p.isConvex());
  1338. REPORTER_ASSERT(reporter, p.getBounds() == bounds);
  1339. SkPath p2(p);
  1340. REPORTER_ASSERT(reporter, p2.isConvex());
  1341. REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
  1342. SkPath other;
  1343. other.swap(p2);
  1344. REPORTER_ASSERT(reporter, other.isConvex());
  1345. REPORTER_ASSERT(reporter, other.getBounds() == bounds);
  1346. }
  1347. static void setFromString(SkPath* path, const char str[]) {
  1348. bool first = true;
  1349. while (str) {
  1350. SkScalar x, y;
  1351. str = SkParse::FindScalar(str, &x);
  1352. if (nullptr == str) {
  1353. break;
  1354. }
  1355. str = SkParse::FindScalar(str, &y);
  1356. SkASSERT(str);
  1357. if (first) {
  1358. path->moveTo(x, y);
  1359. first = false;
  1360. } else {
  1361. path->lineTo(x, y);
  1362. }
  1363. }
  1364. }
  1365. static void test_convexity(skiatest::Reporter* reporter) {
  1366. SkPath path;
  1367. check_convexity(reporter, path, SkPath::kConvex_Convexity);
  1368. path.addCircle(0, 0, SkIntToScalar(10));
  1369. check_convexity(reporter, path, SkPath::kConvex_Convexity);
  1370. path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
  1371. check_convexity(reporter, path, SkPath::kConcave_Convexity);
  1372. path.reset();
  1373. path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
  1374. check_convexity(reporter, path, SkPath::kConvex_Convexity);
  1375. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
  1376. path.reset();
  1377. path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
  1378. check_convexity(reporter, path, SkPath::kConvex_Convexity);
  1379. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
  1380. path.reset();
  1381. path.quadTo(100, 100, 50, 50); // This from GM:convexpaths
  1382. check_convexity(reporter, path, SkPath::kConvex_Convexity);
  1383. static const struct {
  1384. const char* fPathStr;
  1385. SkPath::Convexity fExpectedConvexity;
  1386. SkPathPriv::FirstDirection fExpectedDirection;
  1387. } gRec[] = {
  1388. { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
  1389. { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
  1390. { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
  1391. { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
  1392. { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
  1393. { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
  1394. { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
  1395. { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
  1396. };
  1397. for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
  1398. path.reset();
  1399. setFromString(&path, gRec[i].fPathStr);
  1400. check_convexity(reporter, path, gRec[i].fExpectedConvexity);
  1401. check_direction(reporter, path, gRec[i].fExpectedDirection);
  1402. // check after setting the initial convex and direction
  1403. if (kDontCheckDir != gRec[i].fExpectedDirection) {
  1404. SkPath copy(path);
  1405. SkPathPriv::FirstDirection dir;
  1406. bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
  1407. REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
  1408. ^ foundDir);
  1409. REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
  1410. check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
  1411. }
  1412. REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
  1413. check_direction(reporter, path, gRec[i].fExpectedDirection);
  1414. }
  1415. static const SkPoint nonFinitePts[] = {
  1416. { SK_ScalarInfinity, 0 },
  1417. { 0, SK_ScalarInfinity },
  1418. { SK_ScalarInfinity, SK_ScalarInfinity },
  1419. { SK_ScalarNegativeInfinity, 0},
  1420. { 0, SK_ScalarNegativeInfinity },
  1421. { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
  1422. { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
  1423. { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
  1424. { SK_ScalarNaN, 0 },
  1425. { 0, SK_ScalarNaN },
  1426. { SK_ScalarNaN, SK_ScalarNaN },
  1427. };
  1428. const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
  1429. static const SkPoint axisAlignedPts[] = {
  1430. { SK_ScalarMax, 0 },
  1431. { 0, SK_ScalarMax },
  1432. { SK_ScalarMin, 0 },
  1433. { 0, SK_ScalarMin },
  1434. };
  1435. const size_t axisAlignedPtsCount = sizeof(axisAlignedPts) / sizeof(axisAlignedPts[0]);
  1436. for (int index = 0; index < (int) (13 * nonFinitePtsCount * axisAlignedPtsCount); ++index) {
  1437. int i = (int) (index % nonFinitePtsCount);
  1438. int f = (int) (index % axisAlignedPtsCount);
  1439. int g = (int) ((f + 1) % axisAlignedPtsCount);
  1440. path.reset();
  1441. switch (index % 13) {
  1442. case 0: path.lineTo(nonFinitePts[i]); break;
  1443. case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
  1444. case 2: path.quadTo(nonFinitePts[i], axisAlignedPts[f]); break;
  1445. case 3: path.quadTo(axisAlignedPts[f], nonFinitePts[i]); break;
  1446. case 4: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[f]); break;
  1447. case 5: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], axisAlignedPts[f]); break;
  1448. case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], nonFinitePts[i]); break;
  1449. case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], axisAlignedPts[f]); break;
  1450. case 8: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], nonFinitePts[i]); break;
  1451. case 9: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], nonFinitePts[i]); break;
  1452. case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
  1453. case 11: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[g]); break;
  1454. case 12: path.moveTo(nonFinitePts[i]); break;
  1455. }
  1456. check_convexity(reporter, path, SkPath::kUnknown_Convexity);
  1457. }
  1458. for (int index = 0; index < (int) (11 * axisAlignedPtsCount); ++index) {
  1459. int f = (int) (index % axisAlignedPtsCount);
  1460. int g = (int) ((f + 1) % axisAlignedPtsCount);
  1461. path.reset();
  1462. int curveSelect = index % 11;
  1463. switch (curveSelect) {
  1464. case 0: path.moveTo(axisAlignedPts[f]); break;
  1465. case 1: path.lineTo(axisAlignedPts[f]); break;
  1466. case 2: path.quadTo(axisAlignedPts[f], axisAlignedPts[f]); break;
  1467. case 3: path.quadTo(axisAlignedPts[f], axisAlignedPts[g]); break;
  1468. case 4: path.quadTo(axisAlignedPts[g], axisAlignedPts[f]); break;
  1469. case 5: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[f]); break;
  1470. case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[g]); break;
  1471. case 7: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[f]); break;
  1472. case 8: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[g]); break;
  1473. case 9: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[f]); break;
  1474. case 10: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[g]); break;
  1475. }
  1476. if (curveSelect == 0 || curveSelect == 1 || curveSelect == 2 || curveSelect == 5) {
  1477. check_convexity(reporter, path, SkPath::kConvex_Convexity);
  1478. } else {
  1479. SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
  1480. SkPath::Convexity c = copy.getConvexity();
  1481. REPORTER_ASSERT(reporter, SkPath::kUnknown_Convexity == c
  1482. || SkPath::kConcave_Convexity == c);
  1483. }
  1484. }
  1485. static const SkPoint diagonalPts[] = {
  1486. { SK_ScalarMax, SK_ScalarMax },
  1487. { SK_ScalarMin, SK_ScalarMin },
  1488. };
  1489. const size_t diagonalPtsCount = sizeof(diagonalPts) / sizeof(diagonalPts[0]);
  1490. for (int index = 0; index < (int) (7 * diagonalPtsCount); ++index) {
  1491. int f = (int) (index % diagonalPtsCount);
  1492. int g = (int) ((f + 1) % diagonalPtsCount);
  1493. path.reset();
  1494. int curveSelect = index % 11;
  1495. switch (curveSelect) {
  1496. case 0: path.moveTo(diagonalPts[f]); break;
  1497. case 1: path.lineTo(diagonalPts[f]); break;
  1498. case 2: path.quadTo(diagonalPts[f], diagonalPts[f]); break;
  1499. case 3: path.quadTo(axisAlignedPts[f], diagonalPts[g]); break;
  1500. case 4: path.quadTo(diagonalPts[g], axisAlignedPts[f]); break;
  1501. case 5: path.cubicTo(diagonalPts[f], diagonalPts[f], diagonalPts[f]); break;
  1502. case 6: path.cubicTo(diagonalPts[f], diagonalPts[f], axisAlignedPts[g]); break;
  1503. case 7: path.cubicTo(diagonalPts[f], axisAlignedPts[g], diagonalPts[f]); break;
  1504. case 8: path.cubicTo(axisAlignedPts[f], diagonalPts[g], diagonalPts[g]); break;
  1505. case 9: path.cubicTo(diagonalPts[g], diagonalPts[f], axisAlignedPts[f]); break;
  1506. case 10: path.cubicTo(diagonalPts[g], axisAlignedPts[f], diagonalPts[g]); break;
  1507. }
  1508. if (curveSelect == 0) {
  1509. check_convexity(reporter, path, SkPath::kConvex_Convexity);
  1510. } else {
  1511. SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
  1512. SkPath::Convexity c = copy.getConvexity();
  1513. REPORTER_ASSERT(reporter, SkPath::kUnknown_Convexity == c
  1514. || SkPath::kConcave_Convexity == c);
  1515. }
  1516. }
  1517. path.reset();
  1518. path.moveTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
  1519. path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eea38)); // -0.284072f, -0.0622351f
  1520. path.lineTo(SkBits2Float(0xbe9171a0), SkBits2Float(0xbd7ee5a7)); // -0.28407f, -0.0622307f
  1521. path.lineTo(SkBits2Float(0xbe917147), SkBits2Float(0xbd7ed886)); // -0.284067f, -0.0622182f
  1522. path.lineTo(SkBits2Float(0xbe917378), SkBits2Float(0xbd7ee1a9)); // -0.284084f, -0.0622269f
  1523. path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
  1524. path.close();
  1525. check_convexity(reporter, path, SkPath::kConcave_Convexity);
  1526. }
  1527. static void test_isLine(skiatest::Reporter* reporter) {
  1528. SkPath path;
  1529. SkPoint pts[2];
  1530. const SkScalar value = SkIntToScalar(5);
  1531. REPORTER_ASSERT(reporter, !path.isLine(nullptr));
  1532. // set some non-zero values
  1533. pts[0].set(value, value);
  1534. pts[1].set(value, value);
  1535. REPORTER_ASSERT(reporter, !path.isLine(pts));
  1536. // check that pts was untouched
  1537. REPORTER_ASSERT(reporter, pts[0].equals(value, value));
  1538. REPORTER_ASSERT(reporter, pts[1].equals(value, value));
  1539. const SkScalar moveX = SkIntToScalar(1);
  1540. const SkScalar moveY = SkIntToScalar(2);
  1541. REPORTER_ASSERT(reporter, value != moveX && value != moveY);
  1542. path.moveTo(moveX, moveY);
  1543. REPORTER_ASSERT(reporter, !path.isLine(nullptr));
  1544. REPORTER_ASSERT(reporter, !path.isLine(pts));
  1545. // check that pts was untouched
  1546. REPORTER_ASSERT(reporter, pts[0].equals(value, value));
  1547. REPORTER_ASSERT(reporter, pts[1].equals(value, value));
  1548. const SkScalar lineX = SkIntToScalar(2);
  1549. const SkScalar lineY = SkIntToScalar(2);
  1550. REPORTER_ASSERT(reporter, value != lineX && value != lineY);
  1551. path.lineTo(lineX, lineY);
  1552. REPORTER_ASSERT(reporter, path.isLine(nullptr));
  1553. REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
  1554. REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
  1555. REPORTER_ASSERT(reporter, path.isLine(pts));
  1556. REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
  1557. REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
  1558. path.lineTo(0, 0); // too many points/verbs
  1559. REPORTER_ASSERT(reporter, !path.isLine(nullptr));
  1560. REPORTER_ASSERT(reporter, !path.isLine(pts));
  1561. REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
  1562. REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
  1563. path.reset();
  1564. path.quadTo(1, 1, 2, 2);
  1565. REPORTER_ASSERT(reporter, !path.isLine(nullptr));
  1566. }
  1567. static void test_conservativelyContains(skiatest::Reporter* reporter) {
  1568. SkPath path;
  1569. // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
  1570. static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
  1571. // A circle that bounds kBaseRect (with a significant amount of slop)
  1572. SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
  1573. circleR *= 1.75f / 2;
  1574. static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
  1575. // round-rect radii
  1576. static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
  1577. static const struct SUPPRESS_VISIBILITY_WARNING {
  1578. SkRect fQueryRect;
  1579. bool fInRect;
  1580. bool fInCircle;
  1581. bool fInRR;
  1582. bool fInCubicRR;
  1583. } kQueries[] = {
  1584. {kBaseRect, true, true, false, false},
  1585. // rect well inside of kBaseRect
  1586. {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
  1587. kBaseRect.fTop + 0.25f*kBaseRect.height(),
  1588. kBaseRect.fRight - 0.25f*kBaseRect.width(),
  1589. kBaseRect.fBottom - 0.25f*kBaseRect.height()),
  1590. true, true, true, true},
  1591. // rects with edges off by one from kBaseRect's edges
  1592. {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
  1593. kBaseRect.width(), kBaseRect.height() + 1),
  1594. false, true, false, false},
  1595. {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
  1596. kBaseRect.width() + 1, kBaseRect.height()),
  1597. false, true, false, false},
  1598. {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
  1599. kBaseRect.width() + 1, kBaseRect.height() + 1),
  1600. false, true, false, false},
  1601. {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
  1602. kBaseRect.width(), kBaseRect.height()),
  1603. false, true, false, false},
  1604. {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
  1605. kBaseRect.width(), kBaseRect.height()),
  1606. false, true, false, false},
  1607. {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
  1608. kBaseRect.width() + 2, kBaseRect.height()),
  1609. false, true, false, false},
  1610. {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
  1611. kBaseRect.width() + 2, kBaseRect.height()),
  1612. false, true, false, false},
  1613. // zero-w/h rects at each corner of kBaseRect
  1614. {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
  1615. {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
  1616. {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
  1617. {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
  1618. // far away rect
  1619. {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
  1620. SkIntToScalar(10), SkIntToScalar(10)),
  1621. false, false, false, false},
  1622. // very large rect containing kBaseRect
  1623. {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
  1624. kBaseRect.fTop - 5 * kBaseRect.height(),
  1625. 11 * kBaseRect.width(), 11 * kBaseRect.height()),
  1626. false, false, false, false},
  1627. // skinny rect that spans same y-range as kBaseRect
  1628. {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
  1629. SkIntToScalar(1), kBaseRect.height()),
  1630. true, true, true, true},
  1631. // short rect that spans same x-range as kBaseRect
  1632. {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
  1633. true, true, true, true},
  1634. // skinny rect that spans slightly larger y-range than kBaseRect
  1635. {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
  1636. SkIntToScalar(1), kBaseRect.height() + 1),
  1637. false, true, false, false},
  1638. // short rect that spans slightly larger x-range than kBaseRect
  1639. {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
  1640. kBaseRect.width() + 1, SkScalar(1)),
  1641. false, true, false, false},
  1642. };
  1643. for (int inv = 0; inv < 4; ++inv) {
  1644. for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
  1645. SkRect qRect = kQueries[q].fQueryRect;
  1646. if (inv & 0x1) {
  1647. using std::swap;
  1648. swap(qRect.fLeft, qRect.fRight);
  1649. }
  1650. if (inv & 0x2) {
  1651. using std::swap;
  1652. swap(qRect.fTop, qRect.fBottom);
  1653. }
  1654. for (int d = 0; d < 2; ++d) {
  1655. SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
  1656. path.reset();
  1657. path.addRect(kBaseRect, dir);
  1658. REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
  1659. path.conservativelyContainsRect(qRect));
  1660. path.reset();
  1661. path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
  1662. REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
  1663. path.conservativelyContainsRect(qRect));
  1664. path.reset();
  1665. path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
  1666. REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
  1667. path.conservativelyContainsRect(qRect));
  1668. path.reset();
  1669. path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
  1670. path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
  1671. kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
  1672. kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
  1673. path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
  1674. path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
  1675. path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
  1676. path.close();
  1677. REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
  1678. path.conservativelyContainsRect(qRect));
  1679. }
  1680. // Slightly non-convex shape, shouldn't contain any rects.
  1681. path.reset();
  1682. path.moveTo(0, 0);
  1683. path.lineTo(SkIntToScalar(50), 0.05f);
  1684. path.lineTo(SkIntToScalar(100), 0);
  1685. path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
  1686. path.lineTo(0, SkIntToScalar(100));
  1687. path.close();
  1688. REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
  1689. }
  1690. }
  1691. // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
  1692. path.reset();
  1693. path.moveTo(0, 0);
  1694. path.lineTo(SkIntToScalar(100), 0);
  1695. path.lineTo(0, SkIntToScalar(100));
  1696. // inside, on along top edge
  1697. REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
  1698. SkIntToScalar(10),
  1699. SkIntToScalar(10))));
  1700. // above
  1701. REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
  1702. SkRect::MakeXYWH(SkIntToScalar(50),
  1703. SkIntToScalar(-10),
  1704. SkIntToScalar(10),
  1705. SkIntToScalar(10))));
  1706. // to the left
  1707. REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
  1708. SkIntToScalar(5),
  1709. SkIntToScalar(5),
  1710. SkIntToScalar(5))));
  1711. // outside the diagonal edge
  1712. REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
  1713. SkIntToScalar(200),
  1714. SkIntToScalar(20),
  1715. SkIntToScalar(5))));
  1716. // Test that multiple move commands do not cause asserts.
  1717. path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
  1718. REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
  1719. SkIntToScalar(10),
  1720. SkIntToScalar(10))));
  1721. // Same as above path and first test but with an extra moveTo.
  1722. path.reset();
  1723. path.moveTo(100, 100);
  1724. path.moveTo(0, 0);
  1725. path.lineTo(SkIntToScalar(100), 0);
  1726. path.lineTo(0, SkIntToScalar(100));
  1727. // Convexity logic is now more conservative, so that multiple (non-trailing) moveTos make a
  1728. // path non-convex.
  1729. REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
  1730. SkRect::MakeXYWH(SkIntToScalar(50), 0,
  1731. SkIntToScalar(10),
  1732. SkIntToScalar(10))));
  1733. // Same as above path and first test but with the extra moveTo making a degenerate sub-path
  1734. // following the non-empty sub-path. Verifies that this does not trigger assertions.
  1735. path.reset();
  1736. path.moveTo(0, 0);
  1737. path.lineTo(SkIntToScalar(100), 0);
  1738. path.lineTo(0, SkIntToScalar(100));
  1739. path.moveTo(100, 100);
  1740. REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
  1741. SkIntToScalar(10),
  1742. SkIntToScalar(10))));
  1743. // Test that multiple move commands do not cause asserts and that the function
  1744. // is not confused by the multiple moves.
  1745. path.reset();
  1746. path.moveTo(0, 0);
  1747. path.lineTo(SkIntToScalar(100), 0);
  1748. path.lineTo(0, SkIntToScalar(100));
  1749. path.moveTo(0, SkIntToScalar(200));
  1750. path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
  1751. path.lineTo(0, SkIntToScalar(300));
  1752. REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
  1753. SkRect::MakeXYWH(SkIntToScalar(50), 0,
  1754. SkIntToScalar(10),
  1755. SkIntToScalar(10))));
  1756. path.reset();
  1757. path.lineTo(100, 100);
  1758. REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
  1759. // An empty path should not contain any rectangle. It's questionable whether an empty path
  1760. // contains an empty rectangle. However, since it is a conservative test it is ok to
  1761. // return false.
  1762. path.reset();
  1763. REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
  1764. REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
  1765. }
  1766. static void test_isRect_open_close(skiatest::Reporter* reporter) {
  1767. SkPath path;
  1768. bool isClosed;
  1769. path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
  1770. path.close();
  1771. REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
  1772. REPORTER_ASSERT(reporter, isClosed);
  1773. }
  1774. // Simple isRect test is inline TestPath, below.
  1775. // test_isRect provides more extensive testing.
  1776. static void test_isRect(skiatest::Reporter* reporter) {
  1777. test_isRect_open_close(reporter);
  1778. // passing tests (all moveTo / lineTo...
  1779. SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
  1780. SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
  1781. SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
  1782. SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
  1783. SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
  1784. SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
  1785. SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
  1786. SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
  1787. SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
  1788. SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
  1789. SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
  1790. SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
  1791. SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
  1792. SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
  1793. SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
  1794. // failing tests
  1795. SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
  1796. SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
  1797. SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
  1798. SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
  1799. SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
  1800. SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
  1801. SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
  1802. SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
  1803. SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
  1804. SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
  1805. SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
  1806. // no close, but we should detect them as fillably the same as a rect
  1807. SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
  1808. SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
  1809. SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
  1810. // like c2, but we double-back on ourselves
  1811. SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
  1812. // like c2, but we overshoot the start point
  1813. SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
  1814. SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
  1815. struct IsRectTest {
  1816. SkPoint *fPoints;
  1817. int fPointCount;
  1818. bool fClose;
  1819. bool fIsRect;
  1820. } tests[] = {
  1821. { r1, SK_ARRAY_COUNT(r1), true, true },
  1822. { r2, SK_ARRAY_COUNT(r2), true, true },
  1823. { r3, SK_ARRAY_COUNT(r3), true, true },
  1824. { r4, SK_ARRAY_COUNT(r4), true, true },
  1825. { r5, SK_ARRAY_COUNT(r5), true, true },
  1826. { r6, SK_ARRAY_COUNT(r6), true, true },
  1827. { r7, SK_ARRAY_COUNT(r7), true, true },
  1828. { r8, SK_ARRAY_COUNT(r8), true, true },
  1829. { r9, SK_ARRAY_COUNT(r9), true, true },
  1830. { ra, SK_ARRAY_COUNT(ra), true, true },
  1831. { rb, SK_ARRAY_COUNT(rb), true, true },
  1832. { rc, SK_ARRAY_COUNT(rc), true, true },
  1833. { rd, SK_ARRAY_COUNT(rd), true, true },
  1834. { re, SK_ARRAY_COUNT(re), true, true },
  1835. { rf, SK_ARRAY_COUNT(rf), true, true },
  1836. { f1, SK_ARRAY_COUNT(f1), true, false },
  1837. { f2, SK_ARRAY_COUNT(f2), true, false },
  1838. { f3, SK_ARRAY_COUNT(f3), true, false },
  1839. { f4, SK_ARRAY_COUNT(f4), true, false },
  1840. { f5, SK_ARRAY_COUNT(f5), true, false },
  1841. { f6, SK_ARRAY_COUNT(f6), true, false },
  1842. { f7, SK_ARRAY_COUNT(f7), true, false },
  1843. { f8, SK_ARRAY_COUNT(f8), true, false },
  1844. { f9, SK_ARRAY_COUNT(f9), true, false },
  1845. { fa, SK_ARRAY_COUNT(fa), true, false },
  1846. { fb, SK_ARRAY_COUNT(fb), true, false },
  1847. { c1, SK_ARRAY_COUNT(c1), false, true },
  1848. { c2, SK_ARRAY_COUNT(c2), false, true },
  1849. { c3, SK_ARRAY_COUNT(c3), false, true },
  1850. { d1, SK_ARRAY_COUNT(d1), false, false },
  1851. { d2, SK_ARRAY_COUNT(d2), false, true },
  1852. { d3, SK_ARRAY_COUNT(d3), false, false },
  1853. };
  1854. const size_t testCount = SK_ARRAY_COUNT(tests);
  1855. int index;
  1856. for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
  1857. SkPath path;
  1858. path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
  1859. for (index = 1; index < tests[testIndex].fPointCount; ++index) {
  1860. path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
  1861. }
  1862. if (tests[testIndex].fClose) {
  1863. path.close();
  1864. }
  1865. REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
  1866. if (tests[testIndex].fIsRect) {
  1867. SkRect computed, expected;
  1868. bool isClosed;
  1869. SkPath::Direction direction;
  1870. SkPathPriv::FirstDirection cheapDirection;
  1871. int pointCount = tests[testIndex].fPointCount - (d2 == tests[testIndex].fPoints);
  1872. expected.set(tests[testIndex].fPoints, pointCount);
  1873. REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
  1874. REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
  1875. REPORTER_ASSERT(reporter, expected == computed);
  1876. REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
  1877. REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
  1878. } else {
  1879. SkRect computed;
  1880. computed.set(123, 456, 789, 1011);
  1881. for (auto c : {true, false})
  1882. for (auto d : {SkPath::kCW_Direction, SkPath::kCCW_Direction}) {
  1883. bool isClosed = c;
  1884. SkPath::Direction direction = d;
  1885. REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
  1886. REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
  1887. REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
  1888. REPORTER_ASSERT(reporter, isClosed == c);
  1889. REPORTER_ASSERT(reporter, direction == d);
  1890. }
  1891. }
  1892. }
  1893. // fail, close then line
  1894. SkPath path1;
  1895. path1.moveTo(r1[0].fX, r1[0].fY);
  1896. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  1897. path1.lineTo(r1[index].fX, r1[index].fY);
  1898. }
  1899. path1.close();
  1900. path1.lineTo(1, 0);
  1901. REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
  1902. // fail, move in the middle
  1903. path1.reset();
  1904. path1.moveTo(r1[0].fX, r1[0].fY);
  1905. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  1906. if (index == 2) {
  1907. path1.moveTo(1, .5f);
  1908. }
  1909. path1.lineTo(r1[index].fX, r1[index].fY);
  1910. }
  1911. path1.close();
  1912. REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
  1913. // fail, move on the edge
  1914. path1.reset();
  1915. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  1916. path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
  1917. path1.lineTo(r1[index].fX, r1[index].fY);
  1918. }
  1919. path1.close();
  1920. REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
  1921. // fail, quad
  1922. path1.reset();
  1923. path1.moveTo(r1[0].fX, r1[0].fY);
  1924. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  1925. if (index == 2) {
  1926. path1.quadTo(1, .5f, 1, .5f);
  1927. }
  1928. path1.lineTo(r1[index].fX, r1[index].fY);
  1929. }
  1930. path1.close();
  1931. REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
  1932. // fail, cubic
  1933. path1.reset();
  1934. path1.moveTo(r1[0].fX, r1[0].fY);
  1935. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  1936. if (index == 2) {
  1937. path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
  1938. }
  1939. path1.lineTo(r1[index].fX, r1[index].fY);
  1940. }
  1941. path1.close();
  1942. REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
  1943. }
  1944. static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
  1945. const SkRect& rect, SkPath::Direction dir, unsigned start) {
  1946. SkRect r = SkRect::MakeEmpty();
  1947. SkPath::Direction d = SkPath::kCCW_Direction;
  1948. unsigned s = ~0U;
  1949. REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
  1950. REPORTER_ASSERT(reporter, r == rect);
  1951. REPORTER_ASSERT(reporter, d == dir);
  1952. REPORTER_ASSERT(reporter, s == start);
  1953. }
  1954. static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
  1955. using std::swap;
  1956. SkRect r = SkRect::MakeEmpty();
  1957. SkPath::Direction d = SkPath::kCCW_Direction;
  1958. unsigned s = ~0U;
  1959. const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
  1960. const SkRect emptyRect = SkRect::MakeEmpty();
  1961. SkPath path;
  1962. for (int start = 0; start < 4; ++start) {
  1963. for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
  1964. SkPath path;
  1965. path.addRect(testRect, dir, start);
  1966. check_simple_closed_rect(reporter, path, testRect, dir, start);
  1967. path.close();
  1968. check_simple_closed_rect(reporter, path, testRect, dir, start);
  1969. SkPath path2 = path;
  1970. path2.lineTo(10, 10);
  1971. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
  1972. path2 = path;
  1973. path2.moveTo(10, 10);
  1974. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
  1975. path2 = path;
  1976. path2.addRect(testRect, dir, start);
  1977. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
  1978. // Make the path by hand, manually closing it.
  1979. path2.reset();
  1980. SkPath::RawIter iter(path);
  1981. SkPath::Verb v;
  1982. SkPoint verbPts[4];
  1983. SkPoint firstPt = {0.f, 0.f};
  1984. while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
  1985. switch(v) {
  1986. case SkPath::kMove_Verb:
  1987. firstPt = verbPts[0];
  1988. path2.moveTo(verbPts[0]);
  1989. break;
  1990. case SkPath::kLine_Verb:
  1991. path2.lineTo(verbPts[1]);
  1992. break;
  1993. default:
  1994. break;
  1995. }
  1996. }
  1997. // We haven't closed it yet...
  1998. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
  1999. // ... now we do and test again.
  2000. path2.lineTo(firstPt);
  2001. check_simple_closed_rect(reporter, path2, testRect, dir, start);
  2002. // A redundant close shouldn't cause a failure.
  2003. path2.close();
  2004. check_simple_closed_rect(reporter, path2, testRect, dir, start);
  2005. // Degenerate point and line rects are not allowed
  2006. path2.reset();
  2007. path2.addRect(emptyRect, dir, start);
  2008. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
  2009. SkRect degenRect = testRect;
  2010. degenRect.fLeft = degenRect.fRight;
  2011. path2.reset();
  2012. path2.addRect(degenRect, dir, start);
  2013. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
  2014. degenRect = testRect;
  2015. degenRect.fTop = degenRect.fBottom;
  2016. path2.reset();
  2017. path2.addRect(degenRect, dir, start);
  2018. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
  2019. // An inverted rect makes a rect path, but changes the winding dir and start point.
  2020. SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
  2021. ? SkPath::kCCW_Direction
  2022. : SkPath::kCW_Direction;
  2023. static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
  2024. static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
  2025. SkRect swapRect = testRect;
  2026. swap(swapRect.fLeft, swapRect.fRight);
  2027. path2.reset();
  2028. path2.addRect(swapRect, dir, start);
  2029. check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
  2030. swapRect = testRect;
  2031. swap(swapRect.fTop, swapRect.fBottom);
  2032. path2.reset();
  2033. path2.addRect(swapRect, dir, start);
  2034. check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
  2035. }
  2036. }
  2037. // down, up, left, close
  2038. path.reset();
  2039. path.moveTo(1, 1);
  2040. path.lineTo(1, 2);
  2041. path.lineTo(1, 1);
  2042. path.lineTo(0, 1);
  2043. SkRect rect;
  2044. SkPath::Direction dir;
  2045. unsigned start;
  2046. path.close();
  2047. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
  2048. // right, left, up, close
  2049. path.reset();
  2050. path.moveTo(1, 1);
  2051. path.lineTo(2, 1);
  2052. path.lineTo(1, 1);
  2053. path.lineTo(1, 0);
  2054. path.close();
  2055. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
  2056. // parallelogram with horizontal edges
  2057. path.reset();
  2058. path.moveTo(1, 0);
  2059. path.lineTo(3, 0);
  2060. path.lineTo(2, 1);
  2061. path.lineTo(0, 1);
  2062. path.close();
  2063. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
  2064. // parallelogram with vertical edges
  2065. path.reset();
  2066. path.moveTo(0, 1);
  2067. path.lineTo(0, 3);
  2068. path.lineTo(1, 2);
  2069. path.lineTo(1, 0);
  2070. path.close();
  2071. REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
  2072. }
  2073. static void test_isNestedFillRects(skiatest::Reporter* reporter) {
  2074. // passing tests (all moveTo / lineTo...
  2075. SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
  2076. SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
  2077. SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
  2078. SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
  2079. SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
  2080. SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
  2081. SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
  2082. SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
  2083. SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
  2084. SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
  2085. SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
  2086. SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
  2087. SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
  2088. SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
  2089. // failing tests
  2090. SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
  2091. SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
  2092. SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
  2093. SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
  2094. SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
  2095. SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
  2096. SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
  2097. SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
  2098. // success, no close is OK
  2099. SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
  2100. SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
  2101. struct IsNestedRectTest {
  2102. SkPoint *fPoints;
  2103. int fPointCount;
  2104. SkPathPriv::FirstDirection fDirection;
  2105. bool fClose;
  2106. bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
  2107. } tests[] = {
  2108. { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
  2109. { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
  2110. { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
  2111. { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
  2112. { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
  2113. { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
  2114. { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
  2115. { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
  2116. { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
  2117. { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
  2118. { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
  2119. { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
  2120. { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
  2121. { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
  2122. { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
  2123. { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
  2124. { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
  2125. { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
  2126. { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
  2127. { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
  2128. { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
  2129. { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
  2130. { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
  2131. { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
  2132. };
  2133. const size_t testCount = SK_ARRAY_COUNT(tests);
  2134. int index;
  2135. for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
  2136. for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
  2137. SkPath path;
  2138. if (rectFirst) {
  2139. path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
  2140. }
  2141. path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
  2142. for (index = 1; index < tests[testIndex].fPointCount; ++index) {
  2143. path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
  2144. }
  2145. if (tests[testIndex].fClose) {
  2146. path.close();
  2147. }
  2148. if (!rectFirst) {
  2149. path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
  2150. }
  2151. REPORTER_ASSERT(reporter,
  2152. tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
  2153. if (tests[testIndex].fIsNestedRect) {
  2154. SkRect expected[2], computed[2];
  2155. SkPathPriv::FirstDirection expectedDirs[2];
  2156. SkPath::Direction computedDirs[2];
  2157. SkRect testBounds;
  2158. testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
  2159. expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
  2160. expected[1] = testBounds;
  2161. if (rectFirst) {
  2162. expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
  2163. } else {
  2164. expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
  2165. }
  2166. expectedDirs[1] = tests[testIndex].fDirection;
  2167. REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
  2168. REPORTER_ASSERT(reporter, expected[0] == computed[0]);
  2169. REPORTER_ASSERT(reporter, expected[1] == computed[1]);
  2170. REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
  2171. REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
  2172. }
  2173. }
  2174. // fail, close then line
  2175. SkPath path1;
  2176. if (rectFirst) {
  2177. path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
  2178. }
  2179. path1.moveTo(r1[0].fX, r1[0].fY);
  2180. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  2181. path1.lineTo(r1[index].fX, r1[index].fY);
  2182. }
  2183. path1.close();
  2184. path1.lineTo(1, 0);
  2185. if (!rectFirst) {
  2186. path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
  2187. }
  2188. REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
  2189. // fail, move in the middle
  2190. path1.reset();
  2191. if (rectFirst) {
  2192. path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
  2193. }
  2194. path1.moveTo(r1[0].fX, r1[0].fY);
  2195. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  2196. if (index == 2) {
  2197. path1.moveTo(1, .5f);
  2198. }
  2199. path1.lineTo(r1[index].fX, r1[index].fY);
  2200. }
  2201. path1.close();
  2202. if (!rectFirst) {
  2203. path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
  2204. }
  2205. REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
  2206. // fail, move on the edge
  2207. path1.reset();
  2208. if (rectFirst) {
  2209. path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
  2210. }
  2211. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  2212. path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
  2213. path1.lineTo(r1[index].fX, r1[index].fY);
  2214. }
  2215. path1.close();
  2216. if (!rectFirst) {
  2217. path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
  2218. }
  2219. REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
  2220. // fail, quad
  2221. path1.reset();
  2222. if (rectFirst) {
  2223. path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
  2224. }
  2225. path1.moveTo(r1[0].fX, r1[0].fY);
  2226. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  2227. if (index == 2) {
  2228. path1.quadTo(1, .5f, 1, .5f);
  2229. }
  2230. path1.lineTo(r1[index].fX, r1[index].fY);
  2231. }
  2232. path1.close();
  2233. if (!rectFirst) {
  2234. path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
  2235. }
  2236. REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
  2237. // fail, cubic
  2238. path1.reset();
  2239. if (rectFirst) {
  2240. path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
  2241. }
  2242. path1.moveTo(r1[0].fX, r1[0].fY);
  2243. for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
  2244. if (index == 2) {
  2245. path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
  2246. }
  2247. path1.lineTo(r1[index].fX, r1[index].fY);
  2248. }
  2249. path1.close();
  2250. if (!rectFirst) {
  2251. path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
  2252. }
  2253. REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
  2254. // fail, not nested
  2255. path1.reset();
  2256. path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
  2257. path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
  2258. REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
  2259. }
  2260. // pass, constructed explicitly from manually closed rects specified as moves/lines.
  2261. SkPath path;
  2262. path.moveTo(0, 0);
  2263. path.lineTo(10, 0);
  2264. path.lineTo(10, 10);
  2265. path.lineTo(0, 10);
  2266. path.lineTo(0, 0);
  2267. path.moveTo(1, 1);
  2268. path.lineTo(9, 1);
  2269. path.lineTo(9, 9);
  2270. path.lineTo(1, 9);
  2271. path.lineTo(1, 1);
  2272. REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
  2273. // pass, stroke rect
  2274. SkPath src, dst;
  2275. src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
  2276. SkPaint strokePaint;
  2277. strokePaint.setStyle(SkPaint::kStroke_Style);
  2278. strokePaint.setStrokeWidth(2);
  2279. strokePaint.getFillPath(src, &dst);
  2280. REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
  2281. }
  2282. static void write_and_read_back(skiatest::Reporter* reporter,
  2283. const SkPath& p) {
  2284. SkWriter32 writer;
  2285. writer.writePath(p);
  2286. size_t size = writer.bytesWritten();
  2287. SkAutoMalloc storage(size);
  2288. writer.flatten(storage.get());
  2289. SkReader32 reader(storage.get(), size);
  2290. SkPath readBack;
  2291. REPORTER_ASSERT(reporter, readBack != p);
  2292. reader.readPath(&readBack);
  2293. REPORTER_ASSERT(reporter, readBack == p);
  2294. REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
  2295. p.getConvexityOrUnknown());
  2296. SkRect oval0, oval1;
  2297. SkPath::Direction dir0, dir1;
  2298. unsigned start0, start1;
  2299. REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
  2300. if (SkPathPriv::IsOval(p, &oval0, &dir0, &start0) &&
  2301. SkPathPriv::IsOval(readBack, &oval1, &dir1, &start1)) {
  2302. REPORTER_ASSERT(reporter, oval0 == oval1);
  2303. REPORTER_ASSERT(reporter, dir0 == dir1);
  2304. REPORTER_ASSERT(reporter, start0 == start1);
  2305. }
  2306. REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
  2307. SkRRect rrect0, rrect1;
  2308. if (SkPathPriv::IsRRect(p, &rrect0, &dir0, &start0) &&
  2309. SkPathPriv::IsRRect(readBack, &rrect1, &dir1, &start1)) {
  2310. REPORTER_ASSERT(reporter, rrect0 == rrect1);
  2311. REPORTER_ASSERT(reporter, dir0 == dir1);
  2312. REPORTER_ASSERT(reporter, start0 == start1);
  2313. }
  2314. const SkRect& origBounds = p.getBounds();
  2315. const SkRect& readBackBounds = readBack.getBounds();
  2316. REPORTER_ASSERT(reporter, origBounds == readBackBounds);
  2317. }
  2318. static void test_flattening(skiatest::Reporter* reporter) {
  2319. SkPath p;
  2320. static const SkPoint pts[] = {
  2321. { 0, 0 },
  2322. { SkIntToScalar(10), SkIntToScalar(10) },
  2323. { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
  2324. { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
  2325. };
  2326. p.moveTo(pts[0]);
  2327. p.lineTo(pts[1]);
  2328. p.quadTo(pts[2], pts[3]);
  2329. p.cubicTo(pts[4], pts[5], pts[6]);
  2330. write_and_read_back(reporter, p);
  2331. // create a buffer that should be much larger than the path so we don't
  2332. // kill our stack if writer goes too far.
  2333. char buffer[1024];
  2334. size_t size1 = p.writeToMemory(nullptr);
  2335. size_t size2 = p.writeToMemory(buffer);
  2336. REPORTER_ASSERT(reporter, size1 == size2);
  2337. SkPath p2;
  2338. size_t size3 = p2.readFromMemory(buffer, 1024);
  2339. REPORTER_ASSERT(reporter, size1 == size3);
  2340. REPORTER_ASSERT(reporter, p == p2);
  2341. size3 = p2.readFromMemory(buffer, 0);
  2342. REPORTER_ASSERT(reporter, !size3);
  2343. SkPath tooShort;
  2344. size3 = tooShort.readFromMemory(buffer, size1 - 1);
  2345. REPORTER_ASSERT(reporter, tooShort.isEmpty());
  2346. char buffer2[1024];
  2347. size3 = p2.writeToMemory(buffer2);
  2348. REPORTER_ASSERT(reporter, size1 == size3);
  2349. REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
  2350. // test persistence of the oval flag & convexity
  2351. {
  2352. SkPath oval;
  2353. SkRect rect = SkRect::MakeWH(10, 10);
  2354. oval.addOval(rect);
  2355. write_and_read_back(reporter, oval);
  2356. }
  2357. }
  2358. static void test_transform(skiatest::Reporter* reporter) {
  2359. SkPath p;
  2360. #define CONIC_PERSPECTIVE_BUG_FIXED 0
  2361. static const SkPoint pts[] = {
  2362. { 0, 0 }, // move
  2363. { SkIntToScalar(10), SkIntToScalar(10) }, // line
  2364. { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
  2365. { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
  2366. #if CONIC_PERSPECTIVE_BUG_FIXED
  2367. { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
  2368. #endif
  2369. };
  2370. const int kPtCount = SK_ARRAY_COUNT(pts);
  2371. p.moveTo(pts[0]);
  2372. p.lineTo(pts[1]);
  2373. p.quadTo(pts[2], pts[3]);
  2374. p.cubicTo(pts[4], pts[5], pts[6]);
  2375. #if CONIC_PERSPECTIVE_BUG_FIXED
  2376. p.conicTo(pts[4], pts[5], 0.5f);
  2377. #endif
  2378. p.close();
  2379. {
  2380. SkMatrix matrix;
  2381. matrix.reset();
  2382. SkPath p1;
  2383. p.transform(matrix, &p1);
  2384. REPORTER_ASSERT(reporter, p == p1);
  2385. }
  2386. {
  2387. SkMatrix matrix;
  2388. matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
  2389. SkPath p1; // Leave p1 non-unique (i.e., the empty path)
  2390. p.transform(matrix, &p1);
  2391. SkPoint pts1[kPtCount];
  2392. int count = p1.getPoints(pts1, kPtCount);
  2393. REPORTER_ASSERT(reporter, kPtCount == count);
  2394. for (int i = 0; i < count; ++i) {
  2395. SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
  2396. REPORTER_ASSERT(reporter, newPt == pts1[i]);
  2397. }
  2398. }
  2399. {
  2400. SkMatrix matrix;
  2401. matrix.reset();
  2402. matrix.setPerspX(4);
  2403. SkPath p1;
  2404. p1.moveTo(SkPoint::Make(0, 0));
  2405. p.transform(matrix, &p1);
  2406. REPORTER_ASSERT(reporter, matrix.invert(&matrix));
  2407. p1.transform(matrix, nullptr);
  2408. SkRect pBounds = p.getBounds();
  2409. SkRect p1Bounds = p1.getBounds();
  2410. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
  2411. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
  2412. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
  2413. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
  2414. }
  2415. p.reset();
  2416. p.addCircle(0, 0, 1, SkPath::kCW_Direction);
  2417. {
  2418. SkMatrix matrix;
  2419. matrix.reset();
  2420. SkPath p1;
  2421. p1.moveTo(SkPoint::Make(0, 0));
  2422. p.transform(matrix, &p1);
  2423. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
  2424. }
  2425. {
  2426. SkMatrix matrix;
  2427. matrix.reset();
  2428. matrix.setScaleX(-1);
  2429. SkPath p1;
  2430. p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
  2431. p.transform(matrix, &p1);
  2432. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
  2433. }
  2434. {
  2435. SkMatrix matrix;
  2436. matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
  2437. SkPath p1;
  2438. p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
  2439. p.transform(matrix, &p1);
  2440. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
  2441. }
  2442. {
  2443. SkPath p1;
  2444. p1.addRect({ 10, 20, 30, 40 });
  2445. SkPath p2;
  2446. p2.addRect({ 10, 20, 30, 40 });
  2447. uint32_t id1 = p1.getGenerationID();
  2448. uint32_t id2 = p2.getGenerationID();
  2449. SkMatrix matrix;
  2450. matrix.setScale(2, 2);
  2451. p1.transform(matrix, &p2);
  2452. p1.transform(matrix);
  2453. REPORTER_ASSERT(reporter, id1 != p1.getGenerationID());
  2454. REPORTER_ASSERT(reporter, id2 != p2.getGenerationID());
  2455. }
  2456. }
  2457. static void test_zero_length_paths(skiatest::Reporter* reporter) {
  2458. SkPath p;
  2459. uint8_t verbs[32];
  2460. struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
  2461. const char* testPath;
  2462. const size_t numResultPts;
  2463. const SkRect resultBound;
  2464. const SkPath::Verb* resultVerbs;
  2465. const size_t numResultVerbs;
  2466. };
  2467. static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
  2468. static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
  2469. static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
  2470. static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
  2471. static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
  2472. static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
  2473. static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
  2474. static const SkPath::Verb resultVerbs8[] = {
  2475. SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
  2476. };
  2477. static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
  2478. static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
  2479. static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
  2480. static const SkPath::Verb resultVerbs12[] = {
  2481. SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
  2482. };
  2483. static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
  2484. static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
  2485. static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
  2486. static const SkPath::Verb resultVerbs16[] = {
  2487. SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
  2488. };
  2489. static const struct zeroPathTestData gZeroLengthTests[] = {
  2490. { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2491. { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
  2492. { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
  2493. { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
  2494. { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
  2495. { "M 1 1 L 1 1 M 2 1 L 2 1", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs6, SK_ARRAY_COUNT(resultVerbs6) },
  2496. { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
  2497. { "M 1 1 L 1 1 z M 2 1 L 2 1 z", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs8, SK_ARRAY_COUNT(resultVerbs8) },
  2498. { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
  2499. { "M 1 1 Q 1 1 1 1 M 2 1 Q 2 1 2 1", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs10, SK_ARRAY_COUNT(resultVerbs10) },
  2500. { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
  2501. { "M 1 1 Q 1 1 1 1 z M 2 1 Q 2 1 2 1 z", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs12, SK_ARRAY_COUNT(resultVerbs12) },
  2502. { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
  2503. { "M 1 1 C 1 1 1 1 1 1 M 2 1 C 2 1 2 1 2 1", 8, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs14,
  2504. SK_ARRAY_COUNT(resultVerbs14)
  2505. },
  2506. { "M 1 1 C 1 1 1 1 1 1 z", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs15, SK_ARRAY_COUNT(resultVerbs15) },
  2507. { "M 1 1 C 1 1 1 1 1 1 z M 2 1 C 2 1 2 1 2 1 z", 8, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs16,
  2508. SK_ARRAY_COUNT(resultVerbs16)
  2509. }
  2510. };
  2511. for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
  2512. p.reset();
  2513. bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
  2514. REPORTER_ASSERT(reporter, valid);
  2515. REPORTER_ASSERT(reporter, !p.isEmpty());
  2516. REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
  2517. REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
  2518. REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
  2519. for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
  2520. REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
  2521. }
  2522. }
  2523. }
  2524. struct SegmentInfo {
  2525. SkPath fPath;
  2526. int fPointCount;
  2527. };
  2528. #define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
  2529. static void test_segment_masks(skiatest::Reporter* reporter) {
  2530. SkPath p, p2;
  2531. p.moveTo(0, 0);
  2532. p.quadTo(100, 100, 200, 200);
  2533. REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
  2534. REPORTER_ASSERT(reporter, !p.isEmpty());
  2535. p2 = p;
  2536. REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
  2537. p.cubicTo(100, 100, 200, 200, 300, 300);
  2538. REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
  2539. REPORTER_ASSERT(reporter, !p.isEmpty());
  2540. p2 = p;
  2541. REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
  2542. p.reset();
  2543. p.moveTo(0, 0);
  2544. p.cubicTo(100, 100, 200, 200, 300, 300);
  2545. REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
  2546. p2 = p;
  2547. REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
  2548. REPORTER_ASSERT(reporter, !p.isEmpty());
  2549. }
  2550. static void test_iter(skiatest::Reporter* reporter) {
  2551. SkPath p;
  2552. SkPoint pts[4];
  2553. // Test an iterator with no path
  2554. SkPath::Iter noPathIter;
  2555. REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
  2556. // Test that setting an empty path works
  2557. noPathIter.setPath(p, false);
  2558. REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
  2559. // Test that close path makes no difference for an empty path
  2560. noPathIter.setPath(p, true);
  2561. REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
  2562. // Test an iterator with an initial empty path
  2563. SkPath::Iter iter(p, false);
  2564. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
  2565. // Test that close path makes no difference
  2566. iter.setPath(p, true);
  2567. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
  2568. struct iterTestData {
  2569. const char* testPath;
  2570. const bool forceClose;
  2571. const bool consumeDegenerates;
  2572. const size_t* numResultPtsPerVerb;
  2573. const SkPoint* resultPts;
  2574. const SkPath::Verb* resultVerbs;
  2575. const size_t numResultVerbs;
  2576. };
  2577. static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
  2578. static const SkPath::Verb resultVerbs2[] = {
  2579. SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
  2580. };
  2581. static const SkPath::Verb resultVerbs3[] = {
  2582. SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
  2583. };
  2584. static const SkPath::Verb resultVerbs4[] = {
  2585. SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
  2586. };
  2587. static const SkPath::Verb resultVerbs5[] = {
  2588. SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
  2589. };
  2590. static const size_t resultPtsSizes1[] = { 0 };
  2591. static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
  2592. static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
  2593. static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
  2594. static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
  2595. static const SkPoint* resultPts1 = nullptr;
  2596. static const SkPoint resultPts2[] = {
  2597. { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
  2598. };
  2599. static const SkPoint resultPts3[] = {
  2600. { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
  2601. { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
  2602. };
  2603. static const SkPoint resultPts4[] = {
  2604. { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
  2605. };
  2606. static const SkPoint resultPts5[] = {
  2607. { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
  2608. };
  2609. static const struct iterTestData gIterTests[] = {
  2610. { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2611. { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2612. { "M 1 0 M 1 0 M 3 0 M 4 0 M 5 0", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2613. { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2614. { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2615. { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2616. { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2617. { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
  2618. { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
  2619. { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2620. { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
  2621. { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
  2622. { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }
  2623. };
  2624. for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
  2625. p.reset();
  2626. bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
  2627. REPORTER_ASSERT(reporter, valid);
  2628. iter.setPath(p, gIterTests[i].forceClose);
  2629. int j = 0, l = 0;
  2630. do {
  2631. REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
  2632. for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
  2633. REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
  2634. }
  2635. } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
  2636. REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
  2637. }
  2638. p.reset();
  2639. iter.setPath(p, false);
  2640. REPORTER_ASSERT(reporter, !iter.isClosedContour());
  2641. p.lineTo(1, 1);
  2642. p.close();
  2643. iter.setPath(p, false);
  2644. REPORTER_ASSERT(reporter, iter.isClosedContour());
  2645. p.reset();
  2646. iter.setPath(p, true);
  2647. REPORTER_ASSERT(reporter, !iter.isClosedContour());
  2648. p.lineTo(1, 1);
  2649. iter.setPath(p, true);
  2650. REPORTER_ASSERT(reporter, iter.isClosedContour());
  2651. p.moveTo(0, 0);
  2652. p.lineTo(2, 2);
  2653. iter.setPath(p, false);
  2654. REPORTER_ASSERT(reporter, !iter.isClosedContour());
  2655. // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
  2656. // check to see if the result is correct.
  2657. for (int setNaN = 0; setNaN < 4; ++setNaN) {
  2658. p.reset();
  2659. p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
  2660. p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
  2661. iter.setPath(p, true);
  2662. iter.next(pts, false);
  2663. iter.next(pts, false);
  2664. REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
  2665. }
  2666. p.reset();
  2667. p.quadTo(0, 0, 0, 0);
  2668. iter.setPath(p, false);
  2669. iter.next(pts, false);
  2670. REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
  2671. iter.setPath(p, false);
  2672. iter.next(pts, false);
  2673. REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
  2674. p.reset();
  2675. p.conicTo(0, 0, 0, 0, 0.5f);
  2676. iter.setPath(p, false);
  2677. iter.next(pts, false);
  2678. REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
  2679. iter.setPath(p, false);
  2680. iter.next(pts, false);
  2681. REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
  2682. p.reset();
  2683. p.cubicTo(0, 0, 0, 0, 0, 0);
  2684. iter.setPath(p, false);
  2685. iter.next(pts, false);
  2686. REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
  2687. iter.setPath(p, false);
  2688. iter.next(pts, false);
  2689. REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
  2690. p.moveTo(1, 1); // add a trailing moveto
  2691. iter.setPath(p, false);
  2692. iter.next(pts, false);
  2693. REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
  2694. iter.setPath(p, false);
  2695. iter.next(pts, false);
  2696. REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
  2697. // The GM degeneratesegments.cpp test is more extensive
  2698. // Test out mixed degenerate and non-degenerate geometry with Conics
  2699. const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
  2700. SkRect r = SkRect::MakeWH(100, 100);
  2701. SkRRect rr;
  2702. rr.setRectRadii(r, radii);
  2703. p.reset();
  2704. p.addRRect(rr);
  2705. iter.setPath(p, false);
  2706. REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
  2707. REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
  2708. REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
  2709. REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
  2710. REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
  2711. }
  2712. static void test_raw_iter(skiatest::Reporter* reporter) {
  2713. SkPath p;
  2714. SkPoint pts[4];
  2715. // Test an iterator with no path
  2716. SkPath::RawIter noPathIter;
  2717. REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
  2718. // Test that setting an empty path works
  2719. noPathIter.setPath(p);
  2720. REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
  2721. // Test an iterator with an initial empty path
  2722. SkPath::RawIter iter(p);
  2723. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
  2724. // Test that a move-only path returns the move.
  2725. p.moveTo(SK_Scalar1, 0);
  2726. iter.setPath(p);
  2727. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
  2728. REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
  2729. REPORTER_ASSERT(reporter, pts[0].fY == 0);
  2730. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
  2731. // No matter how many moves we add, we should get them all back
  2732. p.moveTo(SK_Scalar1*2, SK_Scalar1);
  2733. p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
  2734. iter.setPath(p);
  2735. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
  2736. REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
  2737. REPORTER_ASSERT(reporter, pts[0].fY == 0);
  2738. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
  2739. REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
  2740. REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
  2741. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
  2742. REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
  2743. REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
  2744. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
  2745. // Initial close is never ever stored
  2746. p.reset();
  2747. p.close();
  2748. iter.setPath(p);
  2749. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
  2750. // Move/close sequences
  2751. p.reset();
  2752. p.close(); // Not stored, no purpose
  2753. p.moveTo(SK_Scalar1, 0);
  2754. p.close();
  2755. p.close(); // Not stored, no purpose
  2756. p.moveTo(SK_Scalar1*2, SK_Scalar1);
  2757. p.close();
  2758. p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
  2759. p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
  2760. p.close();
  2761. iter.setPath(p);
  2762. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
  2763. REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
  2764. REPORTER_ASSERT(reporter, pts[0].fY == 0);
  2765. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
  2766. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
  2767. REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
  2768. REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
  2769. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
  2770. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
  2771. REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
  2772. REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
  2773. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
  2774. REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
  2775. REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
  2776. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
  2777. REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
  2778. // Generate random paths and verify
  2779. SkPoint randomPts[25];
  2780. for (int i = 0; i < 5; ++i) {
  2781. for (int j = 0; j < 5; ++j) {
  2782. randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
  2783. }
  2784. }
  2785. // Max of 10 segments, max 3 points per segment
  2786. SkRandom rand(9876543);
  2787. SkPoint expectedPts[31]; // May have leading moveTo
  2788. SkPath::Verb expectedVerbs[22]; // May have leading moveTo
  2789. SkPath::Verb nextVerb;
  2790. for (int i = 0; i < 500; ++i) {
  2791. p.reset();
  2792. bool lastWasClose = true;
  2793. bool haveMoveTo = false;
  2794. SkPoint lastMoveToPt = { 0, 0 };
  2795. int numPoints = 0;
  2796. int numVerbs = (rand.nextU() >> 16) % 10;
  2797. int numIterVerbs = 0;
  2798. for (int j = 0; j < numVerbs; ++j) {
  2799. do {
  2800. nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
  2801. } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
  2802. switch (nextVerb) {
  2803. case SkPath::kMove_Verb:
  2804. expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
  2805. p.moveTo(expectedPts[numPoints]);
  2806. lastMoveToPt = expectedPts[numPoints];
  2807. numPoints += 1;
  2808. lastWasClose = false;
  2809. haveMoveTo = true;
  2810. break;
  2811. case SkPath::kLine_Verb:
  2812. if (!haveMoveTo) {
  2813. expectedPts[numPoints++] = lastMoveToPt;
  2814. expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
  2815. haveMoveTo = true;
  2816. }
  2817. expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
  2818. p.lineTo(expectedPts[numPoints]);
  2819. numPoints += 1;
  2820. lastWasClose = false;
  2821. break;
  2822. case SkPath::kQuad_Verb:
  2823. if (!haveMoveTo) {
  2824. expectedPts[numPoints++] = lastMoveToPt;
  2825. expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
  2826. haveMoveTo = true;
  2827. }
  2828. expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
  2829. expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
  2830. p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
  2831. numPoints += 2;
  2832. lastWasClose = false;
  2833. break;
  2834. case SkPath::kConic_Verb:
  2835. if (!haveMoveTo) {
  2836. expectedPts[numPoints++] = lastMoveToPt;
  2837. expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
  2838. haveMoveTo = true;
  2839. }
  2840. expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
  2841. expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
  2842. p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
  2843. rand.nextUScalar1() * 4);
  2844. numPoints += 2;
  2845. lastWasClose = false;
  2846. break;
  2847. case SkPath::kCubic_Verb:
  2848. if (!haveMoveTo) {
  2849. expectedPts[numPoints++] = lastMoveToPt;
  2850. expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
  2851. haveMoveTo = true;
  2852. }
  2853. expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
  2854. expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
  2855. expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
  2856. p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
  2857. expectedPts[numPoints + 2]);
  2858. numPoints += 3;
  2859. lastWasClose = false;
  2860. break;
  2861. case SkPath::kClose_Verb:
  2862. p.close();
  2863. haveMoveTo = false;
  2864. lastWasClose = true;
  2865. break;
  2866. default:
  2867. SkDEBUGFAIL("unexpected verb");
  2868. }
  2869. expectedVerbs[numIterVerbs++] = nextVerb;
  2870. }
  2871. iter.setPath(p);
  2872. numVerbs = numIterVerbs;
  2873. numIterVerbs = 0;
  2874. int numIterPts = 0;
  2875. SkPoint lastMoveTo;
  2876. SkPoint lastPt;
  2877. lastMoveTo.set(0, 0);
  2878. lastPt.set(0, 0);
  2879. while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
  2880. REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
  2881. numIterVerbs++;
  2882. switch (nextVerb) {
  2883. case SkPath::kMove_Verb:
  2884. REPORTER_ASSERT(reporter, numIterPts < numPoints);
  2885. REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
  2886. lastPt = lastMoveTo = pts[0];
  2887. numIterPts += 1;
  2888. break;
  2889. case SkPath::kLine_Verb:
  2890. REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
  2891. REPORTER_ASSERT(reporter, pts[0] == lastPt);
  2892. REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
  2893. lastPt = pts[1];
  2894. numIterPts += 1;
  2895. break;
  2896. case SkPath::kQuad_Verb:
  2897. case SkPath::kConic_Verb:
  2898. REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
  2899. REPORTER_ASSERT(reporter, pts[0] == lastPt);
  2900. REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
  2901. REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
  2902. lastPt = pts[2];
  2903. numIterPts += 2;
  2904. break;
  2905. case SkPath::kCubic_Verb:
  2906. REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
  2907. REPORTER_ASSERT(reporter, pts[0] == lastPt);
  2908. REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
  2909. REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
  2910. REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
  2911. lastPt = pts[3];
  2912. numIterPts += 3;
  2913. break;
  2914. case SkPath::kClose_Verb:
  2915. lastPt = lastMoveTo;
  2916. break;
  2917. default:
  2918. SkDEBUGFAIL("unexpected verb");
  2919. }
  2920. }
  2921. REPORTER_ASSERT(reporter, numIterPts == numPoints);
  2922. REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
  2923. }
  2924. }
  2925. static void check_for_circle(skiatest::Reporter* reporter,
  2926. const SkPath& path,
  2927. bool expectedCircle,
  2928. SkPathPriv::FirstDirection expectedDir) {
  2929. SkRect rect = SkRect::MakeEmpty();
  2930. REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
  2931. SkPath::Direction isOvalDir;
  2932. unsigned isOvalStart;
  2933. if (SkPathPriv::IsOval(path, &rect, &isOvalDir, &isOvalStart)) {
  2934. REPORTER_ASSERT(reporter, rect.height() == rect.width());
  2935. REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
  2936. SkPath tmpPath;
  2937. tmpPath.addOval(rect, isOvalDir, isOvalStart);
  2938. REPORTER_ASSERT(reporter, path == tmpPath);
  2939. }
  2940. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
  2941. }
  2942. static void test_circle_skew(skiatest::Reporter* reporter,
  2943. const SkPath& path,
  2944. SkPathPriv::FirstDirection dir) {
  2945. SkPath tmp;
  2946. SkMatrix m;
  2947. m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
  2948. path.transform(m, &tmp);
  2949. // this matrix reverses the direction.
  2950. if (SkPathPriv::kCCW_FirstDirection == dir) {
  2951. dir = SkPathPriv::kCW_FirstDirection;
  2952. } else {
  2953. REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
  2954. dir = SkPathPriv::kCCW_FirstDirection;
  2955. }
  2956. check_for_circle(reporter, tmp, false, dir);
  2957. }
  2958. static void test_circle_translate(skiatest::Reporter* reporter,
  2959. const SkPath& path,
  2960. SkPathPriv::FirstDirection dir) {
  2961. SkPath tmp;
  2962. // translate at small offset
  2963. SkMatrix m;
  2964. m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
  2965. path.transform(m, &tmp);
  2966. check_for_circle(reporter, tmp, true, dir);
  2967. tmp.reset();
  2968. m.reset();
  2969. // translate at a relatively big offset
  2970. m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
  2971. path.transform(m, &tmp);
  2972. check_for_circle(reporter, tmp, true, dir);
  2973. }
  2974. static void test_circle_rotate(skiatest::Reporter* reporter,
  2975. const SkPath& path,
  2976. SkPathPriv::FirstDirection dir) {
  2977. for (int angle = 0; angle < 360; ++angle) {
  2978. SkPath tmp;
  2979. SkMatrix m;
  2980. m.setRotate(SkIntToScalar(angle));
  2981. path.transform(m, &tmp);
  2982. // TODO: a rotated circle whose rotated angle is not a multiple of 90
  2983. // degrees is not an oval anymore, this can be improved. we made this
  2984. // for the simplicity of our implementation.
  2985. if (angle % 90 == 0) {
  2986. check_for_circle(reporter, tmp, true, dir);
  2987. } else {
  2988. check_for_circle(reporter, tmp, false, dir);
  2989. }
  2990. }
  2991. }
  2992. static void test_circle_mirror_x(skiatest::Reporter* reporter,
  2993. const SkPath& path,
  2994. SkPathPriv::FirstDirection dir) {
  2995. SkPath tmp;
  2996. SkMatrix m;
  2997. m.reset();
  2998. m.setScaleX(-SK_Scalar1);
  2999. path.transform(m, &tmp);
  3000. if (SkPathPriv::kCW_FirstDirection == dir) {
  3001. dir = SkPathPriv::kCCW_FirstDirection;
  3002. } else {
  3003. REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
  3004. dir = SkPathPriv::kCW_FirstDirection;
  3005. }
  3006. check_for_circle(reporter, tmp, true, dir);
  3007. }
  3008. static void test_circle_mirror_y(skiatest::Reporter* reporter,
  3009. const SkPath& path,
  3010. SkPathPriv::FirstDirection dir) {
  3011. SkPath tmp;
  3012. SkMatrix m;
  3013. m.reset();
  3014. m.setScaleY(-SK_Scalar1);
  3015. path.transform(m, &tmp);
  3016. if (SkPathPriv::kCW_FirstDirection == dir) {
  3017. dir = SkPathPriv::kCCW_FirstDirection;
  3018. } else {
  3019. REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
  3020. dir = SkPathPriv::kCW_FirstDirection;
  3021. }
  3022. check_for_circle(reporter, tmp, true, dir);
  3023. }
  3024. static void test_circle_mirror_xy(skiatest::Reporter* reporter,
  3025. const SkPath& path,
  3026. SkPathPriv::FirstDirection dir) {
  3027. SkPath tmp;
  3028. SkMatrix m;
  3029. m.reset();
  3030. m.setScaleX(-SK_Scalar1);
  3031. m.setScaleY(-SK_Scalar1);
  3032. path.transform(m, &tmp);
  3033. check_for_circle(reporter, tmp, true, dir);
  3034. }
  3035. static void test_circle_with_direction(skiatest::Reporter* reporter,
  3036. SkPath::Direction inDir) {
  3037. const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
  3038. SkPath path;
  3039. // circle at origin
  3040. path.addCircle(0, 0, SkIntToScalar(20), inDir);
  3041. check_for_circle(reporter, path, true, dir);
  3042. test_circle_rotate(reporter, path, dir);
  3043. test_circle_translate(reporter, path, dir);
  3044. test_circle_skew(reporter, path, dir);
  3045. test_circle_mirror_x(reporter, path, dir);
  3046. test_circle_mirror_y(reporter, path, dir);
  3047. test_circle_mirror_xy(reporter, path, dir);
  3048. // circle at an offset at (10, 10)
  3049. path.reset();
  3050. path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
  3051. SkIntToScalar(20), inDir);
  3052. check_for_circle(reporter, path, true, dir);
  3053. test_circle_rotate(reporter, path, dir);
  3054. test_circle_translate(reporter, path, dir);
  3055. test_circle_skew(reporter, path, dir);
  3056. test_circle_mirror_x(reporter, path, dir);
  3057. test_circle_mirror_y(reporter, path, dir);
  3058. test_circle_mirror_xy(reporter, path, dir);
  3059. // Try different starting points for the contour.
  3060. for (unsigned start = 0; start < 4; ++start) {
  3061. path.reset();
  3062. path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
  3063. test_circle_rotate(reporter, path, dir);
  3064. test_circle_translate(reporter, path, dir);
  3065. test_circle_skew(reporter, path, dir);
  3066. test_circle_mirror_x(reporter, path, dir);
  3067. test_circle_mirror_y(reporter, path, dir);
  3068. test_circle_mirror_xy(reporter, path, dir);
  3069. }
  3070. }
  3071. static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
  3072. SkPath path;
  3073. SkPath circle;
  3074. SkPath rect;
  3075. SkPath empty;
  3076. const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
  3077. const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
  3078. circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
  3079. rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
  3080. SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
  3081. SkMatrix translate;
  3082. translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
  3083. // Although all the path concatenation related operations leave
  3084. // the path a circle, most mark it as a non-circle for simplicity
  3085. // empty + circle (translate)
  3086. path = empty;
  3087. path.addPath(circle, translate);
  3088. check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
  3089. // circle + empty (translate)
  3090. path = circle;
  3091. path.addPath(empty, translate);
  3092. check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
  3093. // test reverseAddPath
  3094. path = circle;
  3095. path.reverseAddPath(rect);
  3096. check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
  3097. }
  3098. static void test_circle(skiatest::Reporter* reporter) {
  3099. test_circle_with_direction(reporter, SkPath::kCW_Direction);
  3100. test_circle_with_direction(reporter, SkPath::kCCW_Direction);
  3101. // multiple addCircle()
  3102. SkPath path;
  3103. path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
  3104. path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
  3105. check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
  3106. // some extra lineTo() would make isOval() fail
  3107. path.reset();
  3108. path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
  3109. path.lineTo(0, 0);
  3110. check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
  3111. // not back to the original point
  3112. path.reset();
  3113. path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
  3114. path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
  3115. check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
  3116. test_circle_with_add_paths(reporter);
  3117. // test negative radius
  3118. path.reset();
  3119. path.addCircle(0, 0, -1, SkPath::kCW_Direction);
  3120. REPORTER_ASSERT(reporter, path.isEmpty());
  3121. }
  3122. static void test_oval(skiatest::Reporter* reporter) {
  3123. SkRect rect;
  3124. SkMatrix m;
  3125. SkPath path;
  3126. unsigned start = 0;
  3127. SkPath::Direction dir = SkPath::kCCW_Direction;
  3128. rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
  3129. path.addOval(rect);
  3130. // Defaults to dir = CW and start = 1
  3131. REPORTER_ASSERT(reporter, path.isOval(nullptr));
  3132. m.setRotate(SkIntToScalar(90));
  3133. SkPath tmp;
  3134. path.transform(m, &tmp);
  3135. // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
  3136. // is unchanged.
  3137. REPORTER_ASSERT(reporter, SkPathPriv::IsOval(tmp, nullptr, &dir, &start));
  3138. REPORTER_ASSERT(reporter, 2 == start);
  3139. REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
  3140. m.reset();
  3141. m.setRotate(SkIntToScalar(30));
  3142. tmp.reset();
  3143. path.transform(m, &tmp);
  3144. // an oval rotated 30 degrees is not an oval anymore.
  3145. REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
  3146. // since empty path being transformed.
  3147. path.reset();
  3148. tmp.reset();
  3149. m.reset();
  3150. path.transform(m, &tmp);
  3151. REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
  3152. // empty path is not an oval
  3153. tmp.reset();
  3154. REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
  3155. // only has moveTo()s
  3156. tmp.reset();
  3157. tmp.moveTo(0, 0);
  3158. tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
  3159. REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
  3160. // mimic WebKit's calling convention,
  3161. // call moveTo() first and then call addOval()
  3162. path.reset();
  3163. path.moveTo(0, 0);
  3164. path.addOval(rect);
  3165. REPORTER_ASSERT(reporter, path.isOval(nullptr));
  3166. // copy path
  3167. path.reset();
  3168. tmp.reset();
  3169. tmp.addOval(rect);
  3170. path = tmp;
  3171. REPORTER_ASSERT(reporter, SkPathPriv::IsOval(path, nullptr, &dir, &start));
  3172. REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
  3173. REPORTER_ASSERT(reporter, 1 == start);
  3174. }
  3175. static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
  3176. SkPath empty;
  3177. REPORTER_ASSERT(reporter, p.isEmpty());
  3178. REPORTER_ASSERT(reporter, 0 == p.countPoints());
  3179. REPORTER_ASSERT(reporter, 0 == p.countVerbs());
  3180. REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
  3181. REPORTER_ASSERT(reporter, p.isConvex());
  3182. REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
  3183. REPORTER_ASSERT(reporter, !p.isInverseFillType());
  3184. REPORTER_ASSERT(reporter, p == empty);
  3185. REPORTER_ASSERT(reporter, !(p != empty));
  3186. }
  3187. static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
  3188. SkPath::Direction dir) {
  3189. REPORTER_ASSERT(reporter, path->isConvex());
  3190. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
  3191. path->setConvexity(SkPath::kUnknown_Convexity);
  3192. REPORTER_ASSERT(reporter, path->isConvex());
  3193. path->reset();
  3194. }
  3195. static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
  3196. SkPath::Direction dir) {
  3197. REPORTER_ASSERT(reporter, path->isConvex());
  3198. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
  3199. path->setConvexity(SkPath::kUnknown_Convexity);
  3200. REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kConcave_Convexity);
  3201. path->reset();
  3202. }
  3203. static void test_rrect(skiatest::Reporter* reporter) {
  3204. SkPath p;
  3205. SkRRect rr;
  3206. SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
  3207. SkRect r = {10, 20, 30, 40};
  3208. rr.setRectRadii(r, radii);
  3209. p.addRRect(rr);
  3210. test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
  3211. p.addRRect(rr, SkPath::kCCW_Direction);
  3212. test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
  3213. p.addRoundRect(r, &radii[0].fX);
  3214. test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
  3215. p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
  3216. test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
  3217. p.addRoundRect(r, radii[1].fX, radii[1].fY);
  3218. test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
  3219. p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
  3220. test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
  3221. for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
  3222. SkVector save = radii[i];
  3223. radii[i].set(0, 0);
  3224. rr.setRectRadii(r, radii);
  3225. p.addRRect(rr);
  3226. test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
  3227. radii[i] = save;
  3228. }
  3229. p.addRoundRect(r, 0, 0);
  3230. SkRect returnedRect;
  3231. REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
  3232. REPORTER_ASSERT(reporter, returnedRect == r);
  3233. test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
  3234. SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
  3235. rr.setRectRadii(r, zeroRadii);
  3236. p.addRRect(rr);
  3237. bool closed;
  3238. SkPath::Direction dir;
  3239. REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
  3240. REPORTER_ASSERT(reporter, closed);
  3241. REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
  3242. test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
  3243. p.addRRect(rr, SkPath::kCW_Direction);
  3244. p.addRRect(rr, SkPath::kCW_Direction);
  3245. REPORTER_ASSERT(reporter, !p.isConvex());
  3246. p.reset();
  3247. p.addRRect(rr, SkPath::kCCW_Direction);
  3248. p.addRRect(rr, SkPath::kCCW_Direction);
  3249. REPORTER_ASSERT(reporter, !p.isConvex());
  3250. p.reset();
  3251. SkRect emptyR = {10, 20, 10, 30};
  3252. rr.setRectRadii(emptyR, radii);
  3253. p.addRRect(rr);
  3254. // The round rect is "empty" in that it has no fill area. However,
  3255. // the path isn't "empty" in that it should have verbs and points.
  3256. REPORTER_ASSERT(reporter, !p.isEmpty());
  3257. p.reset();
  3258. SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
  3259. rr.setRectRadii(largeR, radii);
  3260. p.addRRect(rr);
  3261. test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
  3262. // we check for non-finites
  3263. SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
  3264. rr.setRectRadii(infR, radii);
  3265. REPORTER_ASSERT(reporter, rr.isEmpty());
  3266. SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
  3267. p.addRoundRect(tinyR, 5e-11f, 5e-11f);
  3268. test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
  3269. }
  3270. static void test_arc(skiatest::Reporter* reporter) {
  3271. SkPath p;
  3272. SkRect emptyOval = {10, 20, 30, 20};
  3273. REPORTER_ASSERT(reporter, emptyOval.isEmpty());
  3274. p.addArc(emptyOval, 1, 2);
  3275. REPORTER_ASSERT(reporter, p.isEmpty());
  3276. p.reset();
  3277. SkRect oval = {10, 20, 30, 40};
  3278. p.addArc(oval, 1, 0);
  3279. REPORTER_ASSERT(reporter, p.isEmpty());
  3280. p.reset();
  3281. SkPath cwOval;
  3282. cwOval.addOval(oval);
  3283. p.addArc(oval, 0, 360);
  3284. REPORTER_ASSERT(reporter, p == cwOval);
  3285. p.reset();
  3286. SkPath ccwOval;
  3287. ccwOval.addOval(oval, SkPath::kCCW_Direction);
  3288. p.addArc(oval, 0, -360);
  3289. REPORTER_ASSERT(reporter, p == ccwOval);
  3290. p.reset();
  3291. p.addArc(oval, 1, 180);
  3292. // diagonal colinear points make arc convex
  3293. // TODO: one way to keep it concave would be to introduce interpolated on curve points
  3294. // between control points and computing the on curve point at scan conversion time
  3295. REPORTER_ASSERT(reporter, p.getConvexity() == SkPath::kConvex_Convexity);
  3296. REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
  3297. p.setConvexity(SkPath::kUnknown_Convexity);
  3298. REPORTER_ASSERT(reporter, p.getConvexity() == SkPath::kConvex_Convexity);
  3299. }
  3300. static inline SkScalar oval_start_index_to_angle(unsigned start) {
  3301. switch (start) {
  3302. case 0:
  3303. return 270.f;
  3304. case 1:
  3305. return 0.f;
  3306. case 2:
  3307. return 90.f;
  3308. case 3:
  3309. return 180.f;
  3310. default:
  3311. return -1.f;
  3312. }
  3313. }
  3314. static inline SkScalar canonical_start_angle(float angle) {
  3315. while (angle < 0.f) {
  3316. angle += 360.f;
  3317. }
  3318. while (angle >= 360.f) {
  3319. angle -= 360.f;
  3320. }
  3321. return angle;
  3322. }
  3323. static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
  3324. const SkPath& path) {
  3325. SkRect r = SkRect::MakeEmpty();
  3326. SkPath::Direction d = SkPath::kCCW_Direction;
  3327. unsigned s = ~0U;
  3328. bool isOval = SkPathPriv::IsOval(path, &r, &d, &s);
  3329. REPORTER_ASSERT(reporter, isOval);
  3330. SkPath recreatedPath;
  3331. recreatedPath.addOval(r, d, s);
  3332. REPORTER_ASSERT(reporter, path == recreatedPath);
  3333. REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
  3334. REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
  3335. }
  3336. static void test_arc_ovals(skiatest::Reporter* reporter) {
  3337. SkRect oval = SkRect::MakeWH(10, 20);
  3338. for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
  3339. for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
  3340. SkPath path;
  3341. path.addArc(oval, start, sweep);
  3342. // SkPath's interfaces for inserting and extracting ovals only allow contours
  3343. // to start at multiples of 90 degrees.
  3344. if (std::fmod(start, 90.f) == 0) {
  3345. check_oval_arc(reporter, start, sweep, path);
  3346. } else {
  3347. REPORTER_ASSERT(reporter, !path.isOval(nullptr));
  3348. }
  3349. }
  3350. // Test start angles that are nearly at valid oval start angles.
  3351. for (float start : {-180.f, -90.f, 90.f, 180.f}) {
  3352. for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
  3353. SkPath path;
  3354. path.addArc(oval, start + delta, sweep);
  3355. check_oval_arc(reporter, start, sweep, path);
  3356. }
  3357. }
  3358. }
  3359. }
  3360. static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
  3361. SkScalar x0, SkScalar y0) {
  3362. SkPoint pts[4];
  3363. SkPath::Verb v = iter->next(pts);
  3364. REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
  3365. REPORTER_ASSERT(reporter, pts[0].fX == x0);
  3366. REPORTER_ASSERT(reporter, pts[0].fY == y0);
  3367. }
  3368. static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
  3369. SkScalar x1, SkScalar y1) {
  3370. SkPoint pts[4];
  3371. SkPath::Verb v = iter->next(pts);
  3372. REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
  3373. REPORTER_ASSERT(reporter, pts[1].fX == x1);
  3374. REPORTER_ASSERT(reporter, pts[1].fY == y1);
  3375. }
  3376. static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
  3377. SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
  3378. SkPoint pts[4];
  3379. SkPath::Verb v = iter->next(pts);
  3380. REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
  3381. REPORTER_ASSERT(reporter, pts[1].fX == x1);
  3382. REPORTER_ASSERT(reporter, pts[1].fY == y1);
  3383. REPORTER_ASSERT(reporter, pts[2].fX == x2);
  3384. REPORTER_ASSERT(reporter, pts[2].fY == y2);
  3385. }
  3386. static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
  3387. SkPoint pts[4];
  3388. SkPath::Verb v = iter->next(pts);
  3389. REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
  3390. }
  3391. static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
  3392. check_done(reporter, p, iter);
  3393. p->reset();
  3394. }
  3395. static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
  3396. SkScalar x0, SkScalar y0) {
  3397. SkPath::RawIter iter(*p);
  3398. check_move(reporter, &iter, x0, y0);
  3399. check_done_and_reset(reporter, p, &iter);
  3400. }
  3401. static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
  3402. SkScalar x1, SkScalar y1) {
  3403. SkPath::RawIter iter(*p);
  3404. check_move(reporter, &iter, 0, 0);
  3405. check_line(reporter, &iter, x1, y1);
  3406. check_done_and_reset(reporter, p, &iter);
  3407. }
  3408. static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
  3409. SkScalar x1, SkScalar y1) {
  3410. SkPath::RawIter iter(*p);
  3411. check_move(reporter, &iter, 0, 0);
  3412. check_line(reporter, &iter, x1, y1);
  3413. check_done(reporter, p, &iter);
  3414. }
  3415. static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
  3416. SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
  3417. SkPath::RawIter iter(*p);
  3418. check_move(reporter, &iter, 0, 0);
  3419. check_line(reporter, &iter, x1, y1);
  3420. check_line(reporter, &iter, x2, y2);
  3421. check_done_and_reset(reporter, p, &iter);
  3422. }
  3423. static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
  3424. SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
  3425. SkPath::RawIter iter(*p);
  3426. check_move(reporter, &iter, 0, 0);
  3427. check_quad(reporter, &iter, x1, y1, x2, y2);
  3428. check_done_and_reset(reporter, p, &iter);
  3429. }
  3430. static bool nearly_equal(const SkRect& a, const SkRect& b) {
  3431. return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
  3432. SkScalarNearlyEqual(a.fTop, b.fTop) &&
  3433. SkScalarNearlyEqual(a.fRight, b.fRight) &&
  3434. SkScalarNearlyEqual(a.fBottom, b.fBottom);
  3435. }
  3436. static void test_arcTo(skiatest::Reporter* reporter) {
  3437. SkPath p;
  3438. p.arcTo(0, 0, 1, 2, 1);
  3439. check_path_is_line_and_reset(reporter, &p, 0, 0);
  3440. p.arcTo(1, 2, 1, 2, 1);
  3441. check_path_is_line_and_reset(reporter, &p, 1, 2);
  3442. p.arcTo(1, 2, 3, 4, 0);
  3443. check_path_is_line_and_reset(reporter, &p, 1, 2);
  3444. p.arcTo(1, 2, 0, 0, 1);
  3445. check_path_is_line_and_reset(reporter, &p, 1, 2);
  3446. p.arcTo(1, 0, 1, 1, 1);
  3447. SkPoint pt;
  3448. REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
  3449. p.reset();
  3450. p.arcTo(1, 0, 1, -1, 1);
  3451. REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
  3452. p.reset();
  3453. SkRect oval = {1, 2, 3, 4};
  3454. p.arcTo(oval, 0, 0, true);
  3455. check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
  3456. p.arcTo(oval, 0, 0, false);
  3457. check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
  3458. p.arcTo(oval, 360, 0, true);
  3459. check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
  3460. p.arcTo(oval, 360, 0, false);
  3461. check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
  3462. for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
  3463. p.arcTo(oval, 0, sweep, false);
  3464. REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
  3465. sweep += delta;
  3466. delta /= 2;
  3467. }
  3468. for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
  3469. p.arcTo(oval, 0, sweep, false);
  3470. REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
  3471. sweep -= delta;
  3472. delta /= 2;
  3473. }
  3474. SkRect noOvalWidth = {1, 2, 0, 3};
  3475. p.reset();
  3476. p.arcTo(noOvalWidth, 0, 360, false);
  3477. REPORTER_ASSERT(reporter, p.isEmpty());
  3478. SkRect noOvalHeight = {1, 2, 3, 1};
  3479. p.reset();
  3480. p.arcTo(noOvalHeight, 0, 360, false);
  3481. REPORTER_ASSERT(reporter, p.isEmpty());
  3482. }
  3483. static void test_addPath(skiatest::Reporter* reporter) {
  3484. SkPath p, q;
  3485. p.lineTo(1, 2);
  3486. q.moveTo(4, 4);
  3487. q.lineTo(7, 8);
  3488. q.conicTo(8, 7, 6, 5, 0.5f);
  3489. q.quadTo(6, 7, 8, 6);
  3490. q.cubicTo(5, 6, 7, 8, 7, 5);
  3491. q.close();
  3492. p.addPath(q, -4, -4);
  3493. SkRect expected = {0, 0, 4, 4};
  3494. REPORTER_ASSERT(reporter, p.getBounds() == expected);
  3495. p.reset();
  3496. p.reverseAddPath(q);
  3497. SkRect reverseExpected = {4, 4, 8, 8};
  3498. REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
  3499. }
  3500. static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
  3501. SkPath p, q;
  3502. if (explicitMoveTo) {
  3503. p.moveTo(1, 1);
  3504. }
  3505. p.lineTo(1, 2);
  3506. if (explicitMoveTo) {
  3507. q.moveTo(2, 1);
  3508. }
  3509. q.lineTo(2, 2);
  3510. p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
  3511. uint8_t verbs[4];
  3512. int verbcount = p.getVerbs(verbs, 4);
  3513. REPORTER_ASSERT(reporter, verbcount == 4);
  3514. REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
  3515. REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
  3516. REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
  3517. REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
  3518. }
  3519. static void test_extendClosedPath(skiatest::Reporter* reporter) {
  3520. SkPath p, q;
  3521. p.moveTo(1, 1);
  3522. p.lineTo(1, 2);
  3523. p.lineTo(2, 2);
  3524. p.close();
  3525. q.moveTo(2, 1);
  3526. q.lineTo(2, 3);
  3527. p.addPath(q, SkPath::kExtend_AddPathMode);
  3528. uint8_t verbs[7];
  3529. int verbcount = p.getVerbs(verbs, 7);
  3530. REPORTER_ASSERT(reporter, verbcount == 7);
  3531. REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
  3532. REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
  3533. REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
  3534. REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
  3535. REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
  3536. REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
  3537. REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
  3538. SkPoint pt;
  3539. REPORTER_ASSERT(reporter, p.getLastPt(&pt));
  3540. REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
  3541. REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
  3542. }
  3543. static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
  3544. SkPath p, q, r;
  3545. // case 1: dst is empty
  3546. p.moveTo(2, 1);
  3547. p.lineTo(2, 3);
  3548. q.addPath(p, mode);
  3549. REPORTER_ASSERT(reporter, q == p);
  3550. // case 2: src is empty
  3551. p.addPath(r, mode);
  3552. REPORTER_ASSERT(reporter, q == p);
  3553. // case 3: src and dst are empty
  3554. q.reset();
  3555. q.addPath(r, mode);
  3556. REPORTER_ASSERT(reporter, q.isEmpty());
  3557. }
  3558. static void test_conicTo_special_case(skiatest::Reporter* reporter) {
  3559. SkPath p;
  3560. p.conicTo(1, 2, 3, 4, -1);
  3561. check_path_is_line_and_reset(reporter, &p, 3, 4);
  3562. p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
  3563. check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
  3564. p.conicTo(1, 2, 3, 4, 1);
  3565. check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
  3566. }
  3567. static void test_get_point(skiatest::Reporter* reporter) {
  3568. SkPath p;
  3569. SkPoint pt = p.getPoint(0);
  3570. REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
  3571. REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
  3572. REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
  3573. p.setLastPt(10, 10);
  3574. pt = p.getPoint(0);
  3575. REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
  3576. REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
  3577. p.rMoveTo(10, 10);
  3578. REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
  3579. }
  3580. static void test_contains(skiatest::Reporter* reporter) {
  3581. SkPath p;
  3582. p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
  3583. p.conicTo(SkBits2Float(0xdfdaa221), SkBits2Float(0x5eaac338), SkBits2Float(0x60342f13), SkBits2Float(0xdf0cbb58), SkBits2Float(0x3f3504f3)); // -3.15084e+19f, 6.15237e+18f, 5.19345e+19f, -1.01408e+19f, 0.707107f
  3584. p.conicTo(SkBits2Float(0x60ead799), SkBits2Float(0xdfb76c24), SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8), SkBits2Float(0x3f3504f4)); // 1.35377e+20f, -2.6434e+19f, 8.96947e+19f, -1.75139e+19f, 0.707107f
  3585. p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
  3586. p.conicTo(SkBits2Float(0x6018b296), SkBits2Float(0xdeee870d), SkBits2Float(0xe008cd8e), SkBits2Float(0x5ed5b2db), SkBits2Float(0x3f3504f3)); // 4.40121e+19f, -8.59386e+18f, -3.94308e+19f, 7.69931e+18f, 0.707107f
  3587. p.conicTo(SkBits2Float(0xe0d526d9), SkBits2Float(0x5fa67b31), SkBits2Float(0xe085e7b2), SkBits2Float(0x5f512c01), SkBits2Float(0x3f3504f3)); // -1.22874e+20f, 2.39925e+19f, -7.7191e+19f, 1.50724e+19f, 0.707107f
  3588. // this may return true or false, depending on the platform's numerics, but it should not crash
  3589. (void) p.contains(-77.2027664f, 15.3066053f);
  3590. p.reset();
  3591. p.setFillType(SkPath::kInverseWinding_FillType);
  3592. REPORTER_ASSERT(reporter, p.contains(0, 0));
  3593. p.setFillType(SkPath::kWinding_FillType);
  3594. REPORTER_ASSERT(reporter, !p.contains(0, 0));
  3595. p.moveTo(4, 4);
  3596. p.lineTo(6, 8);
  3597. p.lineTo(8, 4);
  3598. // test on edge
  3599. REPORTER_ASSERT(reporter, p.contains(6, 4));
  3600. REPORTER_ASSERT(reporter, p.contains(5, 6));
  3601. REPORTER_ASSERT(reporter, p.contains(7, 6));
  3602. // test quick reject
  3603. REPORTER_ASSERT(reporter, !p.contains(4, 0));
  3604. REPORTER_ASSERT(reporter, !p.contains(0, 4));
  3605. REPORTER_ASSERT(reporter, !p.contains(4, 10));
  3606. REPORTER_ASSERT(reporter, !p.contains(10, 4));
  3607. // test various crossings in x
  3608. REPORTER_ASSERT(reporter, !p.contains(5, 7));
  3609. REPORTER_ASSERT(reporter, p.contains(6, 7));
  3610. REPORTER_ASSERT(reporter, !p.contains(7, 7));
  3611. p.reset();
  3612. p.moveTo(4, 4);
  3613. p.lineTo(8, 6);
  3614. p.lineTo(4, 8);
  3615. // test on edge
  3616. REPORTER_ASSERT(reporter, p.contains(4, 6));
  3617. REPORTER_ASSERT(reporter, p.contains(6, 5));
  3618. REPORTER_ASSERT(reporter, p.contains(6, 7));
  3619. // test various crossings in y
  3620. REPORTER_ASSERT(reporter, !p.contains(7, 5));
  3621. REPORTER_ASSERT(reporter, p.contains(7, 6));
  3622. REPORTER_ASSERT(reporter, !p.contains(7, 7));
  3623. p.reset();
  3624. p.moveTo(4, 4);
  3625. p.lineTo(8, 4);
  3626. p.lineTo(8, 8);
  3627. p.lineTo(4, 8);
  3628. // test on vertices
  3629. REPORTER_ASSERT(reporter, p.contains(4, 4));
  3630. REPORTER_ASSERT(reporter, p.contains(8, 4));
  3631. REPORTER_ASSERT(reporter, p.contains(8, 8));
  3632. REPORTER_ASSERT(reporter, p.contains(4, 8));
  3633. p.reset();
  3634. p.moveTo(4, 4);
  3635. p.lineTo(6, 8);
  3636. p.lineTo(2, 8);
  3637. // test on edge
  3638. REPORTER_ASSERT(reporter, p.contains(5, 6));
  3639. REPORTER_ASSERT(reporter, p.contains(4, 8));
  3640. REPORTER_ASSERT(reporter, p.contains(3, 6));
  3641. p.reset();
  3642. p.moveTo(4, 4);
  3643. p.lineTo(0, 6);
  3644. p.lineTo(4, 8);
  3645. // test on edge
  3646. REPORTER_ASSERT(reporter, p.contains(2, 5));
  3647. REPORTER_ASSERT(reporter, p.contains(2, 7));
  3648. REPORTER_ASSERT(reporter, p.contains(4, 6));
  3649. // test canceling coincident edge (a smaller triangle is coincident with a larger one)
  3650. p.reset();
  3651. p.moveTo(4, 0);
  3652. p.lineTo(6, 4);
  3653. p.lineTo(2, 4);
  3654. p.moveTo(4, 0);
  3655. p.lineTo(0, 8);
  3656. p.lineTo(8, 8);
  3657. REPORTER_ASSERT(reporter, !p.contains(1, 2));
  3658. REPORTER_ASSERT(reporter, !p.contains(3, 2));
  3659. REPORTER_ASSERT(reporter, !p.contains(4, 0));
  3660. REPORTER_ASSERT(reporter, p.contains(4, 4));
  3661. // test quads
  3662. p.reset();
  3663. p.moveTo(4, 4);
  3664. p.quadTo(6, 6, 8, 8);
  3665. p.quadTo(6, 8, 4, 8);
  3666. p.quadTo(4, 6, 4, 4);
  3667. REPORTER_ASSERT(reporter, p.contains(5, 6));
  3668. REPORTER_ASSERT(reporter, !p.contains(6, 5));
  3669. // test quad edge
  3670. REPORTER_ASSERT(reporter, p.contains(5, 5));
  3671. REPORTER_ASSERT(reporter, p.contains(5, 8));
  3672. REPORTER_ASSERT(reporter, p.contains(4, 5));
  3673. // test quad endpoints
  3674. REPORTER_ASSERT(reporter, p.contains(4, 4));
  3675. REPORTER_ASSERT(reporter, p.contains(8, 8));
  3676. REPORTER_ASSERT(reporter, p.contains(4, 8));
  3677. p.reset();
  3678. const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
  3679. p.moveTo(qPts[0]);
  3680. for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
  3681. p.quadTo(qPts[index], qPts[index + 1]);
  3682. }
  3683. REPORTER_ASSERT(reporter, p.contains(5, 6));
  3684. REPORTER_ASSERT(reporter, !p.contains(6, 5));
  3685. // test quad edge
  3686. SkPoint halfway;
  3687. for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
  3688. SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
  3689. REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
  3690. }
  3691. // test conics
  3692. p.reset();
  3693. const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
  3694. p.moveTo(kPts[0]);
  3695. for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
  3696. p.conicTo(kPts[index], kPts[index + 1], 0.5f);
  3697. }
  3698. REPORTER_ASSERT(reporter, p.contains(5, 6));
  3699. REPORTER_ASSERT(reporter, !p.contains(6, 5));
  3700. // test conic edge
  3701. for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
  3702. SkConic conic(&kPts[index], 0.5f);
  3703. halfway = conic.evalAt(0.5f);
  3704. REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
  3705. }
  3706. // test conic end points
  3707. REPORTER_ASSERT(reporter, p.contains(4, 4));
  3708. REPORTER_ASSERT(reporter, p.contains(8, 8));
  3709. REPORTER_ASSERT(reporter, p.contains(4, 8));
  3710. // test cubics
  3711. SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
  3712. for (int i = 0; i < 3; ++i) {
  3713. p.reset();
  3714. p.setFillType(SkPath::kEvenOdd_FillType);
  3715. p.moveTo(pts[i].fX, pts[i].fY);
  3716. p.cubicTo(pts[i + 1].fX, pts[i + 1].fY, pts[i + 2].fX, pts[i + 2].fY, pts[i + 3].fX, pts[i + 3].fY);
  3717. p.cubicTo(pts[i + 4].fX, pts[i + 4].fY, pts[i + 5].fX, pts[i + 5].fY, pts[i + 6].fX, pts[i + 6].fY);
  3718. p.close();
  3719. REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
  3720. REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
  3721. // test cubic edge
  3722. SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
  3723. REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
  3724. SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
  3725. REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
  3726. // test cubic end points
  3727. REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
  3728. REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
  3729. REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
  3730. }
  3731. }
  3732. class PathRefTest_Private {
  3733. public:
  3734. static size_t GetFreeSpace(const SkPathRef& ref) {
  3735. return ref.fFreeSpace;
  3736. }
  3737. static void TestPathRef(skiatest::Reporter* reporter) {
  3738. static const int kRepeatCnt = 10;
  3739. sk_sp<SkPathRef> pathRef(new SkPathRef);
  3740. SkPathRef::Editor ed(&pathRef);
  3741. {
  3742. ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
  3743. REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
  3744. REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
  3745. REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
  3746. for (int i = 0; i < kRepeatCnt; ++i) {
  3747. REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
  3748. }
  3749. ed.resetToSize(0, 0, 0);
  3750. }
  3751. {
  3752. ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
  3753. REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
  3754. REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
  3755. REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
  3756. for (int i = 0; i < kRepeatCnt; ++i) {
  3757. REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
  3758. }
  3759. ed.resetToSize(0, 0, 0);
  3760. }
  3761. {
  3762. ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
  3763. REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
  3764. REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
  3765. REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
  3766. for (int i = 0; i < kRepeatCnt; ++i) {
  3767. REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
  3768. }
  3769. ed.resetToSize(0, 0, 0);
  3770. }
  3771. {
  3772. SkScalar* weights = nullptr;
  3773. ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
  3774. REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
  3775. REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
  3776. REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
  3777. REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
  3778. REPORTER_ASSERT(reporter, weights);
  3779. for (int i = 0; i < kRepeatCnt; ++i) {
  3780. REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
  3781. }
  3782. ed.resetToSize(0, 0, 0);
  3783. }
  3784. {
  3785. ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
  3786. REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
  3787. REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
  3788. REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
  3789. for (int i = 0; i < kRepeatCnt; ++i) {
  3790. REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
  3791. }
  3792. ed.resetToSize(0, 0, 0);
  3793. }
  3794. }
  3795. };
  3796. static void test_operatorEqual(skiatest::Reporter* reporter) {
  3797. SkPath a;
  3798. SkPath b;
  3799. REPORTER_ASSERT(reporter, a == a);
  3800. REPORTER_ASSERT(reporter, a == b);
  3801. a.setFillType(SkPath::kInverseWinding_FillType);
  3802. REPORTER_ASSERT(reporter, a != b);
  3803. a.reset();
  3804. REPORTER_ASSERT(reporter, a == b);
  3805. a.lineTo(1, 1);
  3806. REPORTER_ASSERT(reporter, a != b);
  3807. a.reset();
  3808. REPORTER_ASSERT(reporter, a == b);
  3809. a.lineTo(1, 1);
  3810. b.lineTo(1, 2);
  3811. REPORTER_ASSERT(reporter, a != b);
  3812. a.reset();
  3813. a.lineTo(1, 2);
  3814. REPORTER_ASSERT(reporter, a == b);
  3815. }
  3816. static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
  3817. bool dumpAsHex, const char* str) {
  3818. SkDynamicMemoryWStream wStream;
  3819. path.dump(&wStream, force, dumpAsHex);
  3820. sk_sp<SkData> data = wStream.detachAsData();
  3821. REPORTER_ASSERT(reporter, data->size() == strlen(str));
  3822. if (strlen(str) > 0) {
  3823. REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
  3824. } else {
  3825. REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
  3826. }
  3827. }
  3828. static void test_dump(skiatest::Reporter* reporter) {
  3829. SkPath p;
  3830. compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n");
  3831. compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n");
  3832. p.moveTo(1, 2);
  3833. p.lineTo(3, 4);
  3834. compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
  3835. "path.moveTo(1, 2);\n"
  3836. "path.lineTo(3, 4);\n");
  3837. compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"
  3838. "path.moveTo(1, 2);\n"
  3839. "path.lineTo(3, 4);\n"
  3840. "path.lineTo(1, 2);\n"
  3841. "path.close();\n");
  3842. p.reset();
  3843. p.setFillType(SkPath::kEvenOdd_FillType);
  3844. p.moveTo(1, 2);
  3845. p.quadTo(3, 4, 5, 6);
  3846. compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n"
  3847. "path.moveTo(1, 2);\n"
  3848. "path.quadTo(3, 4, 5, 6);\n");
  3849. p.reset();
  3850. p.setFillType(SkPath::kInverseWinding_FillType);
  3851. p.moveTo(1, 2);
  3852. p.conicTo(3, 4, 5, 6, 0.5f);
  3853. compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n"
  3854. "path.moveTo(1, 2);\n"
  3855. "path.conicTo(3, 4, 5, 6, 0.5f);\n");
  3856. p.reset();
  3857. p.setFillType(SkPath::kInverseEvenOdd_FillType);
  3858. p.moveTo(1, 2);
  3859. p.cubicTo(3, 4, 5, 6, 7, 8);
  3860. compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n"
  3861. "path.moveTo(1, 2);\n"
  3862. "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
  3863. p.reset();
  3864. p.setFillType(SkPath::kWinding_FillType);
  3865. p.moveTo(1, 2);
  3866. p.lineTo(3, 4);
  3867. compare_dump(reporter, p, false, true,
  3868. "path.setFillType(SkPath::kWinding_FillType);\n"
  3869. "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
  3870. "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
  3871. p.reset();
  3872. p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
  3873. p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
  3874. compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
  3875. "path.moveTo(1, 2);\n"
  3876. "path.lineTo(3, 4);\n");
  3877. }
  3878. namespace {
  3879. class ChangeListener : public SkPathRef::GenIDChangeListener {
  3880. public:
  3881. ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
  3882. ~ChangeListener() override {}
  3883. void onChange() override {
  3884. *fChanged = true;
  3885. }
  3886. private:
  3887. bool* fChanged;
  3888. };
  3889. }
  3890. class PathTest_Private {
  3891. public:
  3892. static size_t GetFreeSpace(const SkPath& path) {
  3893. return PathRefTest_Private::GetFreeSpace(*path.fPathRef);
  3894. }
  3895. static void TestPathTo(skiatest::Reporter* reporter) {
  3896. SkPath p, q;
  3897. p.lineTo(4, 4);
  3898. p.reversePathTo(q);
  3899. check_path_is_line(reporter, &p, 4, 4);
  3900. q.moveTo(-4, -4);
  3901. p.reversePathTo(q);
  3902. check_path_is_line(reporter, &p, 4, 4);
  3903. q.lineTo(7, 8);
  3904. q.conicTo(8, 7, 6, 5, 0.5f);
  3905. q.quadTo(6, 7, 8, 6);
  3906. q.cubicTo(5, 6, 7, 8, 7, 5);
  3907. q.close();
  3908. p.reversePathTo(q);
  3909. SkRect reverseExpected = {-4, -4, 8, 8};
  3910. REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
  3911. }
  3912. static void TestPathrefListeners(skiatest::Reporter* reporter) {
  3913. SkPath p;
  3914. bool changed = false;
  3915. p.moveTo(0, 0);
  3916. // Check that listener is notified on moveTo().
  3917. SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
  3918. REPORTER_ASSERT(reporter, !changed);
  3919. p.moveTo(10, 0);
  3920. REPORTER_ASSERT(reporter, changed);
  3921. // Check that listener is notified on lineTo().
  3922. SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
  3923. REPORTER_ASSERT(reporter, !changed);
  3924. p.lineTo(20, 0);
  3925. REPORTER_ASSERT(reporter, changed);
  3926. // Check that listener is notified on reset().
  3927. SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
  3928. REPORTER_ASSERT(reporter, !changed);
  3929. p.reset();
  3930. REPORTER_ASSERT(reporter, changed);
  3931. p.moveTo(0, 0);
  3932. // Check that listener is notified on rewind().
  3933. SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
  3934. REPORTER_ASSERT(reporter, !changed);
  3935. p.rewind();
  3936. REPORTER_ASSERT(reporter, changed);
  3937. // Check that listener is notified on transform().
  3938. {
  3939. SkPath q;
  3940. q.moveTo(10, 10);
  3941. SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
  3942. REPORTER_ASSERT(reporter, !changed);
  3943. SkMatrix matrix;
  3944. matrix.setScale(2, 2);
  3945. p.transform(matrix, &q);
  3946. REPORTER_ASSERT(reporter, changed);
  3947. }
  3948. // Check that listener is notified when pathref is deleted.
  3949. {
  3950. SkPath q;
  3951. q.moveTo(10, 10);
  3952. SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
  3953. REPORTER_ASSERT(reporter, !changed);
  3954. }
  3955. // q went out of scope.
  3956. REPORTER_ASSERT(reporter, changed);
  3957. }
  3958. };
  3959. static void test_crbug_629455(skiatest::Reporter* reporter) {
  3960. SkPath path;
  3961. path.moveTo(0, 0);
  3962. path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
  3963. SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
  3964. SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
  3965. // AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
  3966. path.lineTo(0, 0);
  3967. test_draw_AA_path(100, 100, path);
  3968. }
  3969. static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
  3970. SkPath path;
  3971. path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
  3972. path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
  3973. path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
  3974. path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
  3975. path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
  3976. path.close();
  3977. auto surface = SkSurface::MakeRasterN32Premul(100, 100);
  3978. SkPaint paint;
  3979. paint.setAntiAlias(true);
  3980. surface->getCanvas()->clipPath(path, true);
  3981. surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
  3982. }
  3983. static void test_path_crbugskia6003() {
  3984. auto surface(SkSurface::MakeRasterN32Premul(500, 500));
  3985. SkCanvas* canvas = surface->getCanvas();
  3986. SkPaint paint;
  3987. paint.setAntiAlias(true);
  3988. SkPath path;
  3989. path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
  3990. path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
  3991. path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
  3992. path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
  3993. path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
  3994. // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
  3995. path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
  3996. SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
  3997. // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
  3998. path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
  3999. SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
  4000. path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
  4001. path.close();
  4002. canvas->clipPath(path, true);
  4003. canvas->drawRect(SkRect::MakeWH(500, 500), paint);
  4004. }
  4005. static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
  4006. SkPath path;
  4007. path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
  4008. path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
  4009. path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
  4010. path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
  4011. path.close();
  4012. test_draw_AA_path(100, 100, path);
  4013. }
  4014. static void test_skbug_6947() {
  4015. SkPath path;
  4016. SkPoint points[] =
  4017. {{125.126022f, -0.499872506f}, {125.288895f, -0.499338806f},
  4018. {125.299316f, -0.499290764f}, {126.294594f, 0.505449712f},
  4019. {125.999992f, 62.5047531f}, {124.0f, 62.4980202f},
  4020. {124.122749f, 0.498142242f}, {125.126022f, -0.499872506f},
  4021. {125.119476f, 1.50011659f}, {125.122749f, 0.50012207f},
  4022. {126.122749f, 0.502101898f}, {126.0f, 62.5019798f},
  4023. {125.0f, 62.5f}, {124.000008f, 62.4952469f},
  4024. {124.294609f, 0.495946467f}, {125.294601f, 0.50069809f},
  4025. {125.289886f, 1.50068688f}, {125.282349f, 1.50065041f},
  4026. {125.119476f, 1.50011659f}};
  4027. constexpr SkPath::Verb kMove = SkPath::kMove_Verb;
  4028. constexpr SkPath::Verb kLine = SkPath::kLine_Verb;
  4029. constexpr SkPath::Verb kClose = SkPath::kClose_Verb;
  4030. SkPath::Verb verbs[] = {kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose,
  4031. kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose};
  4032. int pointIndex = 0;
  4033. for(auto verb : verbs) {
  4034. switch (verb) {
  4035. case kMove:
  4036. path.moveTo(points[pointIndex++]);
  4037. break;
  4038. case kLine:
  4039. path.lineTo(points[pointIndex++]);
  4040. break;
  4041. case kClose:
  4042. default:
  4043. path.close();
  4044. break;
  4045. }
  4046. }
  4047. test_draw_AA_path(250, 125, path);
  4048. }
  4049. static void test_skbug_7015() {
  4050. SkPath path;
  4051. path.setFillType(SkPath::kWinding_FillType);
  4052. path.moveTo(SkBits2Float(0x4388c000), SkBits2Float(0x43947c08)); // 273.5f, 296.969f
  4053. path.lineTo(SkBits2Float(0x4386c000), SkBits2Float(0x43947c08)); // 269.5f, 296.969f
  4054. // 269.297f, 292.172f, 273.695f, 292.172f, 273.5f, 296.969f
  4055. path.cubicTo(SkBits2Float(0x4386a604), SkBits2Float(0x43921604),
  4056. SkBits2Float(0x4388d8f6), SkBits2Float(0x43921604),
  4057. SkBits2Float(0x4388c000), SkBits2Float(0x43947c08));
  4058. path.close();
  4059. test_draw_AA_path(500, 500, path);
  4060. }
  4061. static void test_skbug_7051() {
  4062. SkPath path;
  4063. path.moveTo(10, 10);
  4064. path.cubicTo(10, 20, 10, 30, 30, 30);
  4065. path.lineTo(50, 20);
  4066. path.lineTo(50, 10);
  4067. path.close();
  4068. test_draw_AA_path(100, 100, path);
  4069. }
  4070. static void test_interp(skiatest::Reporter* reporter) {
  4071. SkPath p1, p2, out;
  4072. REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
  4073. REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
  4074. REPORTER_ASSERT(reporter, p1 == out);
  4075. REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
  4076. REPORTER_ASSERT(reporter, p1 == out);
  4077. p1.moveTo(0, 2);
  4078. p1.lineTo(0, 4);
  4079. REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
  4080. REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
  4081. p2.moveTo(6, 0);
  4082. p2.lineTo(8, 0);
  4083. REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
  4084. REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
  4085. REPORTER_ASSERT(reporter, p2 == out);
  4086. REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
  4087. REPORTER_ASSERT(reporter, p1 == out);
  4088. REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
  4089. REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
  4090. p1.reset();
  4091. p1.moveTo(4, 4);
  4092. p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
  4093. p2.reset();
  4094. p2.moveTo(4, 2);
  4095. p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
  4096. REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
  4097. REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
  4098. REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
  4099. p2.reset();
  4100. p2.moveTo(4, 2);
  4101. p2.conicTo(6, 3, 6, 5, 1);
  4102. REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
  4103. p2.reset();
  4104. p2.moveTo(4, 4);
  4105. p2.conicTo(5, 4, 5, 5, 0.5f);
  4106. REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
  4107. }
  4108. DEF_TEST(PathInterp, reporter) {
  4109. test_interp(reporter);
  4110. }
  4111. #include "include/core/SkSurface.h"
  4112. DEF_TEST(PathBigCubic, reporter) {
  4113. SkPath path;
  4114. path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
  4115. path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
  4116. path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052), SkBits2Float(0x00000100), SkBits2Float(0x00000000), SkBits2Float(0x00000100), SkBits2Float(0x00000000)); // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
  4117. path.moveTo(0, 512);
  4118. // this call should not assert
  4119. SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint());
  4120. }
  4121. DEF_TEST(PathContains, reporter) {
  4122. test_contains(reporter);
  4123. }
  4124. DEF_TEST(Paths, reporter) {
  4125. test_fuzz_crbug_647922();
  4126. test_fuzz_crbug_643933();
  4127. test_sect_with_horizontal_needs_pinning();
  4128. test_crbug_629455(reporter);
  4129. test_fuzz_crbug_627414(reporter);
  4130. test_path_crbug364224();
  4131. test_fuzz_crbug_662952(reporter);
  4132. test_fuzz_crbug_662730(reporter);
  4133. test_fuzz_crbug_662780();
  4134. test_mask_overflow();
  4135. test_path_crbugskia6003();
  4136. test_fuzz_crbug_668907();
  4137. test_skbug_6947();
  4138. test_skbug_7015();
  4139. test_skbug_7051();
  4140. SkSize::Make(3, 4);
  4141. SkPath p, empty;
  4142. SkRect bounds, bounds2;
  4143. test_empty(reporter, p);
  4144. REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
  4145. // this triggers a code path in SkPath::operator= which is otherwise unexercised
  4146. SkPath& self = p;
  4147. p = self;
  4148. // this triggers a code path in SkPath::swap which is otherwise unexercised
  4149. p.swap(self);
  4150. bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
  4151. p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
  4152. check_convex_bounds(reporter, p, bounds);
  4153. // we have quads or cubics
  4154. REPORTER_ASSERT(reporter,
  4155. p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
  4156. REPORTER_ASSERT(reporter, !p.isEmpty());
  4157. p.reset();
  4158. test_empty(reporter, p);
  4159. p.addOval(bounds);
  4160. check_convex_bounds(reporter, p, bounds);
  4161. REPORTER_ASSERT(reporter, !p.isEmpty());
  4162. p.rewind();
  4163. test_empty(reporter, p);
  4164. p.addRect(bounds);
  4165. check_convex_bounds(reporter, p, bounds);
  4166. // we have only lines
  4167. REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
  4168. REPORTER_ASSERT(reporter, !p.isEmpty());
  4169. REPORTER_ASSERT(reporter, p != empty);
  4170. REPORTER_ASSERT(reporter, !(p == empty));
  4171. // do getPoints and getVerbs return the right result
  4172. REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
  4173. REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
  4174. SkPoint pts[4];
  4175. int count = p.getPoints(pts, 4);
  4176. REPORTER_ASSERT(reporter, count == 4);
  4177. uint8_t verbs[6];
  4178. verbs[5] = 0xff;
  4179. p.getVerbs(verbs, 5);
  4180. REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
  4181. REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
  4182. REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
  4183. REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
  4184. REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
  4185. REPORTER_ASSERT(reporter, 0xff == verbs[5]);
  4186. bounds2.set(pts, 4);
  4187. REPORTER_ASSERT(reporter, bounds == bounds2);
  4188. bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
  4189. p.offset(SK_Scalar1*3, SK_Scalar1*4);
  4190. REPORTER_ASSERT(reporter, bounds == p.getBounds());
  4191. REPORTER_ASSERT(reporter, p.isRect(nullptr));
  4192. bounds2.setEmpty();
  4193. REPORTER_ASSERT(reporter, p.isRect(&bounds2));
  4194. REPORTER_ASSERT(reporter, bounds == bounds2);
  4195. // now force p to not be a rect
  4196. bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
  4197. p.addRect(bounds);
  4198. REPORTER_ASSERT(reporter, !p.isRect(nullptr));
  4199. // Test an edge case w.r.t. the bound returned by isRect (i.e., the
  4200. // path has a trailing moveTo. Please see crbug.com\445368)
  4201. {
  4202. SkRect r;
  4203. p.reset();
  4204. p.addRect(bounds);
  4205. REPORTER_ASSERT(reporter, p.isRect(&r));
  4206. REPORTER_ASSERT(reporter, r == bounds);
  4207. // add a moveTo outside of our bounds
  4208. p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
  4209. REPORTER_ASSERT(reporter, p.isRect(&r));
  4210. REPORTER_ASSERT(reporter, r == bounds);
  4211. }
  4212. test_operatorEqual(reporter);
  4213. test_isLine(reporter);
  4214. test_isRect(reporter);
  4215. test_is_simple_closed_rect(reporter);
  4216. test_isNestedFillRects(reporter);
  4217. test_zero_length_paths(reporter);
  4218. test_direction(reporter);
  4219. test_convexity(reporter);
  4220. test_convexity2(reporter);
  4221. test_convexity_doubleback(reporter);
  4222. test_conservativelyContains(reporter);
  4223. test_close(reporter);
  4224. test_segment_masks(reporter);
  4225. test_flattening(reporter);
  4226. test_transform(reporter);
  4227. test_bounds(reporter);
  4228. test_iter(reporter);
  4229. test_raw_iter(reporter);
  4230. test_circle(reporter);
  4231. test_oval(reporter);
  4232. test_strokerec(reporter);
  4233. test_addPoly(reporter);
  4234. test_isfinite(reporter);
  4235. test_isfinite_after_transform(reporter);
  4236. test_islastcontourclosed(reporter);
  4237. test_arb_round_rect_is_convex(reporter);
  4238. test_arb_zero_rad_round_rect_is_rect(reporter);
  4239. test_addrect(reporter);
  4240. test_addrect_isfinite(reporter);
  4241. test_tricky_cubic();
  4242. test_clipped_cubic();
  4243. test_crbug_170666();
  4244. test_crbug_493450(reporter);
  4245. test_crbug_495894(reporter);
  4246. test_crbug_613918();
  4247. test_bad_cubic_crbug229478();
  4248. test_bad_cubic_crbug234190();
  4249. test_gen_id(reporter);
  4250. test_path_close_issue1474(reporter);
  4251. test_path_to_region(reporter);
  4252. test_rrect(reporter);
  4253. test_arc(reporter);
  4254. test_arc_ovals(reporter);
  4255. test_arcTo(reporter);
  4256. test_addPath(reporter);
  4257. test_addPathMode(reporter, false, false);
  4258. test_addPathMode(reporter, true, false);
  4259. test_addPathMode(reporter, false, true);
  4260. test_addPathMode(reporter, true, true);
  4261. test_extendClosedPath(reporter);
  4262. test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
  4263. test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
  4264. test_conicTo_special_case(reporter);
  4265. test_get_point(reporter);
  4266. test_contains(reporter);
  4267. PathTest_Private::TestPathTo(reporter);
  4268. PathRefTest_Private::TestPathRef(reporter);
  4269. PathTest_Private::TestPathrefListeners(reporter);
  4270. test_dump(reporter);
  4271. test_path_crbug389050(reporter);
  4272. test_path_crbugskia2820(reporter);
  4273. test_path_crbugskia5995();
  4274. test_skbug_3469(reporter);
  4275. test_skbug_3239(reporter);
  4276. test_bounds_crbug_513799(reporter);
  4277. test_fuzz_crbug_638223();
  4278. }
  4279. DEF_TEST(conservatively_contains_rect, reporter) {
  4280. SkPath path;
  4281. path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
  4282. // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
  4283. path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
  4284. SkBits2Float(0x00000100), SkBits2Float(0x00000000),
  4285. SkBits2Float(0x00000100), SkBits2Float(0x00000000));
  4286. path.moveTo(0, 0);
  4287. // this guy should not assert
  4288. path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
  4289. }
  4290. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4291. static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
  4292. for (int i = 0; i < n; ++i) {
  4293. switch (verb) {
  4294. case SkPath::kLine_Verb:
  4295. path->lineTo(rand.nextF()*100, rand.nextF()*100);
  4296. break;
  4297. case SkPath::kQuad_Verb:
  4298. path->quadTo(rand.nextF()*100, rand.nextF()*100,
  4299. rand.nextF()*100, rand.nextF()*100);
  4300. break;
  4301. case SkPath::kConic_Verb:
  4302. path->conicTo(rand.nextF()*100, rand.nextF()*100,
  4303. rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
  4304. break;
  4305. case SkPath::kCubic_Verb:
  4306. path->cubicTo(rand.nextF()*100, rand.nextF()*100,
  4307. rand.nextF()*100, rand.nextF()*100,
  4308. rand.nextF()*100, rand.nextF()*100);
  4309. break;
  4310. default:
  4311. SkASSERT(false);
  4312. }
  4313. }
  4314. }
  4315. #include "include/pathops/SkPathOps.h"
  4316. DEF_TEST(path_tight_bounds, reporter) {
  4317. SkRandom rand;
  4318. const SkPath::Verb verbs[] = {
  4319. SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
  4320. };
  4321. for (int i = 0; i < 1000; ++i) {
  4322. for (int n = 1; n <= 10; n += 9) {
  4323. for (SkPath::Verb verb : verbs) {
  4324. SkPath path;
  4325. rand_path(&path, rand, verb, n);
  4326. SkRect bounds = path.getBounds();
  4327. SkRect tight = path.computeTightBounds();
  4328. REPORTER_ASSERT(reporter, bounds.contains(tight));
  4329. SkRect tight2;
  4330. TightBounds(path, &tight2);
  4331. REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
  4332. }
  4333. }
  4334. }
  4335. }
  4336. DEF_TEST(skbug_6450, r) {
  4337. SkRect ri = { 0.18554693f, 195.26283f, 0.185784385f, 752.644409f };
  4338. SkVector rdi[4] = {
  4339. { 1.81159976e-09f, 7.58768801e-05f },
  4340. { 0.000118725002f, 0.000118725002f },
  4341. { 0.000118725002f, 0.000118725002f },
  4342. { 0.000118725002f, 0.486297607f }
  4343. };
  4344. SkRRect irr;
  4345. irr.setRectRadii(ri, rdi);
  4346. SkRect ro = { 9.18354821e-39f, 2.1710848e+9f, 2.16945843e+9f, 3.47808128e+9f };
  4347. SkVector rdo[4] = {
  4348. { 0, 0 },
  4349. { 0.0103298295f, 0.185887396f },
  4350. { 2.52999727e-29f, 169.001938f },
  4351. { 195.262741f, 195.161255f }
  4352. };
  4353. SkRRect orr;
  4354. orr.setRectRadii(ro, rdo);
  4355. SkMakeNullCanvas()->drawDRRect(orr, irr, SkPaint());
  4356. }
  4357. DEF_TEST(PathRefSerialization, reporter) {
  4358. SkPath path;
  4359. const size_t numMoves = 5;
  4360. const size_t numConics = 7;
  4361. const size_t numPoints = numMoves + 2 * numConics;
  4362. const size_t numVerbs = numMoves + numConics;
  4363. for (size_t i = 0; i < numMoves; ++i) path.moveTo(1, 2);
  4364. for (size_t i = 0; i < numConics; ++i) path.conicTo(1, 2, 3, 4, 5);
  4365. REPORTER_ASSERT(reporter, path.countPoints() == numPoints);
  4366. REPORTER_ASSERT(reporter, path.countVerbs() == numVerbs);
  4367. // Verify that path serializes/deserializes properly.
  4368. sk_sp<SkData> data = path.serialize();
  4369. size_t bytesWritten = data->size();
  4370. {
  4371. SkPath readBack;
  4372. REPORTER_ASSERT(reporter, readBack != path);
  4373. size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten);
  4374. REPORTER_ASSERT(reporter, bytesRead == bytesWritten);
  4375. REPORTER_ASSERT(reporter, readBack == path);
  4376. }
  4377. // One less byte (rounded down to alignment) than was written will also
  4378. // fail to be deserialized.
  4379. {
  4380. SkPath readBack;
  4381. size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten - 4);
  4382. REPORTER_ASSERT(reporter, !bytesRead);
  4383. }
  4384. }
  4385. DEF_TEST(NonFinitePathIteration, reporter) {
  4386. SkPath path;
  4387. path.moveTo(SK_ScalarInfinity, SK_ScalarInfinity);
  4388. int verbs = 0;
  4389. SkPath::RawIter iter(path);
  4390. SkPoint pts[4];
  4391. while (iter.next(pts) != SkPath::kDone_Verb) {
  4392. verbs++;
  4393. }
  4394. REPORTER_ASSERT(reporter, verbs == 0);
  4395. }
  4396. DEF_TEST(AndroidArc, reporter) {
  4397. const char* tests[] = {
  4398. "M50,0A50,50,0,0 1 100,50 L100,85 A15,15,0,0 1 85,100 L50,100 A50,50,0,0 1 50,0z",
  4399. "M50,0L92,0 A8,8,0,0 1 100,8 L100,92 A8,8,0,0 1 92,100 L8,100"
  4400. " A8,8,0,0 1 0,92 L 0,8 A8,8,0,0 1 8,0z",
  4401. "M50 0A50 50,0,1,1,50 100A50 50,0,1,1,50 0"
  4402. };
  4403. for (auto test : tests) {
  4404. SkPath aPath;
  4405. SkAssertResult(SkParsePath::FromSVGString(test, &aPath));
  4406. SkASSERT(aPath.isConvex());
  4407. for (SkScalar scale = 1; scale < 1000; scale *= 1.1f) {
  4408. SkPath scalePath = aPath;
  4409. SkMatrix matrix;
  4410. matrix.setScale(scale, scale);
  4411. scalePath.transform(matrix);
  4412. SkASSERT(scalePath.isConvex());
  4413. }
  4414. for (SkScalar scale = 1; scale < .001; scale /= 1.1f) {
  4415. SkPath scalePath = aPath;
  4416. SkMatrix matrix;
  4417. matrix.setScale(scale, scale);
  4418. scalePath.transform(matrix);
  4419. SkASSERT(scalePath.isConvex());
  4420. }
  4421. }
  4422. }
  4423. /*
  4424. * Try a range of crazy values, just to ensure that we don't assert/crash.
  4425. */
  4426. DEF_TEST(HugeGeometry, reporter) {
  4427. auto surf = SkSurface::MakeRasterN32Premul(100, 100);
  4428. auto canvas = surf->getCanvas();
  4429. const bool aas[] = { false, true };
  4430. const SkPaint::Style styles[] = {
  4431. SkPaint::kFill_Style, SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
  4432. };
  4433. const SkScalar values[] = {
  4434. 0, 1, 1000, 1000 * 1000, 1000.f * 1000 * 10000, SK_ScalarMax / 2, SK_ScalarMax,
  4435. SK_ScalarInfinity
  4436. };
  4437. SkPaint paint;
  4438. for (auto x : values) {
  4439. SkRect r = { -x, -x, x, x };
  4440. for (auto width : values) {
  4441. paint.setStrokeWidth(width);
  4442. for (auto aa : aas) {
  4443. paint.setAntiAlias(aa);
  4444. for (auto style : styles) {
  4445. paint.setStyle(style);
  4446. canvas->drawRect(r, paint);
  4447. canvas->drawOval(r, paint);
  4448. }
  4449. }
  4450. }
  4451. }
  4452. }
  4453. // Treat nonfinite paths as "empty" or "full", depending on inverse-filltype
  4454. DEF_TEST(ClipPath_nonfinite, reporter) {
  4455. auto surf = SkSurface::MakeRasterN32Premul(10, 10);
  4456. SkCanvas* canvas = surf->getCanvas();
  4457. REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
  4458. for (bool aa : {false, true}) {
  4459. for (SkPath::FillType ft : {SkPath::kWinding_FillType, SkPath::kInverseWinding_FillType}) {
  4460. for (SkScalar bad : {SK_ScalarInfinity, SK_ScalarNaN}) {
  4461. for (int bits = 1; bits <= 15; ++bits) {
  4462. SkPoint p0 = { 0, 0 };
  4463. SkPoint p1 = { 0, 0 };
  4464. if (bits & 1) p0.fX = -bad;
  4465. if (bits & 2) p0.fY = -bad;
  4466. if (bits & 4) p1.fX = bad;
  4467. if (bits & 8) p1.fY = bad;
  4468. SkPath path;
  4469. path.moveTo(p0);
  4470. path.lineTo(p1);
  4471. path.setFillType(ft);
  4472. canvas->save();
  4473. canvas->clipPath(path, aa);
  4474. REPORTER_ASSERT(reporter, canvas->isClipEmpty() == !path.isInverseFillType());
  4475. canvas->restore();
  4476. }
  4477. }
  4478. }
  4479. }
  4480. REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
  4481. }
  4482. // skbug.com/7792
  4483. DEF_TEST(Path_isRect, reporter) {
  4484. auto makePath = [](const SkPoint* points, size_t count, bool close) -> SkPath {
  4485. SkPath path;
  4486. for (size_t index = 0; index < count; ++index) {
  4487. index < 2 ? path.moveTo(points[index]) : path.lineTo(points[index]);
  4488. }
  4489. if (close) {
  4490. path.close();
  4491. }
  4492. return path;
  4493. };
  4494. auto makePath2 = [](const SkPoint* points, const SkPath::Verb* verbs, size_t count) -> SkPath {
  4495. SkPath path;
  4496. for (size_t index = 0; index < count; ++index) {
  4497. switch (verbs[index]) {
  4498. case SkPath::kMove_Verb:
  4499. path.moveTo(*points++);
  4500. break;
  4501. case SkPath::kLine_Verb:
  4502. path.lineTo(*points++);
  4503. break;
  4504. case SkPath::kClose_Verb:
  4505. path.close();
  4506. break;
  4507. default:
  4508. SkASSERT(0);
  4509. }
  4510. }
  4511. return path;
  4512. };
  4513. // isolated from skbug.com/7792 (bug description)
  4514. SkRect rect;
  4515. SkPoint points[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
  4516. SkPath path = makePath(points, SK_ARRAY_COUNT(points), false);
  4517. REPORTER_ASSERT(reporter, path.isRect(&rect));
  4518. SkRect compare;
  4519. compare.set(&points[1], SK_ARRAY_COUNT(points) - 1);
  4520. REPORTER_ASSERT(reporter, rect == compare);
  4521. // isolated from skbug.com/7792#c3
  4522. SkPoint points3[] = { {75, 50}, {100, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 50} };
  4523. path = makePath(points3, SK_ARRAY_COUNT(points3), true);
  4524. REPORTER_ASSERT(reporter, !path.isRect(&rect));
  4525. // isolated from skbug.com/7792#c9
  4526. SkPoint points9[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
  4527. path = makePath(points9, SK_ARRAY_COUNT(points9), true);
  4528. REPORTER_ASSERT(reporter, path.isRect(&rect));
  4529. compare.set(&points9[1], SK_ARRAY_COUNT(points9) - 1);
  4530. REPORTER_ASSERT(reporter, rect == compare);
  4531. // isolated from skbug.com/7792#c11
  4532. SkPath::Verb verbs11[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4533. SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb };
  4534. SkPoint points11[] = { {75, 150}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 150} };
  4535. path = makePath2(points11, verbs11, SK_ARRAY_COUNT(verbs11));
  4536. REPORTER_ASSERT(reporter, path.isRect(&rect));
  4537. compare.set(&points11[0], SK_ARRAY_COUNT(points11));
  4538. REPORTER_ASSERT(reporter, rect == compare);
  4539. // isolated from skbug.com/7792#c14
  4540. SkPath::Verb verbs14[] = { SkPath::kMove_Verb, SkPath::kMove_Verb, SkPath::kMove_Verb,
  4541. SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4542. SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb,
  4543. SkPath::kLine_Verb, SkPath::kClose_Verb };
  4544. SkPoint points14[] = { {250, 75}, {250, 75}, {250, 75}, {100, 75},
  4545. {150, 75}, {150, 150}, {75, 150}, {75, 75}, {0, 0} };
  4546. path = makePath2(points14, verbs14, SK_ARRAY_COUNT(verbs14));
  4547. REPORTER_ASSERT(reporter, !path.isRect(&rect));
  4548. // isolated from skbug.com/7792#c15
  4549. SkPath::Verb verbs15[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4550. SkPath::kLine_Verb, SkPath::kMove_Verb };
  4551. SkPoint points15[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {250, 75} };
  4552. path = makePath2(points15, verbs15, SK_ARRAY_COUNT(verbs15));
  4553. REPORTER_ASSERT(reporter, path.isRect(&rect));
  4554. compare.set(&points15[0], SK_ARRAY_COUNT(points15) - 1);
  4555. REPORTER_ASSERT(reporter, rect == compare);
  4556. // isolated from skbug.com/7792#c17
  4557. SkPoint points17[] = { {75, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10} };
  4558. path = makePath(points17, SK_ARRAY_COUNT(points17), true);
  4559. REPORTER_ASSERT(reporter, !path.isRect(&rect));
  4560. // isolated from skbug.com/7792#c19
  4561. SkPath::Verb verbs19[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4562. SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4563. SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb,
  4564. SkPath::kLine_Verb, SkPath::kLine_Verb };
  4565. SkPoint points19[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
  4566. {75, 150}, {10, 10}, {30, 10}, {10, 30} };
  4567. path = makePath2(points19, verbs19, SK_ARRAY_COUNT(verbs19));
  4568. REPORTER_ASSERT(reporter, !path.isRect(&rect));
  4569. // isolated from skbug.com/7792#c23
  4570. SkPath::Verb verbs23[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
  4571. SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4572. SkPath::kLine_Verb, SkPath::kClose_Verb };
  4573. SkPoint points23[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
  4574. {75, 150} };
  4575. path = makePath2(points23, verbs23, SK_ARRAY_COUNT(verbs23));
  4576. REPORTER_ASSERT(reporter, path.isRect(&rect));
  4577. compare.set(&points23[0], SK_ARRAY_COUNT(points23));
  4578. REPORTER_ASSERT(reporter, rect == compare);
  4579. // isolated from skbug.com/7792#c29
  4580. SkPath::Verb verbs29[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4581. SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
  4582. SkPath::kClose_Verb };
  4583. SkPoint points29[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 250}, {75, 75} };
  4584. path = makePath2(points29, verbs29, SK_ARRAY_COUNT(verbs29));
  4585. REPORTER_ASSERT(reporter, !path.isRect(&rect));
  4586. // isolated from skbug.com/7792#c31
  4587. SkPath::Verb verbs31[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4588. SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
  4589. SkPath::kClose_Verb };
  4590. SkPoint points31[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10}, {75, 75} };
  4591. path = makePath2(points31, verbs31, SK_ARRAY_COUNT(verbs31));
  4592. REPORTER_ASSERT(reporter, path.isRect(&rect));
  4593. compare.set(&points31[0], 4);
  4594. REPORTER_ASSERT(reporter, rect == compare);
  4595. // isolated from skbug.com/7792#c36
  4596. SkPath::Verb verbs36[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4597. SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
  4598. SkPoint points36[] = { {75, 75}, {150, 75}, {150, 150}, {10, 150}, {75, 75}, {75, 75} };
  4599. path = makePath2(points36, verbs36, SK_ARRAY_COUNT(verbs36));
  4600. REPORTER_ASSERT(reporter, !path.isRect(&rect));
  4601. // isolated from skbug.com/7792#c39
  4602. SkPath::Verb verbs39[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4603. SkPath::kLine_Verb };
  4604. SkPoint points39[] = { {150, 75}, {150, 150}, {75, 150}, {75, 100} };
  4605. path = makePath2(points39, verbs39, SK_ARRAY_COUNT(verbs39));
  4606. REPORTER_ASSERT(reporter, !path.isRect(&rect));
  4607. // isolated from zero_length_paths_aa
  4608. SkPath::Verb verbsAA[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4609. SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4610. SkPath::kLine_Verb, SkPath::kClose_Verb };
  4611. SkPoint pointsAA[] = { {32, 9.5f}, {32, 9.5f}, {32, 17}, {17, 17}, {17, 9.5f}, {17, 2},
  4612. {32, 2} };
  4613. path = makePath2(pointsAA, verbsAA, SK_ARRAY_COUNT(verbsAA));
  4614. REPORTER_ASSERT(reporter, path.isRect(&rect));
  4615. compare.set(&pointsAA[0], SK_ARRAY_COUNT(pointsAA));
  4616. REPORTER_ASSERT(reporter, rect == compare);
  4617. // isolated from skbug.com/7792#c41
  4618. SkPath::Verb verbs41[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4619. SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
  4620. SkPath::kClose_Verb };
  4621. SkPoint points41[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
  4622. path = makePath2(points41, verbs41, SK_ARRAY_COUNT(verbs41));
  4623. REPORTER_ASSERT(reporter, path.isRect(&rect));
  4624. compare.set(&points41[1], 4);
  4625. REPORTER_ASSERT(reporter, rect == compare);
  4626. // isolated from skbug.com/7792#c53
  4627. SkPath::Verb verbs53[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
  4628. SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
  4629. SkPath::kClose_Verb };
  4630. SkPoint points53[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
  4631. path = makePath2(points53, verbs53, SK_ARRAY_COUNT(verbs53));
  4632. REPORTER_ASSERT(reporter, path.isRect(&rect));
  4633. compare.set(&points53[1], 4);
  4634. REPORTER_ASSERT(reporter, rect == compare);
  4635. }
  4636. // Be sure we can safely add ourselves
  4637. DEF_TEST(Path_self_add, reporter) {
  4638. // The possible problem is that during path.add() we may have to grow the dst buffers as
  4639. // we append the src pts/verbs, but all the while we are iterating over the src. If src == dst
  4640. // we could realloc the buffer's (on behalf of dst) leaving the src iterator pointing at
  4641. // garbage.
  4642. //
  4643. // The test runs though verious sized src paths, since its not defined publicly what the
  4644. // reserve allocation strategy is for SkPath, therefore we can't know when an append operation
  4645. // will trigger a realloc. At the time of this writing, these loops were sufficient to trigger
  4646. // an ASAN error w/o the fix to SkPath::addPath().
  4647. //
  4648. for (int count = 0; count < 10; ++count) {
  4649. SkPath path;
  4650. for (int add = 0; add < count; ++add) {
  4651. // just add some stuff, so we have something to copy/append in addPath()
  4652. path.moveTo(1, 2).lineTo(3, 4).cubicTo(1,2,3,4,5,6).conicTo(1,2,3,4,5);
  4653. }
  4654. path.addPath(path, 1, 2);
  4655. path.addPath(path, 3, 4);
  4656. }
  4657. }
  4658. #include "include/core/SkVertices.h"
  4659. static void draw_triangle(SkCanvas* canvas, const SkPoint pts[]) {
  4660. // draw in different ways, looking for an assert
  4661. {
  4662. SkPath path;
  4663. path.addPoly(pts, 3, false);
  4664. canvas->drawPath(path, SkPaint());
  4665. }
  4666. const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK };
  4667. auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
  4668. canvas->drawVertices(v, SkBlendMode::kSrcOver, SkPaint());
  4669. }
  4670. DEF_TEST(triangle_onehalf, reporter) {
  4671. auto surface(SkSurface::MakeRasterN32Premul(100, 100));
  4672. const SkPoint pts[] = {
  4673. { 0.499069244f, 9.63295173f },
  4674. { 0.499402374f, 7.88207579f },
  4675. { 10.2363272f, 0.49999997f }
  4676. };
  4677. draw_triangle(surface->getCanvas(), pts);
  4678. }
  4679. DEF_TEST(triangle_big, reporter) {
  4680. auto surface(SkSurface::MakeRasterN32Premul(4, 4304));
  4681. // The first two points, when sent through our fixed-point SkEdge, can walk negative beyond
  4682. // -0.5 due to accumulated += error of the slope. We have since make the bounds calculation
  4683. // be conservative, so we invoke clipping if we get in this situation.
  4684. // This test was added to demonstrate the need for this conservative bounds calc.
  4685. // (found by a fuzzer)
  4686. const SkPoint pts[] = {
  4687. { 0.327190518f, -114.945152f },
  4688. { -0.5f, 1.00003874f },
  4689. { 0.666425824f, 4304.26172f },
  4690. };
  4691. draw_triangle(surface->getCanvas(), pts);
  4692. }
  4693. static void add_verbs(SkPath* path, int count) {
  4694. path->moveTo(0, 0);
  4695. for (int i = 0; i < count; ++i) {
  4696. switch (i & 3) {
  4697. case 0: path->lineTo(10, 20); break;
  4698. case 1: path->quadTo(5, 6, 7, 8); break;
  4699. case 2: path->conicTo(1, 2, 3, 4, 0.5f); break;
  4700. case 3: path->cubicTo(2, 4, 6, 8, 10, 12); break;
  4701. }
  4702. }
  4703. }
  4704. // Make sure when we call shrinkToFit() that we always shrink (or stay the same)
  4705. // and that if we call twice, we stay the same.
  4706. DEF_TEST(Path_shrinkToFit, reporter) {
  4707. size_t max_free = 0;
  4708. for (int verbs = 0; verbs < 100; ++verbs) {
  4709. SkPath unique_path, shared_path;
  4710. add_verbs(&unique_path, verbs);
  4711. add_verbs(&shared_path, verbs);
  4712. const SkPath copy = shared_path;
  4713. REPORTER_ASSERT(reporter, shared_path == unique_path);
  4714. REPORTER_ASSERT(reporter, shared_path == copy);
  4715. #ifdef SK_DEBUG
  4716. size_t before = PathTest_Private::GetFreeSpace(unique_path);
  4717. #endif
  4718. unique_path.shrinkToFit();
  4719. shared_path.shrinkToFit();
  4720. REPORTER_ASSERT(reporter, shared_path == unique_path);
  4721. REPORTER_ASSERT(reporter, shared_path == copy);
  4722. #ifdef SK_DEBUG
  4723. size_t after = PathTest_Private::GetFreeSpace(unique_path);
  4724. REPORTER_ASSERT(reporter, before >= after);
  4725. max_free = std::max(max_free, before - after);
  4726. size_t after2 = PathTest_Private::GetFreeSpace(unique_path);
  4727. REPORTER_ASSERT(reporter, after == after2);
  4728. #endif
  4729. }
  4730. if (false) {
  4731. SkDebugf("max_free %zu\n", max_free);
  4732. }
  4733. }
  4734. DEF_TEST(Path_setLastPt, r) {
  4735. // There was a time where SkPath::setLastPoint() didn't invalidate cached path bounds.
  4736. SkPath p;
  4737. p.moveTo(0,0);
  4738. p.moveTo(20,01);
  4739. p.moveTo(20,10);
  4740. p.moveTo(20,61);
  4741. REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 20,61));
  4742. p.setLastPt(30,01);
  4743. REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 30,10)); // was {0,0, 20,61}
  4744. REPORTER_ASSERT(r, p.isValid());
  4745. }
  4746. DEF_TEST(Path_increserve_handle_neg_crbug_883666, r) {
  4747. SkPath path;
  4748. path.conicTo({0, 0}, {1, 1}, SK_FloatNegativeInfinity);
  4749. // <== use a copy path object to force SkPathRef::copy() and SkPathRef::resetToSize()
  4750. SkPath shallowPath = path;
  4751. // make sure we don't assert/crash on this.
  4752. shallowPath.incReserve(0xffffffff);
  4753. }
  4754. ////////////////////////////////////////////////////////////////////////////////////////////////
  4755. /*
  4756. * For speed, we tried to preserve useful/expensive attributes about paths,
  4757. * - convexity, isrect, isoval, ...
  4758. * Axis-aligned shapes (rect, oval, rrect) should survive, including convexity if the matrix
  4759. * is axis-aligned (e.g. scale+translate)
  4760. */
  4761. struct Xforms {
  4762. SkMatrix fIM, fTM, fSM, fRM;
  4763. Xforms() {
  4764. fIM.reset();
  4765. fTM.setTranslate(10, 20);
  4766. fSM.setScale(2, 3);
  4767. fRM.setRotate(30);
  4768. }
  4769. };
  4770. static bool conditional_convex(const SkPath& path, bool is_convex) {
  4771. SkPath::Convexity c = path.getConvexityOrUnknown();
  4772. return is_convex ? (c == SkPath::kConvex_Convexity) : (c != SkPath::kConvex_Convexity);
  4773. }
  4774. // expect axis-aligned shape to survive assignment, identity and scale/translate matrices
  4775. template <typename ISA>
  4776. void survive(SkPath* path, const Xforms& x, bool isAxisAligned, skiatest::Reporter* reporter,
  4777. ISA isa_proc) {
  4778. REPORTER_ASSERT(reporter, isa_proc(*path));
  4779. // force the issue (computing convexity) the first time.
  4780. REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kConvex_Convexity);
  4781. SkPath path2;
  4782. // a path's isa and convexity should survive assignment
  4783. path2 = *path;
  4784. REPORTER_ASSERT(reporter, isa_proc(path2));
  4785. REPORTER_ASSERT(reporter, path2.getConvexityOrUnknown() == SkPath::kConvex_Convexity);
  4786. // a path's isa and convexity should identity transform
  4787. path->transform(x.fIM, &path2);
  4788. path->transform(x.fIM);
  4789. REPORTER_ASSERT(reporter, isa_proc(path2));
  4790. REPORTER_ASSERT(reporter, path2.getConvexityOrUnknown() == SkPath::kConvex_Convexity);
  4791. REPORTER_ASSERT(reporter, isa_proc(*path));
  4792. REPORTER_ASSERT(reporter, path->getConvexityOrUnknown() == SkPath::kConvex_Convexity);
  4793. // a path's isa should survive translation, convexity depends on axis alignment
  4794. path->transform(x.fTM, &path2);
  4795. path->transform(x.fTM);
  4796. REPORTER_ASSERT(reporter, isa_proc(path2));
  4797. REPORTER_ASSERT(reporter, isa_proc(*path));
  4798. REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
  4799. REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
  4800. // a path's isa should survive scaling, convexity depends on axis alignment
  4801. path->transform(x.fSM, &path2);
  4802. path->transform(x.fSM);
  4803. REPORTER_ASSERT(reporter, isa_proc(path2));
  4804. REPORTER_ASSERT(reporter, isa_proc(*path));
  4805. REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
  4806. REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
  4807. // For security, post-rotation, we can't assume we're still convex. It might prove to be,
  4808. // in fact, still be convex, be we can't have cached that setting, hence the call to
  4809. // getConvexityOrUnknown() instead of getConvexity().
  4810. path->transform(x.fRM, &path2);
  4811. path->transform(x.fRM);
  4812. if (isAxisAligned) {
  4813. REPORTER_ASSERT(reporter, !isa_proc(path2));
  4814. REPORTER_ASSERT(reporter, !isa_proc(*path));
  4815. }
  4816. REPORTER_ASSERT(reporter, path2.getConvexityOrUnknown() != SkPath::kConvex_Convexity);
  4817. REPORTER_ASSERT(reporter, path->getConvexityOrUnknown() != SkPath::kConvex_Convexity);
  4818. }
  4819. DEF_TEST(Path_survive_transform, r) {
  4820. const Xforms x;
  4821. SkPath path;
  4822. path.addRect({10, 10, 40, 50});
  4823. survive(&path, x, true, r, [](const SkPath& p) { return p.isRect(nullptr); });
  4824. path.reset();
  4825. path.addOval({10, 10, 40, 50});
  4826. survive(&path, x, true, r, [](const SkPath& p) { return p.isOval(nullptr); });
  4827. path.reset();
  4828. path.addRRect(SkRRect::MakeRectXY({10, 10, 40, 50}, 5, 5));
  4829. survive(&path, x, true, r, [](const SkPath& p) { return p.isRRect(nullptr); });
  4830. // make a trapazoid; definitely convex, but not marked as axis-aligned (e.g. oval, rrect)
  4831. path.reset();
  4832. path.moveTo(0, 0).lineTo(100, 0).lineTo(70, 100).lineTo(30, 100);
  4833. REPORTER_ASSERT(r, path.getConvexity() == SkPath::kConvex_Convexity);
  4834. survive(&path, x, false, r, [](const SkPath& p) { return true; });
  4835. }
  4836. DEF_TEST(path_last_move_to_index, r) {
  4837. // Make sure that copyPath is safe after the call to path.offset().
  4838. // Previously, we would leave its fLastMoveToIndex alone after the copy, but now we should
  4839. // set it to path's value inside SkPath::transform()
  4840. const char text[] = "hello";
  4841. constexpr size_t len = sizeof(text) - 1;
  4842. SkGlyphID glyphs[len];
  4843. SkFont font;
  4844. font.textToGlyphs(text, len, SkTextEncoding::kUTF8, glyphs, len);
  4845. SkPath copyPath;
  4846. SkFont().getPaths(glyphs, len, [](const SkPath* src, const SkMatrix& mx, void* ctx) {
  4847. if (src) {
  4848. ((SkPath*)ctx)->addPath(*src, mx);
  4849. }
  4850. }, &copyPath);
  4851. SkScalar radii[] = { 80, 100, 0, 0, 40, 60, 0, 0 };
  4852. SkPath path;
  4853. path.addRoundRect({10, 10, 110, 110}, radii);
  4854. path.offset(0, 5, &(copyPath)); // <== change buffer copyPath.fPathRef->fPoints but not reset copyPath.fLastMoveToIndex lead to out of bound
  4855. copyPath.rConicTo(1, 1, 3, 3, 0.707107f);
  4856. }