picture_layer_impl_unittest.cc 265 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "cc/layers/picture_layer_impl.h"
  5. #include <stddef.h>
  6. #include <algorithm>
  7. #include <limits>
  8. #include <memory>
  9. #include <set>
  10. #include <utility>
  11. #include "base/location.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/test/scoped_feature_list.h"
  14. #include "base/threading/thread_task_runner_handle.h"
  15. #include "cc/animation/animation_host.h"
  16. #include "cc/base/math_util.h"
  17. #include "cc/layers/append_quads_data.h"
  18. #include "cc/layers/picture_layer.h"
  19. #include "cc/test/fake_content_layer_client.h"
  20. #include "cc/test/fake_impl_task_runner_provider.h"
  21. #include "cc/test/fake_layer_tree_frame_sink.h"
  22. #include "cc/test/fake_layer_tree_host.h"
  23. #include "cc/test/fake_layer_tree_host_client.h"
  24. #include "cc/test/fake_layer_tree_host_impl.h"
  25. #include "cc/test/fake_picture_layer_impl.h"
  26. #include "cc/test/fake_raster_source.h"
  27. #include "cc/test/fake_recording_source.h"
  28. #include "cc/test/layer_tree_impl_test_base.h"
  29. #include "cc/test/skia_common.h"
  30. #include "cc/test/test_layer_tree_host_base.h"
  31. #include "cc/test/test_paint_worklet_input.h"
  32. #include "cc/test/test_task_graph_runner.h"
  33. #include "cc/tiles/tiling_set_raster_queue_all.h"
  34. #include "cc/tiles/tiling_set_raster_queue_required.h"
  35. #include "cc/trees/layer_tree_impl.h"
  36. #include "components/viz/common/quads/draw_quad.h"
  37. #include "components/viz/common/quads/tile_draw_quad.h"
  38. #include "components/viz/test/begin_frame_args_test.h"
  39. #include "testing/gtest/include/gtest/gtest.h"
  40. #include "ui/base/ui_base_features.h"
  41. #include "ui/gfx/geometry/rect_conversions.h"
  42. #include "ui/gfx/geometry/size_conversions.h"
  43. #include "ui/gfx/geometry/test/geometry_util.h"
  44. namespace cc {
  45. namespace {
  46. #define EXPECT_BOTH_EQ(expression, x) \
  47. do { \
  48. EXPECT_EQ(x, pending_layer()->expression); \
  49. EXPECT_EQ(x, active_layer()->expression); \
  50. } while (false)
  51. #define EXPECT_BOTH_NE(expression, x) \
  52. do { \
  53. EXPECT_NE(x, pending_layer()->expression); \
  54. EXPECT_NE(x, active_layer()->expression); \
  55. } while (false)
  56. #define EXPECT_BOTH_TRUE(expression) \
  57. do { \
  58. EXPECT_TRUE(pending_layer()->expression); \
  59. EXPECT_TRUE(active_layer()->expression); \
  60. } while (false)
  61. #define EXPECT_BOTH_FALSE(expression) \
  62. do { \
  63. EXPECT_FALSE(pending_layer()->expression); \
  64. EXPECT_FALSE(active_layer()->expression); \
  65. } while (false)
  66. class PictureLayerImplTest : public TestLayerTreeHostBase {
  67. public:
  68. void SetUp() override {
  69. TestLayerTreeHostBase::SetUp();
  70. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(10000, 10000));
  71. }
  72. LayerTreeSettings CreateSettings() override {
  73. auto settings = TestLayerTreeHostBase::CreateSettings();
  74. settings.commit_to_active_tree = false;
  75. settings.create_low_res_tiling = true;
  76. return settings;
  77. }
  78. std::unique_ptr<LayerTreeFrameSink> CreateLayerTreeFrameSink() override {
  79. return FakeLayerTreeFrameSink::Create3dForGpuRasterization();
  80. }
  81. void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
  82. const gfx::Size& tile_size,
  83. const Region& invalidation) {
  84. scoped_refptr<FakeRasterSource> pending_raster_source =
  85. FakeRasterSource::CreateFilled(layer_bounds);
  86. scoped_refptr<FakeRasterSource> active_raster_source =
  87. FakeRasterSource::CreateFilled(layer_bounds);
  88. SetupTreesWithFixedTileSize(std::move(pending_raster_source),
  89. std::move(active_raster_source), tile_size,
  90. invalidation);
  91. }
  92. void SetupTreesWithFixedTileSize(
  93. scoped_refptr<RasterSource> pending_raster_source,
  94. scoped_refptr<RasterSource> active_raster_source,
  95. const gfx::Size& tile_size,
  96. const Region& pending_invalidation) {
  97. SetupPendingTree(std::move(active_raster_source), tile_size, Region());
  98. ActivateTree();
  99. SetupPendingTree(std::move(pending_raster_source), tile_size,
  100. pending_invalidation);
  101. }
  102. void SetupDefaultTreesWithInvalidation(const gfx::Size& layer_bounds,
  103. const Region& invalidation) {
  104. scoped_refptr<FakeRasterSource> pending_raster_source =
  105. FakeRasterSource::CreateFilled(layer_bounds);
  106. scoped_refptr<FakeRasterSource> active_raster_source =
  107. FakeRasterSource::CreateFilled(layer_bounds);
  108. SetupTreesWithInvalidation(std::move(pending_raster_source),
  109. std::move(active_raster_source), invalidation);
  110. }
  111. void SetupTreesWithInvalidation(
  112. scoped_refptr<RasterSource> pending_raster_source,
  113. scoped_refptr<RasterSource> active_raster_source,
  114. const Region& pending_invalidation) {
  115. SetupPendingTree(std::move(active_raster_source), gfx::Size(), Region());
  116. ActivateTree();
  117. SetupPendingTree(std::move(pending_raster_source), gfx::Size(),
  118. pending_invalidation);
  119. }
  120. void SetupPendingTreeWithInvalidation(
  121. scoped_refptr<RasterSource> raster_source,
  122. const Region& invalidation) {
  123. SetupPendingTree(std::move(raster_source), gfx::Size(), invalidation);
  124. }
  125. void SetupPendingTreeWithFixedTileSize(
  126. scoped_refptr<RasterSource> raster_source,
  127. const gfx::Size& tile_size,
  128. const Region& invalidation) {
  129. SetupPendingTree(std::move(raster_source), tile_size, invalidation);
  130. }
  131. void SetupDrawProperties(FakePictureLayerImpl* layer,
  132. const gfx::Vector2dF& ideal_contents_scale,
  133. float device_scale_factor,
  134. float page_scale_factor) {
  135. layer->layer_tree_impl()->SetDeviceScaleFactor(device_scale_factor);
  136. host_impl()->active_tree()->SetPageScaleOnActiveTree(page_scale_factor);
  137. gfx::Transform scale_transform;
  138. scale_transform.Scale(ideal_contents_scale.x(), ideal_contents_scale.y());
  139. layer->draw_properties().screen_space_transform = scale_transform;
  140. layer->draw_properties().target_space_transform = scale_transform;
  141. layer->set_contributes_to_drawn_render_surface(true);
  142. DCHECK_EQ(layer->GetIdealContentsScale(), ideal_contents_scale);
  143. }
  144. void SetupDrawProperties(FakePictureLayerImpl* layer,
  145. float ideal_contents_scale,
  146. float device_scale_factor,
  147. float page_scale_factor) {
  148. SetupDrawProperties(
  149. layer, gfx::Vector2dF(ideal_contents_scale, ideal_contents_scale),
  150. device_scale_factor, page_scale_factor);
  151. }
  152. void SetupDrawPropertiesAndUpdateTiles(
  153. FakePictureLayerImpl* layer,
  154. const gfx::Vector2dF& ideal_contents_scale,
  155. float device_scale_factor,
  156. float page_scale_factor) {
  157. SetupDrawProperties(layer, ideal_contents_scale, device_scale_factor,
  158. page_scale_factor);
  159. layer->UpdateTiles();
  160. }
  161. void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
  162. float ideal_contents_scale,
  163. float device_scale_factor,
  164. float page_scale_factor) {
  165. SetupDrawPropertiesAndUpdateTiles(
  166. layer, gfx::Vector2dF(ideal_contents_scale, ideal_contents_scale),
  167. device_scale_factor, page_scale_factor);
  168. }
  169. static void VerifyAllPrioritizedTilesExistAndHaveRasterSource(
  170. const PictureLayerTiling* tiling,
  171. RasterSource* raster_source) {
  172. auto prioritized_tiles =
  173. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  174. for (PictureLayerTiling::CoverageIterator iter(
  175. tiling, tiling->contents_scale_key(),
  176. gfx::Rect(tiling->tiling_size()));
  177. iter; ++iter) {
  178. EXPECT_TRUE(*iter);
  179. EXPECT_EQ(raster_source, prioritized_tiles[*iter].raster_source());
  180. }
  181. }
  182. void SetMaximumAnimationToScreenScale(FakePictureLayerImpl* layer,
  183. float maximum_animation_to_screen_scale,
  184. bool affected_by_invalid_scale) {
  185. layer->layer_tree_impl()
  186. ->property_trees()
  187. ->SetMaximumAnimationToScreenScaleForTesting(
  188. layer->transform_tree_index(), maximum_animation_to_screen_scale,
  189. affected_by_invalid_scale);
  190. layer->draw_properties().screen_space_transform_is_animating = true;
  191. }
  192. void SetContentsScaleOnBothLayers(float contents_scale,
  193. float device_scale_factor,
  194. float page_scale_factor) {
  195. // Sets arbitrary animation scale to ensure it's not used in any way.
  196. constexpr float kArbitraryScale = 12345.0f;
  197. SetMaximumAnimationToScreenScale(pending_layer(), kArbitraryScale, false);
  198. pending_layer()->draw_properties().screen_space_transform_is_animating =
  199. false;
  200. SetupDrawPropertiesAndUpdateTiles(pending_layer(), contents_scale,
  201. device_scale_factor, page_scale_factor);
  202. SetMaximumAnimationToScreenScale(active_layer(), kArbitraryScale, false);
  203. active_layer()->draw_properties().screen_space_transform_is_animating =
  204. false;
  205. SetupDrawPropertiesAndUpdateTiles(active_layer(), contents_scale,
  206. device_scale_factor, page_scale_factor);
  207. }
  208. void SetContentsAndAnimationScalesOnBothLayers(
  209. float contents_scale,
  210. float device_scale_factor,
  211. float page_scale_factor,
  212. float maximum_animation_scale,
  213. bool affected_by_invalid_scale) {
  214. SetMaximumAnimationToScreenScale(pending_layer(), maximum_animation_scale,
  215. affected_by_invalid_scale);
  216. SetupDrawPropertiesAndUpdateTiles(pending_layer(), contents_scale,
  217. device_scale_factor, page_scale_factor);
  218. SetMaximumAnimationToScreenScale(active_layer(), maximum_animation_scale,
  219. affected_by_invalid_scale);
  220. SetupDrawPropertiesAndUpdateTiles(active_layer(), contents_scale,
  221. device_scale_factor, page_scale_factor);
  222. }
  223. void ResetTilingsAndRasterScales() {
  224. if (pending_layer()) {
  225. pending_layer()->ReleaseTileResources();
  226. EXPECT_TRUE(pending_layer()->tilings());
  227. EXPECT_EQ(0u, pending_layer()->num_tilings());
  228. pending_layer()->RecreateTileResources();
  229. EXPECT_EQ(0u, pending_layer()->num_tilings());
  230. }
  231. if (active_layer()) {
  232. active_layer()->ReleaseTileResources();
  233. EXPECT_TRUE(active_layer()->tilings());
  234. EXPECT_EQ(0u, pending_layer()->num_tilings());
  235. active_layer()->RecreateTileResources();
  236. EXPECT_EQ(0u, active_layer()->tilings()->num_tilings());
  237. }
  238. }
  239. size_t NumberOfTilesRequired(PictureLayerTiling* tiling) {
  240. size_t num_required = 0;
  241. std::vector<Tile*> tiles = tiling->AllTilesForTesting();
  242. for (size_t i = 0; i < tiles.size(); ++i) {
  243. if (tiles[i]->required_for_activation())
  244. num_required++;
  245. }
  246. return num_required;
  247. }
  248. void AssertAllTilesRequired(PictureLayerTiling* tiling) {
  249. std::vector<Tile*> tiles = tiling->AllTilesForTesting();
  250. for (size_t i = 0; i < tiles.size(); ++i)
  251. EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
  252. EXPECT_GT(tiles.size(), 0u);
  253. }
  254. void AssertNoTilesRequired(PictureLayerTiling* tiling) {
  255. std::vector<Tile*> tiles = tiling->AllTilesForTesting();
  256. for (size_t i = 0; i < tiles.size(); ++i)
  257. EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
  258. EXPECT_GT(tiles.size(), 0u);
  259. }
  260. void SetInitialDeviceScaleFactor(float device_scale_factor) {
  261. // Device scale factor is a per-tree property. However, tests can't directly
  262. // set the pending tree's device scale factor before the pending tree is
  263. // created, and setting it after SetupPendingTree is too late, since
  264. // draw properties will already have been updated on the tree. To handle
  265. // this, we initially set only the active tree's device scale factor, and we
  266. // copy this over to the pending tree inside SetupPendingTree.
  267. host_impl()->active_tree()->SetDeviceScaleFactor(device_scale_factor);
  268. }
  269. void TestQuadsForSolidColor(bool test_for_solid, bool partial_opaque);
  270. };
  271. // Legacy PictureLayerImplTest which forces SW rasterization. New tests should
  272. // default to the more common GPU rasterization path.
  273. class LegacySWPictureLayerImplTest : public PictureLayerImplTest {
  274. public:
  275. LayerTreeSettings CreateSettings() override {
  276. auto settings = PictureLayerImplTest::CreateSettings();
  277. settings.gpu_rasterization_disabled = true;
  278. return settings;
  279. }
  280. };
  281. class CommitToActiveTreePictureLayerImplTest : public PictureLayerImplTest {
  282. public:
  283. LayerTreeSettings CreateSettings() override {
  284. LayerTreeSettings settings = PictureLayerImplTest::CreateSettings();
  285. settings.commit_to_active_tree = true;
  286. return settings;
  287. }
  288. };
  289. class NoLowResPictureLayerImplTest : public LegacySWPictureLayerImplTest {
  290. public:
  291. LayerTreeSettings CreateSettings() override {
  292. LayerTreeSettings settings = LegacySWPictureLayerImplTest::CreateSettings();
  293. settings.create_low_res_tiling = false;
  294. return settings;
  295. }
  296. };
  297. TEST_F(LegacySWPictureLayerImplTest, CloneNoInvalidation) {
  298. gfx::Size layer_bounds(400, 400);
  299. SetupDefaultTrees(layer_bounds);
  300. EXPECT_EQ(pending_layer()->tilings()->num_tilings(),
  301. active_layer()->tilings()->num_tilings());
  302. const PictureLayerTilingSet* tilings = pending_layer()->tilings();
  303. EXPECT_GT(tilings->num_tilings(), 0u);
  304. for (size_t i = 0; i < tilings->num_tilings(); ++i)
  305. EXPECT_TRUE(tilings->tiling_at(i)->AllTilesForTesting().empty());
  306. }
  307. TEST_F(LegacySWPictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
  308. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  309. gfx::Size layer_bounds(400, 400);
  310. SetupDefaultTrees(layer_bounds);
  311. SetupDrawPropertiesAndUpdateTiles(active_layer(), 1.f, 1.f, 1.f);
  312. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  313. // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
  314. // identify transform for tile priority.
  315. gfx::Rect viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
  316. gfx::Transform transform_for_tile_priority;
  317. host_impl()->SetExternalTilePriorityConstraints(
  318. viewport_rect_for_tile_priority, transform_for_tile_priority);
  319. UpdateDrawProperties(host_impl()->active_tree());
  320. // Verify the viewport rect for tile priority is used in picture layer tiling.
  321. EXPECT_EQ(viewport_rect_for_tile_priority,
  322. active_layer()->viewport_rect_for_tile_priority_in_content_space());
  323. PictureLayerTilingSet* tilings = active_layer()->tilings();
  324. for (size_t i = 0; i < tilings->num_tilings(); i++) {
  325. PictureLayerTiling* tiling = tilings->tiling_at(i);
  326. EXPECT_EQ(tiling->GetCurrentVisibleRectForTesting(),
  327. gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority,
  328. tiling->contents_scale_key()));
  329. }
  330. // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
  331. // root layer space and the transform for tile priority is translated and
  332. // rotated.
  333. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  334. viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
  335. transform_for_tile_priority.Translate(100, 100);
  336. transform_for_tile_priority.Rotate(45);
  337. host_impl()->SetExternalTilePriorityConstraints(
  338. viewport_rect_for_tile_priority, transform_for_tile_priority);
  339. UpdateDrawProperties(host_impl()->active_tree());
  340. EXPECT_EQ(viewport_rect_for_tile_priority,
  341. active_layer()->viewport_rect_for_tile_priority_in_content_space());
  342. tilings = active_layer()->tilings();
  343. for (size_t i = 0; i < tilings->num_tilings(); i++) {
  344. PictureLayerTiling* tiling = tilings->tiling_at(i);
  345. EXPECT_EQ(tiling->GetCurrentVisibleRectForTesting(),
  346. gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority,
  347. tiling->contents_scale_key()));
  348. }
  349. }
  350. TEST_F(LegacySWPictureLayerImplTest, ViewportRectForTilePriorityIsCached) {
  351. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  352. gfx::Size layer_bounds(400, 400);
  353. SetupDefaultTrees(layer_bounds);
  354. SetupDrawPropertiesAndUpdateTiles(active_layer(), 1.f, 1.f, 1.f);
  355. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  356. gfx::Rect viewport_rect_for_tile_priority(0, 0, 100, 100);
  357. gfx::Transform transform_for_tile_priority;
  358. host_impl()->SetExternalTilePriorityConstraints(
  359. viewport_rect_for_tile_priority, transform_for_tile_priority);
  360. UpdateDrawProperties(host_impl()->active_tree());
  361. EXPECT_EQ(viewport_rect_for_tile_priority,
  362. active_layer()->viewport_rect_for_tile_priority_in_content_space());
  363. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  364. gfx::Rect another_viewport_rect_for_tile_priority(11, 11, 50, 50);
  365. host_impl()->SetExternalTilePriorityConstraints(
  366. another_viewport_rect_for_tile_priority, transform_for_tile_priority);
  367. // Didn't call UpdateDrawProperties yet. The viewport rect for tile priority
  368. // should remain to be the previously cached value.
  369. EXPECT_EQ(viewport_rect_for_tile_priority,
  370. active_layer()->viewport_rect_for_tile_priority_in_content_space());
  371. UpdateDrawProperties(host_impl()->active_tree());
  372. // Now the UpdateDrawProperties is called. The viewport rect for tile
  373. // priority should be the latest value.
  374. EXPECT_EQ(another_viewport_rect_for_tile_priority,
  375. active_layer()->viewport_rect_for_tile_priority_in_content_space());
  376. }
  377. TEST_F(LegacySWPictureLayerImplTest, ClonePartialInvalidation) {
  378. gfx::Size layer_bounds(400, 400);
  379. gfx::Rect layer_invalidation(150, 200, 30, 180);
  380. scoped_refptr<FakeRasterSource> pending_raster_source =
  381. FakeRasterSource::CreateFilled(layer_bounds);
  382. scoped_refptr<FakeRasterSource> active_raster_source =
  383. FakeRasterSource::CreateFilled(layer_bounds);
  384. scoped_refptr<FakeRasterSource> lost_raster_source =
  385. FakeRasterSource::CreateFilled(layer_bounds);
  386. SetupPendingTreeWithFixedTileSize(lost_raster_source, gfx::Size(50, 50),
  387. Region());
  388. ActivateTree();
  389. {
  390. // Add a unique tiling on the active tree.
  391. PictureLayerTiling* tiling =
  392. active_layer()->AddTiling(gfx::AxisTransform2d(3.f, gfx::Vector2dF()));
  393. tiling->set_resolution(HIGH_RESOLUTION);
  394. tiling->CreateAllTilesForTesting();
  395. }
  396. // Ensure UpdateTiles won't remove any tilings.
  397. active_layer()->MarkAllTilingsUsed();
  398. // Then setup a new pending tree and activate it.
  399. SetupTreesWithFixedTileSize(pending_raster_source, active_raster_source,
  400. gfx::Size(50, 50), layer_invalidation);
  401. EXPECT_EQ(1u, pending_layer()->num_tilings());
  402. EXPECT_EQ(3u, active_layer()->num_tilings());
  403. const PictureLayerTilingSet* tilings = pending_layer()->tilings();
  404. EXPECT_GT(tilings->num_tilings(), 0u);
  405. for (size_t i = 0; i < tilings->num_tilings(); ++i) {
  406. const PictureLayerTiling* tiling = tilings->tiling_at(i);
  407. gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
  408. layer_invalidation, tiling->contents_scale_key());
  409. auto prioritized_tiles =
  410. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  411. for (PictureLayerTiling::CoverageIterator iter(
  412. tiling, tiling->contents_scale_key(),
  413. gfx::Rect(tiling->tiling_size()));
  414. iter; ++iter) {
  415. // We don't always have a tile, but when we do it's because it was
  416. // invalidated and it has the latest raster source.
  417. if (*iter) {
  418. EXPECT_FALSE(iter.geometry_rect().IsEmpty());
  419. EXPECT_EQ(pending_raster_source.get(),
  420. prioritized_tiles[*iter].raster_source());
  421. EXPECT_TRUE(iter.geometry_rect().Intersects(content_invalidation));
  422. } else {
  423. // We don't create tiles in non-invalidated regions.
  424. EXPECT_FALSE(iter.geometry_rect().Intersects(content_invalidation));
  425. }
  426. }
  427. }
  428. tilings = active_layer()->tilings();
  429. EXPECT_GT(tilings->num_tilings(), 0u);
  430. for (size_t i = 0; i < tilings->num_tilings(); ++i) {
  431. const PictureLayerTiling* tiling = tilings->tiling_at(i);
  432. auto prioritized_tiles =
  433. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  434. for (PictureLayerTiling::CoverageIterator iter(
  435. tiling, tiling->contents_scale_key(),
  436. gfx::Rect(tiling->tiling_size()));
  437. iter; ++iter) {
  438. EXPECT_TRUE(*iter);
  439. EXPECT_FALSE(iter.geometry_rect().IsEmpty());
  440. // Raster source will be updated upon activation.
  441. EXPECT_EQ(active_raster_source.get(),
  442. prioritized_tiles[*iter].raster_source());
  443. }
  444. }
  445. }
  446. TEST_F(LegacySWPictureLayerImplTest, CloneFullInvalidation) {
  447. gfx::Size layer_bounds(300, 500);
  448. scoped_refptr<FakeRasterSource> pending_raster_source =
  449. FakeRasterSource::CreateFilled(layer_bounds);
  450. scoped_refptr<FakeRasterSource> active_raster_source =
  451. FakeRasterSource::CreateFilled(layer_bounds);
  452. SetupTreesWithInvalidation(pending_raster_source, active_raster_source,
  453. gfx::Rect(layer_bounds));
  454. EXPECT_EQ(pending_layer()->tilings()->num_tilings(),
  455. active_layer()->tilings()->num_tilings());
  456. const PictureLayerTilingSet* tilings = pending_layer()->tilings();
  457. EXPECT_GT(tilings->num_tilings(), 0u);
  458. for (size_t i = 0; i < tilings->num_tilings(); ++i) {
  459. VerifyAllPrioritizedTilesExistAndHaveRasterSource(
  460. tilings->tiling_at(i), pending_raster_source.get());
  461. }
  462. }
  463. TEST_F(LegacySWPictureLayerImplTest, UpdateTilesCreatesTilings) {
  464. gfx::Size layer_bounds(1300, 1900);
  465. SetupDefaultTrees(layer_bounds);
  466. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  467. EXPECT_LT(low_res_factor, 1.f);
  468. active_layer()->ReleaseTileResources();
  469. EXPECT_TRUE(active_layer()->tilings());
  470. EXPECT_EQ(0u, active_layer()->num_tilings());
  471. active_layer()->RecreateTileResources();
  472. EXPECT_EQ(0u, active_layer()->num_tilings());
  473. SetupDrawPropertiesAndUpdateTiles(active_layer(),
  474. 6.f, // ideal contents scale
  475. 3.f, // device scale
  476. 2.f); // page scale
  477. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  478. EXPECT_FLOAT_EQ(
  479. 6.f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  480. EXPECT_FLOAT_EQ(
  481. 6.f * low_res_factor,
  482. active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  483. // If we change the page scale factor, then we should get new tilings.
  484. SetupDrawPropertiesAndUpdateTiles(active_layer(),
  485. 6.6f, // ideal contents scale
  486. 3.f, // device scale
  487. 2.2f); // page scale
  488. ASSERT_EQ(4u, active_layer()->tilings()->num_tilings());
  489. EXPECT_FLOAT_EQ(
  490. 6.6f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  491. EXPECT_FLOAT_EQ(
  492. 6.6f * low_res_factor,
  493. active_layer()->tilings()->tiling_at(2)->contents_scale_key());
  494. // If we change the device scale factor, then we should get new tilings.
  495. SetupDrawPropertiesAndUpdateTiles(active_layer(),
  496. 7.26f, // ideal contents scale
  497. 3.3f, // device scale
  498. 2.2f); // page scale
  499. ASSERT_EQ(6u, active_layer()->tilings()->num_tilings());
  500. EXPECT_FLOAT_EQ(
  501. 7.26f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  502. EXPECT_FLOAT_EQ(
  503. 7.26f * low_res_factor,
  504. active_layer()->tilings()->tiling_at(3)->contents_scale_key());
  505. // If we change the device scale factor, but end up at the same total scale
  506. // factor somehow, then we don't get new tilings.
  507. SetupDrawPropertiesAndUpdateTiles(active_layer(),
  508. 7.26f, // ideal contents scale
  509. 2.2f, // device scale
  510. 3.3f); // page scale
  511. ASSERT_EQ(6u, active_layer()->tilings()->num_tilings());
  512. EXPECT_FLOAT_EQ(
  513. 7.26f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  514. EXPECT_FLOAT_EQ(
  515. 7.26f * low_res_factor,
  516. active_layer()->tilings()->tiling_at(3)->contents_scale_key());
  517. }
  518. TEST_F(LegacySWPictureLayerImplTest, PendingLayerOnlyHasHighResTiling) {
  519. gfx::Size layer_bounds(1300, 1900);
  520. SetupDefaultTrees(layer_bounds);
  521. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  522. EXPECT_LT(low_res_factor, 1.f);
  523. pending_layer()->ReleaseTileResources();
  524. pending_layer()->RecreateTileResources();
  525. EXPECT_EQ(0u, pending_layer()->tilings()->num_tilings());
  526. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  527. 6.f, // ideal contents scale
  528. 3.f, // device scale
  529. 2.f); // page scale
  530. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  531. EXPECT_FLOAT_EQ(
  532. 6.f, pending_layer()->tilings()->tiling_at(0)->contents_scale_key());
  533. // If we change the page scale factor, then we should get new tilings.
  534. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  535. 6.6f, // ideal contents scale
  536. 3.f, // device scale
  537. 2.2f); // page scale
  538. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  539. EXPECT_FLOAT_EQ(
  540. 6.6f, pending_layer()->tilings()->tiling_at(0)->contents_scale_key());
  541. // If we change the device scale factor, then we should get new tilings.
  542. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  543. 7.26f, // ideal contents scale
  544. 3.3f, // device scale
  545. 2.2f); // page scale
  546. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  547. EXPECT_FLOAT_EQ(
  548. 7.26f, pending_layer()->tilings()->tiling_at(0)->contents_scale_key());
  549. // If we change the device scale factor, but end up at the same total scale
  550. // factor somehow, then we don't get new tilings.
  551. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  552. 7.26f, // ideal contents scale
  553. 2.2f, // device scale
  554. 3.3f); // page scale
  555. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  556. EXPECT_FLOAT_EQ(
  557. 7.26f, pending_layer()->tilings()->tiling_at(0)->contents_scale_key());
  558. }
  559. TEST_F(LegacySWPictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
  560. // This test makes sure that if a layer can have tilings, then a commit makes
  561. // it not able to have tilings (empty size), and then a future commit that
  562. // makes it valid again should be able to create tilings.
  563. gfx::Size layer_bounds(1300, 1900);
  564. scoped_refptr<FakeRasterSource> empty_raster_source =
  565. FakeRasterSource::CreateEmpty(layer_bounds);
  566. scoped_refptr<FakeRasterSource> valid_raster_source =
  567. FakeRasterSource::CreateFilled(layer_bounds);
  568. SetupPendingTree(valid_raster_source);
  569. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  570. ActivateTree();
  571. SetupPendingTree(empty_raster_source);
  572. EXPECT_FALSE(pending_layer()->CanHaveTilings());
  573. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  574. ASSERT_EQ(0u, pending_layer()->tilings()->num_tilings());
  575. ActivateTree();
  576. EXPECT_FALSE(active_layer()->CanHaveTilings());
  577. ASSERT_EQ(0u, active_layer()->tilings()->num_tilings());
  578. SetupPendingTree(valid_raster_source);
  579. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  580. ASSERT_EQ(0u, active_layer()->tilings()->num_tilings());
  581. }
  582. TEST_F(LegacySWPictureLayerImplTest, LowResTilingStaysOnActiveTree) {
  583. gfx::Size layer_bounds(1300, 1900);
  584. scoped_refptr<FakeRasterSource> valid_raster_source =
  585. FakeRasterSource::CreateFilled(layer_bounds);
  586. scoped_refptr<FakeRasterSource> other_valid_raster_source =
  587. FakeRasterSource::CreateFilled(layer_bounds);
  588. SetupPendingTree(valid_raster_source);
  589. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  590. ActivateTree();
  591. SetupPendingTree(other_valid_raster_source);
  592. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  593. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  594. auto* low_res_tiling =
  595. active_layer()->tilings()->FindTilingWithResolution(LOW_RESOLUTION);
  596. EXPECT_TRUE(low_res_tiling);
  597. ActivateTree();
  598. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  599. auto* other_low_res_tiling =
  600. active_layer()->tilings()->FindTilingWithResolution(LOW_RESOLUTION);
  601. EXPECT_TRUE(other_low_res_tiling);
  602. EXPECT_EQ(low_res_tiling, other_low_res_tiling);
  603. }
  604. TEST_F(LegacySWPictureLayerImplTest, ZoomOutCrash) {
  605. gfx::Size layer_bounds(1300, 1900);
  606. // Set up the high and low res tilings before pinch zoom.
  607. SetupDefaultTrees(layer_bounds);
  608. ResetTilingsAndRasterScales();
  609. EXPECT_EQ(0u, active_layer()->tilings()->num_tilings());
  610. SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f);
  611. EXPECT_EQ(32.f, active_layer()->HighResTiling()->contents_scale_key());
  612. // Since this test simulates a pinch it needs an input handler.
  613. // TODO(bokan): This is a raster unit test, it shouldn't be using a real
  614. // input handler.
  615. InputHandler::Create(static_cast<CompositorDelegateForInput&>(*host_impl()));
  616. host_impl()->GetInputHandler().PinchGestureBegin(
  617. gfx::Point(), ui::ScrollInputType::kTouchscreen);
  618. SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f);
  619. SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f);
  620. EXPECT_EQ(active_layer()->tilings()->NumHighResTilings(), 1);
  621. }
  622. TEST_F(LegacySWPictureLayerImplTest, ScaledBoundsOverflowInt) {
  623. // Limit visible size.
  624. gfx::Size viewport_size(1, 1);
  625. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  626. gfx::Size layer_bounds(600000, 60);
  627. // Set up the high and low res tilings before pinch zoom.
  628. SetupDefaultTrees(layer_bounds);
  629. ResetTilingsAndRasterScales();
  630. EXPECT_EQ(0u, active_layer()->tilings()->num_tilings());
  631. float scale = 8000.f;
  632. // Verify this will overflow an int.
  633. EXPECT_GT(static_cast<float>(layer_bounds.width()) * scale,
  634. static_cast<float>(std::numeric_limits<int>::max()));
  635. SetContentsScaleOnBothLayers(scale, 1.0f, scale);
  636. float adjusted_scale = active_layer()->HighResTiling()->contents_scale_key();
  637. EXPECT_LT(adjusted_scale, scale);
  638. // PopulateSharedQuadState CHECKs for overflows.
  639. // See http://crbug.com/679035
  640. active_layer()->draw_properties().visible_layer_rect =
  641. gfx::Rect(layer_bounds);
  642. viz::SharedQuadState state;
  643. active_layer()->PopulateScaledSharedQuadState(
  644. &state, adjusted_scale, active_layer()->contents_opaque());
  645. }
  646. TEST_F(LegacySWPictureLayerImplTest, PinchGestureTilings) {
  647. gfx::Size layer_bounds(1300, 1900);
  648. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  649. // Set up the high and low res tilings before pinch zoom.
  650. SetupDefaultTrees(layer_bounds);
  651. ResetTilingsAndRasterScales();
  652. SetContentsScaleOnBothLayers(2.f, 1.0f, 2.f);
  653. ASSERT_EQ(active_layer()->num_tilings(), 2u);
  654. ASSERT_EQ(pending_layer()->num_tilings(), 1u);
  655. EXPECT_EQ(active_layer()->tilings()->tiling_at(0)->contents_scale_key(), 2.f);
  656. EXPECT_EQ(active_layer()->tilings()->tiling_at(1)->contents_scale_key(),
  657. 2.f * low_res_factor);
  658. // One of the tilings has to be a low resolution one.
  659. EXPECT_EQ(LOW_RESOLUTION,
  660. active_layer()->tilings()->tiling_at(1)->resolution());
  661. // Ensure UpdateTiles won't remove any tilings.
  662. active_layer()->MarkAllTilingsUsed();
  663. // Since this test simulates a pinch it needs an input handler.
  664. // TODO(bokan): This is a raster unit test, it shouldn't be using a real
  665. // input handler.
  666. InputHandler::Create(static_cast<CompositorDelegateForInput&>(*host_impl()));
  667. // Start a pinch gesture.
  668. host_impl()->GetInputHandler().PinchGestureBegin(
  669. gfx::Point(), ui::ScrollInputType::kTouchscreen);
  670. // Zoom out by a small amount. We should create a tiling at half
  671. // the scale (2/kMaxScaleRatioDuringPinch).
  672. SetContentsScaleOnBothLayers(1.8f, 1.0f, 1.8f);
  673. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  674. EXPECT_FLOAT_EQ(
  675. 2.0f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  676. EXPECT_FLOAT_EQ(
  677. 1.0f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  678. EXPECT_FLOAT_EQ(
  679. 2.0f * low_res_factor,
  680. active_layer()->tilings()->tiling_at(2)->contents_scale_key());
  681. // Since we're pinching, we shouldn't create a low resolution tiling.
  682. EXPECT_FALSE(
  683. active_layer()->tilings()->FindTilingWithResolution(LOW_RESOLUTION));
  684. // Ensure UpdateTiles won't remove any tilings.
  685. active_layer()->MarkAllTilingsUsed();
  686. // Zoom out further, close to our low-res scale factor. We should
  687. // use that tiling as high-res, and not create a new tiling.
  688. SetContentsScaleOnBothLayers(low_res_factor * 2.1f, 1.0f,
  689. low_res_factor * 2.1f);
  690. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  691. EXPECT_FALSE(
  692. active_layer()->tilings()->FindTilingWithResolution(LOW_RESOLUTION));
  693. // Zoom in a lot now. Since we increase by increments of
  694. // kMaxScaleRatioDuringPinch, this will create a new tiling at 4.0.
  695. SetContentsScaleOnBothLayers(3.8f, 1.0f, 3.8f);
  696. ASSERT_EQ(4u, active_layer()->tilings()->num_tilings());
  697. EXPECT_FLOAT_EQ(
  698. 4.0f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  699. // Although one of the tilings matches the low resolution scale, it still
  700. // shouldn't be marked as low resolution since we're pinching.
  701. auto* low_res_tiling =
  702. active_layer()->tilings()->FindTilingWithScaleKey(4.f * low_res_factor);
  703. EXPECT_TRUE(low_res_tiling);
  704. EXPECT_NE(LOW_RESOLUTION, low_res_tiling->resolution());
  705. // Stop a pinch gesture.
  706. host_impl()->GetInputHandler().PinchGestureEnd(gfx::Point());
  707. // Ensure UpdateTiles won't remove any tilings.
  708. active_layer()->MarkAllTilingsUsed();
  709. // After pinch ends, set the scale to what the raster scale was updated to
  710. // (checked above).
  711. SetContentsScaleOnBothLayers(4.0f, 1.0f, 4.0f);
  712. ASSERT_EQ(4u, active_layer()->tilings()->num_tilings());
  713. EXPECT_FLOAT_EQ(
  714. 4.0f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  715. // Now that we stopped pinching, the low resolution tiling that existed should
  716. // now be marked as low resolution.
  717. low_res_tiling =
  718. active_layer()->tilings()->FindTilingWithScaleKey(4.f * low_res_factor);
  719. EXPECT_TRUE(low_res_tiling);
  720. EXPECT_EQ(LOW_RESOLUTION, low_res_tiling->resolution());
  721. }
  722. TEST_F(LegacySWPictureLayerImplTest, SnappedTilingDuringZoom) {
  723. gfx::Size layer_bounds(2600, 3800);
  724. SetupDefaultTrees(layer_bounds);
  725. ResetTilingsAndRasterScales();
  726. EXPECT_EQ(0u, active_layer()->tilings()->num_tilings());
  727. // Set up the high and low res tilings before pinch zoom.
  728. SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f);
  729. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  730. EXPECT_FLOAT_EQ(
  731. 0.24f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  732. EXPECT_FLOAT_EQ(
  733. 0.06f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  734. // Ensure UpdateTiles won't remove any tilings.
  735. active_layer()->MarkAllTilingsUsed();
  736. // Since this test simulates a pinch it needs an input handler.
  737. // TODO(bokan): This is a raster unit test, it shouldn't be using a real
  738. // input handler.
  739. InputHandler::Create(static_cast<CompositorDelegateForInput&>(*host_impl()));
  740. // Start a pinch gesture.
  741. host_impl()->GetInputHandler().PinchGestureBegin(
  742. gfx::Point(), ui::ScrollInputType::kTouchscreen);
  743. // Zoom out by a small amount. We should create a tiling at half
  744. // the scale (1/kMaxScaleRatioDuringPinch).
  745. SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f);
  746. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  747. EXPECT_FLOAT_EQ(
  748. 0.24f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  749. EXPECT_FLOAT_EQ(
  750. 0.12f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  751. EXPECT_FLOAT_EQ(
  752. 0.06f, active_layer()->tilings()->tiling_at(2)->contents_scale_key());
  753. // Ensure UpdateTiles won't remove any tilings.
  754. active_layer()->MarkAllTilingsUsed();
  755. // Zoom out further, close to our low-res scale factor. We should
  756. // use that tiling as high-res, and not create a new tiling.
  757. SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f);
  758. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  759. // Zoom in. 0.22(desired_scale) should be snapped to 0.24 during zoom-in
  760. // because 0.22(desired_scale) is within the ratio(1.2).
  761. SetContentsScaleOnBothLayers(0.22f, 1.0f, 0.22f);
  762. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  763. EXPECT_FLOAT_EQ(
  764. 0.24f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  765. // Zoom in a lot. Since we move in factors of two, we should get a scale that
  766. // is a power of 2 times 0.24.
  767. SetContentsScaleOnBothLayers(1.f, 1.0f, 1.f);
  768. ASSERT_EQ(4u, active_layer()->tilings()->num_tilings());
  769. EXPECT_FLOAT_EQ(
  770. 1.92f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  771. }
  772. TEST_F(LegacySWPictureLayerImplTest, CleanUpTilings) {
  773. gfx::Size layer_bounds(1300, 1900);
  774. std::vector<PictureLayerTiling*> used_tilings;
  775. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  776. EXPECT_LT(low_res_factor, 1.f);
  777. float scale = 1.f;
  778. float page_scale = 1.f;
  779. SetupDefaultTrees(layer_bounds);
  780. SetWillChangeTransform(active_layer(), true);
  781. EXPECT_FLOAT_EQ(2u, active_layer()->tilings()->num_tilings());
  782. EXPECT_FLOAT_EQ(
  783. 1.f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  784. EXPECT_FLOAT_EQ(
  785. 1.f * low_res_factor,
  786. active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  787. // Ensure UpdateTiles won't remove any tilings. Note this is unrelated to
  788. // |used_tilings| variable, and it's here only to ensure that active_layer()
  789. // won't remove tilings before the test has a chance to verify behavior.
  790. active_layer()->MarkAllTilingsUsed();
  791. // We only have ideal tilings, so they aren't removed.
  792. used_tilings.clear();
  793. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  794. EXPECT_FLOAT_EQ(2u, active_layer()->tilings()->num_tilings());
  795. EXPECT_FLOAT_EQ(
  796. 1.f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  797. EXPECT_FLOAT_EQ(
  798. 1.f * low_res_factor,
  799. active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  800. // Since this test simulates a pinch it needs an input handler.
  801. // TODO(bokan): This is a raster unit test, it shouldn't be using a real
  802. // input handler.
  803. InputHandler::Create(static_cast<CompositorDelegateForInput&>(*host_impl()));
  804. host_impl()->GetInputHandler().PinchGestureBegin(
  805. gfx::Point(), ui::ScrollInputType::kTouchscreen);
  806. // Changing the ideal but not creating new tilings.
  807. scale = 1.5f;
  808. page_scale = 1.5f;
  809. SetContentsScaleOnBothLayers(scale, 1.f, page_scale);
  810. EXPECT_FLOAT_EQ(2u, active_layer()->tilings()->num_tilings());
  811. EXPECT_FLOAT_EQ(
  812. 1.f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  813. EXPECT_FLOAT_EQ(
  814. 1.f * low_res_factor,
  815. active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  816. // The tilings are still our target scale, so they aren't removed.
  817. used_tilings.clear();
  818. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  819. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  820. EXPECT_FLOAT_EQ(
  821. 1.f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  822. EXPECT_FLOAT_EQ(
  823. 1.f * low_res_factor,
  824. active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  825. host_impl()->GetInputHandler().PinchGestureEnd(gfx::Point());
  826. // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
  827. scale = 1.2f;
  828. page_scale = 1.2f;
  829. SetContentsScaleOnBothLayers(1.2f, 1.f, page_scale);
  830. ASSERT_EQ(4u, active_layer()->tilings()->num_tilings());
  831. EXPECT_FLOAT_EQ(
  832. 1.2f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  833. EXPECT_FLOAT_EQ(
  834. 1.f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  835. EXPECT_FLOAT_EQ(
  836. 1.2f * low_res_factor,
  837. active_layer()->tilings()->tiling_at(2)->contents_scale_key());
  838. EXPECT_FLOAT_EQ(
  839. 1.f * low_res_factor,
  840. active_layer()->tilings()->tiling_at(3)->contents_scale_key());
  841. // Ensure UpdateTiles won't remove any tilings.
  842. active_layer()->MarkAllTilingsUsed();
  843. // Mark the non-ideal tilings as used. They won't be removed.
  844. used_tilings.clear();
  845. used_tilings.push_back(active_layer()->tilings()->tiling_at(1));
  846. used_tilings.push_back(active_layer()->tilings()->tiling_at(3));
  847. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  848. ASSERT_EQ(4u, active_layer()->tilings()->num_tilings());
  849. EXPECT_FLOAT_EQ(
  850. 1.2f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  851. EXPECT_FLOAT_EQ(
  852. 1.f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  853. EXPECT_FLOAT_EQ(
  854. 1.2f * low_res_factor,
  855. active_layer()->tilings()->tiling_at(2)->contents_scale_key());
  856. EXPECT_FLOAT_EQ(
  857. 1.f * low_res_factor,
  858. active_layer()->tilings()->tiling_at(3)->contents_scale_key());
  859. // Now move the ideal scale to 0.5. Our target stays 1.2.
  860. SetContentsScaleOnBothLayers(0.5f, 1.f, page_scale);
  861. // The high resolution tiling is between target and ideal, so is not
  862. // removed. The low res tiling for the old ideal=1.0 scale is removed.
  863. used_tilings.clear();
  864. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  865. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  866. EXPECT_FLOAT_EQ(
  867. 1.2f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  868. EXPECT_FLOAT_EQ(
  869. 1.f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  870. EXPECT_FLOAT_EQ(
  871. 1.2f * low_res_factor,
  872. active_layer()->tilings()->tiling_at(2)->contents_scale_key());
  873. // Now move the ideal scale to 1.0. Our target stays 1.2.
  874. SetContentsScaleOnBothLayers(1.f, 1.f, page_scale);
  875. // All the tilings are between are target and the ideal, so they are not
  876. // removed.
  877. used_tilings.clear();
  878. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  879. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  880. EXPECT_FLOAT_EQ(
  881. 1.2f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  882. EXPECT_FLOAT_EQ(
  883. 1.f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  884. EXPECT_FLOAT_EQ(
  885. 1.2f * low_res_factor,
  886. active_layer()->tilings()->tiling_at(2)->contents_scale_key());
  887. // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
  888. SetupDrawPropertiesAndUpdateTiles(active_layer(), 1.1f, 1.f, page_scale);
  889. // Because the pending layer's ideal scale is still 1.0, our tilings fall
  890. // in the range [1.0,1.2] and are kept.
  891. used_tilings.clear();
  892. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  893. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  894. EXPECT_FLOAT_EQ(
  895. 1.2f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  896. EXPECT_FLOAT_EQ(
  897. 1.f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  898. EXPECT_FLOAT_EQ(
  899. 1.2f * low_res_factor,
  900. active_layer()->tilings()->tiling_at(2)->contents_scale_key());
  901. // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
  902. // 1.2 still.
  903. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.1f, 1.f, page_scale);
  904. // Our 1.0 tiling now falls outside the range between our ideal scale and our
  905. // target raster scale. But it is in our used tilings set, so nothing is
  906. // deleted.
  907. used_tilings.clear();
  908. used_tilings.push_back(active_layer()->tilings()->tiling_at(1));
  909. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  910. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  911. EXPECT_FLOAT_EQ(
  912. 1.2f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  913. EXPECT_FLOAT_EQ(
  914. 1.f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  915. EXPECT_FLOAT_EQ(
  916. 1.2f * low_res_factor,
  917. active_layer()->tilings()->tiling_at(2)->contents_scale_key());
  918. // If we remove it from our used tilings set, it is outside the range to keep
  919. // so it is deleted.
  920. used_tilings.clear();
  921. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  922. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  923. EXPECT_FLOAT_EQ(
  924. 1.2f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  925. EXPECT_FLOAT_EQ(
  926. 1.2 * low_res_factor,
  927. active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  928. }
  929. TEST_F(LegacySWPictureLayerImplTest, DontAddLowResDuringAnimation) {
  930. // Make sure this layer covers multiple tiles, since otherwise low
  931. // res won't get created because it is too small.
  932. gfx::Size tile_size(host_impl()->settings().default_tile_size);
  933. // Avoid max untiled layer size heuristics via fixed tile size.
  934. gfx::Size layer_bounds(tile_size.width() + 1, tile_size.height() + 1);
  935. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
  936. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  937. float contents_scale = 1.f;
  938. float device_scale = 1.f;
  939. float page_scale = 1.f;
  940. float maximum_animation_scale = 1.f;
  941. bool affected_by_invalid_scale = false;
  942. ResetTilingsAndRasterScales();
  943. // Animating, so don't create low res even if there isn't one already.
  944. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  945. page_scale, maximum_animation_scale,
  946. affected_by_invalid_scale);
  947. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  948. EXPECT_BOTH_EQ(num_tilings(), 1u);
  949. // Stop animating, low res gets created.
  950. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  951. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  952. EXPECT_EQ(active_layer()->LowResTiling()->contents_scale_key(),
  953. low_res_factor);
  954. EXPECT_EQ(active_layer()->num_tilings(), 2u);
  955. EXPECT_EQ(pending_layer()->num_tilings(), 1u);
  956. // Ensure UpdateTiles won't remove any tilings.
  957. active_layer()->MarkAllTilingsUsed();
  958. // Page scale animation, new high res, but no low res. We still have
  959. // a tiling at the previous scale, it's just not marked as low res on the
  960. // active layer. The pending layer drops non-ideal tilings.
  961. contents_scale = 2.f;
  962. page_scale = 2.f;
  963. maximum_animation_scale = 2.f;
  964. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  965. page_scale, maximum_animation_scale,
  966. affected_by_invalid_scale);
  967. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  968. EXPECT_FALSE(active_layer()->LowResTiling());
  969. EXPECT_FALSE(pending_layer()->LowResTiling());
  970. EXPECT_EQ(3u, active_layer()->num_tilings());
  971. EXPECT_EQ(1u, pending_layer()->num_tilings());
  972. // Stop animating, new low res gets created for final page scale.
  973. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  974. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  975. EXPECT_EQ(active_layer()->LowResTiling()->contents_scale_key(),
  976. 2.f * low_res_factor);
  977. EXPECT_EQ(4u, active_layer()->num_tilings());
  978. EXPECT_EQ(1u, pending_layer()->num_tilings());
  979. }
  980. TEST_F(LegacySWPictureLayerImplTest, DontAddLowResForSmallLayers) {
  981. gfx::Size layer_bounds(host_impl()->settings().default_tile_size);
  982. scoped_refptr<FakeRasterSource> pending_raster_source =
  983. FakeRasterSource::CreateFilled(layer_bounds);
  984. scoped_refptr<FakeRasterSource> active_raster_source =
  985. FakeRasterSource::CreateFilled(layer_bounds);
  986. SetupTrees(pending_raster_source, active_raster_source);
  987. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  988. float device_scale = 1.f;
  989. float page_scale = 1.f;
  990. // Contents exactly fit on one tile at scale 1, no low res.
  991. float contents_scale = 1.f;
  992. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  993. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), contents_scale);
  994. EXPECT_BOTH_EQ(num_tilings(), 1u);
  995. ResetTilingsAndRasterScales();
  996. // Contents that are smaller than one tile, no low res.
  997. contents_scale = 0.123f;
  998. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  999. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), contents_scale);
  1000. EXPECT_BOTH_EQ(num_tilings(), 1u);
  1001. // TODO(danakj): Remove these when raster scale doesn't get fixed?
  1002. ResetTilingsAndRasterScales();
  1003. // Any content bounds that would create more than one tile will
  1004. // generate a low res tiling.
  1005. contents_scale = 2.5f;
  1006. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  1007. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), contents_scale);
  1008. EXPECT_EQ(active_layer()->LowResTiling()->contents_scale_key(),
  1009. contents_scale * low_res_factor);
  1010. EXPECT_FALSE(pending_layer()->LowResTiling());
  1011. EXPECT_EQ(active_layer()->num_tilings(), 2u);
  1012. EXPECT_EQ(pending_layer()->num_tilings(), 1u);
  1013. // Mask layers dont create low res since they always fit on one tile.
  1014. CreateEffectNode(pending_layer());
  1015. auto* mask = AddLayer<FakePictureLayerImpl>(host_impl()->pending_tree(),
  1016. pending_raster_source);
  1017. SetupMaskProperties(pending_layer(), mask);
  1018. UpdateDrawProperties(host_impl()->pending_tree());
  1019. // We did an UpdateDrawProperties above, which will set a contents scale on
  1020. // the mask layer, so allow us to reset the contents scale.
  1021. mask->ReleaseTileResources();
  1022. mask->RecreateTileResources();
  1023. SetupDrawPropertiesAndUpdateTiles(mask, contents_scale, device_scale,
  1024. page_scale);
  1025. EXPECT_EQ(mask->HighResTiling()->contents_scale_key(), contents_scale);
  1026. EXPECT_EQ(mask->num_tilings(), 1u);
  1027. }
  1028. TEST_F(LegacySWPictureLayerImplTest, HugeBackdropFilterMasksGetScaledDown) {
  1029. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1030. gfx::Size layer_bounds(1000, 1000);
  1031. scoped_refptr<FakeRasterSource> valid_raster_source =
  1032. FakeRasterSource::CreateFilled(layer_bounds);
  1033. SetupPendingTree(valid_raster_source);
  1034. CreateEffectNode(pending_layer())
  1035. .backdrop_filters.Append(FilterOperation::CreateInvertFilter(1.0));
  1036. auto* pending_mask = AddLayer<FakePictureLayerImpl>(
  1037. host_impl()->pending_tree(), valid_raster_source);
  1038. SetupMaskProperties(pending_layer(), pending_mask);
  1039. ASSERT_TRUE(pending_mask->is_backdrop_filter_mask());
  1040. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1041. UpdateDrawProperties(host_impl()->pending_tree());
  1042. EXPECT_EQ(1.f, pending_mask->HighResTiling()->contents_scale_key());
  1043. EXPECT_EQ(1u, pending_mask->num_tilings());
  1044. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  1045. pending_mask->HighResTiling()->AllTilesForTesting());
  1046. ActivateTree();
  1047. FakePictureLayerImpl* active_mask = static_cast<FakePictureLayerImpl*>(
  1048. host_impl()->active_tree()->LayerById(pending_mask->id()));
  1049. // Mask layers have a tiling with a single tile in it.
  1050. EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
  1051. // The mask resource exists.
  1052. viz::ResourceId mask_resource_id;
  1053. gfx::Size mask_texture_size;
  1054. gfx::SizeF mask_uv_size;
  1055. active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size,
  1056. &mask_uv_size);
  1057. EXPECT_NE(viz::kInvalidResourceId, mask_resource_id);
  1058. EXPECT_EQ(active_mask->bounds(), mask_texture_size);
  1059. EXPECT_EQ(gfx::SizeF(1.0f, 1.0f), mask_uv_size);
  1060. // Drop resources and recreate them, still the same.
  1061. pending_mask->ReleaseTileResources();
  1062. active_mask->ReleaseTileResources();
  1063. pending_mask->RecreateTileResources();
  1064. active_mask->RecreateTileResources();
  1065. SetupDrawPropertiesAndUpdateTiles(active_mask, 1.f, 1.f, 1.f);
  1066. active_mask->HighResTiling()->CreateAllTilesForTesting();
  1067. EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
  1068. // Resize larger than the max texture size.
  1069. int max_texture_size = host_impl()->max_texture_size();
  1070. gfx::Size huge_bounds(max_texture_size + 1, 10);
  1071. scoped_refptr<FakeRasterSource> huge_raster_source =
  1072. FakeRasterSource::CreateFilled(huge_bounds);
  1073. SetupPendingTree(huge_raster_source);
  1074. pending_mask->SetBounds(huge_bounds);
  1075. pending_mask->SetRasterSource(huge_raster_source, Region());
  1076. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1077. UpdateDrawProperties(host_impl()->pending_tree());
  1078. // The mask tiling gets scaled down.
  1079. EXPECT_LT(pending_mask->HighResTiling()->contents_scale_key(), 1.f);
  1080. EXPECT_EQ(1u, pending_mask->num_tilings());
  1081. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  1082. pending_mask->HighResTiling()->AllTilesForTesting());
  1083. ActivateTree();
  1084. // Mask layers have a tiling with a single tile in it.
  1085. EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
  1086. // The mask resource exists.
  1087. active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size,
  1088. &mask_uv_size);
  1089. EXPECT_NE(viz::kInvalidResourceId, mask_resource_id);
  1090. gfx::Size expected_size = active_mask->bounds();
  1091. expected_size.SetToMin(gfx::Size(max_texture_size, max_texture_size));
  1092. EXPECT_EQ(expected_size, mask_texture_size);
  1093. EXPECT_EQ(gfx::SizeF(1.0f, 1.0f), mask_uv_size);
  1094. // Drop resources and recreate them, still the same.
  1095. pending_mask->ReleaseTileResources();
  1096. active_mask->ReleaseTileResources();
  1097. pending_mask->RecreateTileResources();
  1098. active_mask->RecreateTileResources();
  1099. SetupDrawPropertiesAndUpdateTiles(active_mask, 1.f, 1.f, 1.f);
  1100. active_mask->HighResTiling()->CreateAllTilesForTesting();
  1101. EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
  1102. EXPECT_NE(viz::kInvalidResourceId, mask_resource_id);
  1103. EXPECT_EQ(expected_size, mask_texture_size);
  1104. // Do another activate, the same holds.
  1105. SetupPendingTree(huge_raster_source);
  1106. ActivateTree();
  1107. EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
  1108. active_layer()->GetContentsResourceId(&mask_resource_id, &mask_texture_size,
  1109. &mask_uv_size);
  1110. EXPECT_EQ(expected_size, mask_texture_size);
  1111. EXPECT_EQ(viz::kInvalidResourceId, mask_resource_id);
  1112. EXPECT_EQ(gfx::SizeF(1.0f, 1.0f), mask_uv_size);
  1113. // Resize even larger, so that the scale would be smaller than the minimum
  1114. // contents scale. Then the layer should no longer have any tiling.
  1115. gfx::Size extra_huge_bounds(max_texture_size * 10 + 1, 10);
  1116. scoped_refptr<FakeRasterSource> extra_huge_raster_source =
  1117. FakeRasterSource::CreateFilled(extra_huge_bounds);
  1118. SetupPendingTree(extra_huge_raster_source);
  1119. pending_mask->SetBounds(extra_huge_bounds);
  1120. pending_mask->SetRasterSource(extra_huge_raster_source, Region());
  1121. EXPECT_FALSE(pending_mask->CanHaveTilings());
  1122. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1123. UpdateDrawProperties(host_impl()->pending_tree());
  1124. EXPECT_EQ(0u, pending_mask->num_tilings());
  1125. }
  1126. TEST_F(LegacySWPictureLayerImplTest, ScaledBackdropFilterMaskLayer) {
  1127. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1128. gfx::Size layer_bounds(1000, 1000);
  1129. SetInitialDeviceScaleFactor(1.3f);
  1130. scoped_refptr<FakeRasterSource> valid_raster_source =
  1131. FakeRasterSource::CreateFilled(layer_bounds);
  1132. SetupPendingTree(valid_raster_source);
  1133. CreateEffectNode(pending_layer())
  1134. .backdrop_filters.Append(FilterOperation::CreateInvertFilter(1.0));
  1135. auto* pending_mask = AddLayer<FakePictureLayerImpl>(
  1136. host_impl()->pending_tree(), valid_raster_source);
  1137. SetupMaskProperties(pending_layer(), pending_mask);
  1138. ASSERT_TRUE(pending_mask->is_backdrop_filter_mask());
  1139. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1140. UpdateDrawProperties(host_impl()->pending_tree());
  1141. // Masks are scaled, and do not have a low res tiling.
  1142. EXPECT_EQ(1.3f, pending_mask->HighResTiling()->contents_scale_key());
  1143. EXPECT_EQ(1u, pending_mask->num_tilings());
  1144. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  1145. pending_mask->HighResTiling()->AllTilesForTesting());
  1146. ActivateTree();
  1147. FakePictureLayerImpl* active_mask = static_cast<FakePictureLayerImpl*>(
  1148. host_impl()->active_tree()->LayerById(pending_mask->id()));
  1149. // Mask layers have a tiling with a single tile in it.
  1150. EXPECT_EQ(1u, active_mask->HighResTiling()->AllTilesForTesting().size());
  1151. // The mask resource exists.
  1152. viz::ResourceId mask_resource_id;
  1153. gfx::Size mask_texture_size;
  1154. gfx::SizeF mask_uv_size;
  1155. active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size,
  1156. &mask_uv_size);
  1157. EXPECT_NE(viz::kInvalidResourceId, mask_resource_id);
  1158. gfx::Size expected_mask_texture_size =
  1159. gfx::ScaleToCeiledSize(active_mask->bounds(), 1.3f);
  1160. EXPECT_EQ(mask_texture_size, expected_mask_texture_size);
  1161. EXPECT_EQ(gfx::SizeF(1.0f, 1.0f), mask_uv_size);
  1162. }
  1163. TEST_F(LegacySWPictureLayerImplTest, ScaledMaskLayer) {
  1164. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1165. gfx::Size layer_bounds(1000, 1000);
  1166. SetInitialDeviceScaleFactor(1.3f);
  1167. scoped_refptr<FakeRasterSource> valid_raster_source =
  1168. FakeRasterSource::CreateFilled(layer_bounds);
  1169. SetupPendingTree(valid_raster_source);
  1170. CreateEffectNode(pending_layer());
  1171. auto* pending_mask = AddLayer<FakePictureLayerImpl>(
  1172. host_impl()->pending_tree(), valid_raster_source);
  1173. SetupMaskProperties(pending_layer(), pending_mask);
  1174. ASSERT_FALSE(pending_mask->is_backdrop_filter_mask());
  1175. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1176. UpdateDrawProperties(host_impl()->pending_tree());
  1177. // Masks are scaled, and do not have a low res tiling.
  1178. EXPECT_EQ(1.3f, pending_mask->HighResTiling()->contents_scale_key());
  1179. EXPECT_EQ(1u, pending_mask->num_tilings());
  1180. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  1181. pending_mask->HighResTiling()->AllTilesForTesting());
  1182. ActivateTree();
  1183. FakePictureLayerImpl* active_mask = static_cast<FakePictureLayerImpl*>(
  1184. host_impl()->active_tree()->LayerById(pending_mask->id()));
  1185. // Non-backdrop-filter mask layers are tiled normally.
  1186. EXPECT_EQ(36u, active_mask->HighResTiling()->AllTilesForTesting().size());
  1187. // And don't have mask resources.
  1188. viz::ResourceId mask_resource_id;
  1189. gfx::Size mask_texture_size;
  1190. gfx::SizeF mask_uv_size;
  1191. active_mask->GetContentsResourceId(&mask_resource_id, &mask_texture_size,
  1192. &mask_uv_size);
  1193. EXPECT_EQ(viz::kInvalidResourceId, mask_resource_id);
  1194. EXPECT_EQ(gfx::Size(), mask_texture_size);
  1195. EXPECT_EQ(gfx::SizeF(), mask_uv_size);
  1196. }
  1197. TEST_F(LegacySWPictureLayerImplTest, ReleaseTileResources) {
  1198. gfx::Size layer_bounds(1300, 1900);
  1199. SetupDefaultTrees(layer_bounds);
  1200. EXPECT_EQ(1u, pending_layer()->tilings()->num_tilings());
  1201. // All tilings should be removed when losing output surface.
  1202. active_layer()->ReleaseTileResources();
  1203. active_layer()->RecreateTileResources();
  1204. EXPECT_EQ(0u, active_layer()->num_tilings());
  1205. pending_layer()->ReleaseTileResources();
  1206. pending_layer()->RecreateTileResources();
  1207. EXPECT_EQ(0u, pending_layer()->num_tilings());
  1208. // This should create new tilings.
  1209. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  1210. 1.f, // ideal contents scale
  1211. 1.f, // device scale
  1212. 1.f); // page scale
  1213. EXPECT_EQ(1u, pending_layer()->tilings()->num_tilings());
  1214. }
  1215. // ReleaseResources should behave identically to ReleaseTileResources.
  1216. TEST_F(LegacySWPictureLayerImplTest, ReleaseResources) {
  1217. gfx::Size layer_bounds(1300, 1900);
  1218. SetupDefaultTrees(layer_bounds);
  1219. EXPECT_EQ(1u, pending_layer()->tilings()->num_tilings());
  1220. // All tilings should be removed when losing output surface.
  1221. active_layer()->ReleaseResources();
  1222. EXPECT_TRUE(active_layer()->tilings());
  1223. EXPECT_EQ(0u, active_layer()->num_tilings());
  1224. active_layer()->RecreateTileResources();
  1225. EXPECT_EQ(0u, active_layer()->num_tilings());
  1226. pending_layer()->ReleaseResources();
  1227. EXPECT_TRUE(pending_layer()->tilings());
  1228. EXPECT_EQ(0u, pending_layer()->num_tilings());
  1229. pending_layer()->RecreateTileResources();
  1230. EXPECT_EQ(0u, pending_layer()->num_tilings());
  1231. }
  1232. TEST_F(LegacySWPictureLayerImplTest, ClampTilesToMaxTileSize) {
  1233. gfx::Size layer_bounds(5000, 5000);
  1234. scoped_refptr<FakeRasterSource> pending_raster_source =
  1235. FakeRasterSource::CreateFilled(layer_bounds);
  1236. SetupPendingTree(pending_raster_source);
  1237. EXPECT_GE(pending_layer()->tilings()->num_tilings(), 1u);
  1238. pending_layer()->tilings()->tiling_at(0)->CreateAllTilesForTesting();
  1239. // The default value.
  1240. EXPECT_EQ(gfx::Size(256, 256).ToString(),
  1241. host_impl()->settings().default_tile_size.ToString());
  1242. Tile* tile =
  1243. pending_layer()->tilings()->tiling_at(0)->AllTilesForTesting()[0];
  1244. EXPECT_EQ(gfx::Size(256, 256).ToString(),
  1245. tile->content_rect().size().ToString());
  1246. ResetTilingsAndRasterScales();
  1247. // Change the max texture size on the output surface context.
  1248. auto gl_owned = std::make_unique<viz::TestGLES2Interface>();
  1249. gl_owned->set_max_texture_size(140);
  1250. ResetLayerTreeFrameSink(
  1251. FakeLayerTreeFrameSink::Create3d(std::move(gl_owned)));
  1252. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.f, 1.f, 1.f);
  1253. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  1254. pending_layer()->tilings()->tiling_at(0)->CreateAllTilesForTesting();
  1255. // Verify the tiles are not larger than the context's max texture size.
  1256. tile = pending_layer()->tilings()->tiling_at(0)->AllTilesForTesting()[0];
  1257. EXPECT_GE(140, tile->content_rect().width());
  1258. EXPECT_GE(140, tile->content_rect().height());
  1259. }
  1260. TEST_F(LegacySWPictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
  1261. gfx::Size layer_bounds(500, 500);
  1262. SetupDefaultTrees(layer_bounds);
  1263. EXPECT_GE(active_layer()->tilings()->num_tilings(), 1u);
  1264. active_layer()->tilings()->tiling_at(0)->CreateAllTilesForTesting();
  1265. // The default value. The layer is smaller than this.
  1266. EXPECT_EQ(gfx::Size(512, 512).ToString(),
  1267. host_impl()->settings().max_untiled_layer_size.ToString());
  1268. // There should be a single tile since the layer is small.
  1269. PictureLayerTiling* high_res_tiling = active_layer()->tilings()->tiling_at(0);
  1270. EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
  1271. ResetTilingsAndRasterScales();
  1272. // Change the max texture size on the output surface context.
  1273. auto gl_owned = std::make_unique<viz::TestGLES2Interface>();
  1274. gl_owned->set_max_texture_size(140);
  1275. ResetLayerTreeFrameSink(
  1276. FakeLayerTreeFrameSink::Create3d(std::move(gl_owned)));
  1277. SetupDrawPropertiesAndUpdateTiles(active_layer(), 1.f, 1.f, 1.f);
  1278. ASSERT_LE(1u, active_layer()->tilings()->num_tilings());
  1279. active_layer()->tilings()->tiling_at(0)->CreateAllTilesForTesting();
  1280. // There should be more than one tile since the max texture size won't cover
  1281. // the layer.
  1282. high_res_tiling = active_layer()->tilings()->tiling_at(0);
  1283. EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
  1284. // Verify the tiles are not larger than the context's max texture size.
  1285. Tile* tile = active_layer()->tilings()->tiling_at(0)->AllTilesForTesting()[0];
  1286. EXPECT_GE(140, tile->content_rect().width());
  1287. EXPECT_GE(140, tile->content_rect().height());
  1288. }
  1289. TEST_F(LegacySWPictureLayerImplTest, DisallowTileDrawQuads) {
  1290. auto render_pass = viz::CompositorRenderPass::Create();
  1291. gfx::Size layer_bounds(1300, 1900);
  1292. gfx::Rect layer_rect(layer_bounds);
  1293. gfx::Rect layer_invalidation(150, 200, 30, 180);
  1294. SetupDefaultTreesWithInvalidation(layer_bounds, layer_invalidation);
  1295. active_layer()->SetContentsOpaque(true);
  1296. active_layer()->draw_properties().visible_layer_rect =
  1297. gfx::Rect(layer_bounds);
  1298. AppendQuadsData data;
  1299. active_layer()->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, nullptr);
  1300. active_layer()->AppendQuads(render_pass.get(), &data);
  1301. active_layer()->DidDraw(nullptr);
  1302. ASSERT_EQ(1u, render_pass->quad_list.size());
  1303. EXPECT_EQ(viz::DrawQuad::Material::kPictureContent,
  1304. render_pass->quad_list.front()->material);
  1305. EXPECT_EQ(render_pass->quad_list.front()->rect, layer_rect);
  1306. EXPECT_FALSE(render_pass->quad_list.front()->needs_blending);
  1307. EXPECT_TRUE(
  1308. render_pass->quad_list.front()->shared_quad_state->are_contents_opaque);
  1309. EXPECT_EQ(render_pass->quad_list.front()->visible_rect, layer_rect);
  1310. }
  1311. TEST_F(LegacySWPictureLayerImplTest, ResourcelessPartialRecording) {
  1312. auto render_pass = viz::CompositorRenderPass::Create();
  1313. gfx::Size layer_bounds(700, 650);
  1314. gfx::Rect layer_rect(layer_bounds);
  1315. SetInitialDeviceScaleFactor(2.f);
  1316. gfx::Rect recorded_viewport(20, 30, 40, 50);
  1317. scoped_refptr<FakeRasterSource> active_raster_source =
  1318. FakeRasterSource::CreatePartiallyFilled(layer_bounds, recorded_viewport);
  1319. SetupPendingTree(active_raster_source);
  1320. ActivateTree();
  1321. active_layer()->SetContentsOpaque(true);
  1322. gfx::Rect visible_rect(30, 35, 10, 5);
  1323. active_layer()->draw_properties().visible_layer_rect = visible_rect;
  1324. AppendQuadsData data;
  1325. active_layer()->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, nullptr);
  1326. active_layer()->AppendQuads(render_pass.get(), &data);
  1327. active_layer()->DidDraw(nullptr);
  1328. gfx::Rect scaled_visible = gfx::ScaleToEnclosingRect(visible_rect, 2.f);
  1329. gfx::Rect scaled_recorded = gfx::ScaleToEnclosingRect(recorded_viewport, 2.f);
  1330. gfx::Rect quad_visible = gfx::IntersectRects(scaled_visible, scaled_recorded);
  1331. ASSERT_EQ(1U, render_pass->quad_list.size());
  1332. EXPECT_EQ(viz::DrawQuad::Material::kPictureContent,
  1333. render_pass->quad_list.front()->material);
  1334. const viz::DrawQuad* quad = render_pass->quad_list.front();
  1335. EXPECT_EQ(quad_visible, quad->rect);
  1336. EXPECT_TRUE(quad->shared_quad_state->are_contents_opaque);
  1337. EXPECT_EQ(quad_visible, quad->visible_rect);
  1338. EXPECT_FALSE(quad->needs_blending);
  1339. }
  1340. TEST_F(LegacySWPictureLayerImplTest, ResourcelessEmptyRecording) {
  1341. auto render_pass = viz::CompositorRenderPass::Create();
  1342. gfx::Size layer_bounds(700, 650);
  1343. scoped_refptr<FakeRasterSource> active_raster_source =
  1344. FakeRasterSource::CreatePartiallyFilled(layer_bounds, gfx::Rect());
  1345. SetupPendingTree(active_raster_source);
  1346. ActivateTree();
  1347. active_layer()->SetContentsOpaque(true);
  1348. active_layer()->draw_properties().visible_layer_rect =
  1349. gfx::Rect(layer_bounds);
  1350. AppendQuadsData data;
  1351. active_layer()->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, nullptr);
  1352. active_layer()->AppendQuads(render_pass.get(), &data);
  1353. active_layer()->DidDraw(nullptr);
  1354. EXPECT_EQ(0U, render_pass->quad_list.size());
  1355. }
  1356. TEST_F(LegacySWPictureLayerImplTest, FarScrolledQuadsShifted) {
  1357. auto render_pass = viz::CompositorRenderPass::Create();
  1358. gfx::Size layer_bounds(1000, 10000);
  1359. scoped_refptr<FakeRasterSource> active_raster_source =
  1360. FakeRasterSource::CreateFilled(layer_bounds);
  1361. SetupPendingTree(active_raster_source);
  1362. ActivateTree();
  1363. active_layer()->SetContentsOpaque(true);
  1364. active_layer()->draw_properties().visible_layer_rect =
  1365. gfx::Rect(0, 5000, 1000, 1000);
  1366. active_layer()->UpdateTiles();
  1367. auto* high_res_tiling = active_layer()->HighResTiling();
  1368. ASSERT_TRUE(high_res_tiling);
  1369. const std::vector<Tile*>& tiles = high_res_tiling->AllTilesForTesting();
  1370. ASSERT_GT(tiles.size(), 0u);
  1371. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
  1372. AppendQuadsData data;
  1373. active_layer()->WillDraw(DRAW_MODE_HARDWARE, nullptr);
  1374. active_layer()->AppendQuads(render_pass.get(), &data);
  1375. active_layer()->DidDraw(nullptr);
  1376. EXPECT_EQ(20u, render_pass->quad_list.size());
  1377. int last_y = -1;
  1378. int last_height = -1;
  1379. int min_y = std::numeric_limits<int>::max();
  1380. float min_transformed_y = std::numeric_limits<float>::max();
  1381. float max_transformed_y = -1;
  1382. for (auto* draw_quad : render_pass->quad_list) {
  1383. if (last_y == -1) {
  1384. last_y = draw_quad->rect.y();
  1385. min_y = last_y;
  1386. last_height = draw_quad->rect.height();
  1387. }
  1388. if (last_y != draw_quad->rect.y()) {
  1389. EXPECT_EQ(last_y + last_height, draw_quad->rect.y());
  1390. last_y = draw_quad->rect.y();
  1391. min_y = std::min(min_y, last_y);
  1392. last_height = draw_quad->rect.height();
  1393. }
  1394. EXPECT_LT(last_y, 5000);
  1395. EXPECT_EQ(draw_quad->material, viz::DrawQuad::Material::kTiledContent);
  1396. auto transform = [draw_quad](const gfx::Rect& rect) {
  1397. gfx::RectF result(rect);
  1398. draw_quad->shared_quad_state->quad_to_target_transform.TransformRect(
  1399. &result);
  1400. return result;
  1401. };
  1402. gfx::RectF transformed_rect = transform(draw_quad->rect);
  1403. EXPECT_GT(transformed_rect.y(), 0);
  1404. if (min_transformed_y < 0 || transformed_rect.y() < min_transformed_y)
  1405. min_transformed_y = transformed_rect.y();
  1406. if (transformed_rect.bottom() > max_transformed_y)
  1407. max_transformed_y = transformed_rect.bottom();
  1408. gfx::RectF transformed_quad_layer_rect =
  1409. transform(draw_quad->shared_quad_state->quad_layer_rect);
  1410. EXPECT_RECTF_EQ(transformed_quad_layer_rect,
  1411. gfx::RectF(0.f, 0.f, 1000.f, 10000.f));
  1412. gfx::RectF transformed_visible_quad_layer_rect =
  1413. transform(draw_quad->shared_quad_state->visible_quad_layer_rect);
  1414. EXPECT_RECTF_EQ(transformed_visible_quad_layer_rect,
  1415. gfx::RectF(0.f, 5000.f, 1000.f, 1000.f));
  1416. }
  1417. EXPECT_EQ(min_y, 0);
  1418. EXPECT_FLOAT_EQ(min_transformed_y, 5000.f);
  1419. EXPECT_FLOAT_EQ(max_transformed_y, 6000.f);
  1420. }
  1421. TEST_F(LegacySWPictureLayerImplTest, FarScrolledSolidColorQuadsShifted) {
  1422. auto render_pass = viz::CompositorRenderPass::Create();
  1423. gfx::Size layer_bounds(1000, 10000);
  1424. scoped_refptr<FakeRasterSource> active_raster_source =
  1425. FakeRasterSource::CreateFilled(layer_bounds);
  1426. SetupPendingTree(active_raster_source);
  1427. ActivateTree();
  1428. active_layer()->SetContentsOpaque(true);
  1429. active_layer()->draw_properties().visible_layer_rect =
  1430. gfx::Rect(0, 9000, 1000, 1000);
  1431. active_layer()->UpdateTiles();
  1432. auto* high_res_tiling = active_layer()->HighResTiling();
  1433. ASSERT_TRUE(high_res_tiling);
  1434. const std::vector<Tile*>& tiles = high_res_tiling->AllTilesForTesting();
  1435. ASSERT_GT(tiles.size(), 0u);
  1436. for (auto* tile : tiles)
  1437. tile->draw_info().SetSolidColorForTesting(SkColors::kBlue);
  1438. AppendQuadsData data;
  1439. active_layer()->WillDraw(DRAW_MODE_HARDWARE, nullptr);
  1440. active_layer()->AppendQuads(render_pass.get(), &data);
  1441. active_layer()->DidDraw(nullptr);
  1442. EXPECT_EQ(20u, render_pass->quad_list.size());
  1443. int last_y = -1;
  1444. int last_height = -1;
  1445. int min_y = std::numeric_limits<int>::max();
  1446. float min_transformed_y = std::numeric_limits<float>::max();
  1447. float max_transformed_y = -1;
  1448. for (auto* draw_quad : render_pass->quad_list) {
  1449. if (last_y == -1) {
  1450. last_y = draw_quad->rect.y();
  1451. min_y = last_y;
  1452. last_height = draw_quad->rect.height();
  1453. }
  1454. if (last_y != draw_quad->rect.y()) {
  1455. EXPECT_EQ(last_y + last_height, draw_quad->rect.y());
  1456. last_y = draw_quad->rect.y();
  1457. min_y = std::min(min_y, last_y);
  1458. last_height = draw_quad->rect.height();
  1459. }
  1460. EXPECT_LT(last_y, 5000);
  1461. EXPECT_EQ(draw_quad->material, viz::DrawQuad::Material::kSolidColor);
  1462. auto transform = [draw_quad](const gfx::Rect& rect) {
  1463. gfx::RectF result(rect);
  1464. draw_quad->shared_quad_state->quad_to_target_transform.TransformRect(
  1465. &result);
  1466. return result;
  1467. };
  1468. gfx::RectF transformed_rect = transform(draw_quad->rect);
  1469. EXPECT_GT(transformed_rect.y(), 0);
  1470. if (transformed_rect.y() < min_transformed_y)
  1471. min_transformed_y = transformed_rect.y();
  1472. if (transformed_rect.bottom() > max_transformed_y)
  1473. max_transformed_y = transformed_rect.bottom();
  1474. gfx::RectF transformed_quad_layer_rect =
  1475. transform(draw_quad->shared_quad_state->quad_layer_rect);
  1476. EXPECT_RECTF_EQ(transformed_quad_layer_rect,
  1477. gfx::RectF(0.f, 0.f, 1000.f, 10000.f));
  1478. gfx::RectF transformed_visible_quad_layer_rect =
  1479. transform(draw_quad->shared_quad_state->visible_quad_layer_rect);
  1480. EXPECT_RECTF_EQ(transformed_visible_quad_layer_rect,
  1481. gfx::RectF(0.f, 9000.f, 1000.f, 1000.f));
  1482. }
  1483. EXPECT_EQ(min_y, 0);
  1484. EXPECT_FLOAT_EQ(min_transformed_y, 9000.f);
  1485. EXPECT_FLOAT_EQ(max_transformed_y, 10000.f);
  1486. }
  1487. TEST_F(LegacySWPictureLayerImplTest, SolidColorLayerHasVisibleFullCoverage) {
  1488. auto render_pass = viz::CompositorRenderPass::Create();
  1489. gfx::Size layer_bounds(1500, 1500);
  1490. gfx::Rect visible_rect(250, 250, 1000, 1000);
  1491. scoped_refptr<FakeRasterSource> pending_raster_source =
  1492. FakeRasterSource::CreateFilledSolidColor(layer_bounds);
  1493. scoped_refptr<FakeRasterSource> active_raster_source =
  1494. FakeRasterSource::CreateFilledSolidColor(layer_bounds);
  1495. SetupTrees(pending_raster_source, active_raster_source);
  1496. active_layer()->draw_properties().visible_layer_rect = visible_rect;
  1497. AppendQuadsData data;
  1498. active_layer()->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  1499. active_layer()->AppendQuads(render_pass.get(), &data);
  1500. active_layer()->DidDraw(nullptr);
  1501. Region remaining = visible_rect;
  1502. for (auto* quad : render_pass->quad_list) {
  1503. EXPECT_TRUE(visible_rect.Contains(quad->rect));
  1504. EXPECT_TRUE(remaining.Contains(quad->rect));
  1505. remaining.Subtract(quad->rect);
  1506. }
  1507. EXPECT_TRUE(remaining.IsEmpty());
  1508. }
  1509. TEST_F(LegacySWPictureLayerImplTest, TileScalesWithSolidColorRasterSource) {
  1510. gfx::Size layer_bounds(200, 200);
  1511. scoped_refptr<FakeRasterSource> pending_raster_source =
  1512. FakeRasterSource::CreateFilled(layer_bounds);
  1513. scoped_refptr<FakeRasterSource> active_raster_source =
  1514. FakeRasterSource::CreateFilledSolidColor(layer_bounds);
  1515. SetupTrees(pending_raster_source, active_raster_source);
  1516. // Solid color raster source should not allow tilings at any scale.
  1517. EXPECT_FALSE(active_layer()->CanHaveTilings());
  1518. EXPECT_EQ(0.f, active_layer()->ideal_contents_scale_key());
  1519. // Activate non-solid-color pending raster source makes active layer can have
  1520. // tilings.
  1521. ActivateTree();
  1522. EXPECT_TRUE(active_layer()->CanHaveTilings());
  1523. EXPECT_GT(active_layer()->ideal_contents_scale_key(), 0.f);
  1524. }
  1525. TEST_F(NoLowResPictureLayerImplTest, MarkRequiredOffscreenTiles) {
  1526. gfx::Size layer_bounds(200, 200);
  1527. gfx::Transform transform;
  1528. gfx::Rect viewport(0, 0, 100, 200);
  1529. host_impl()->SetExternalTilePriorityConstraints(viewport, transform);
  1530. scoped_refptr<FakeRasterSource> pending_raster_source =
  1531. FakeRasterSource::CreateFilled(layer_bounds);
  1532. SetupPendingTreeWithFixedTileSize(pending_raster_source, gfx::Size(100, 100),
  1533. Region());
  1534. EXPECT_EQ(1u, pending_layer()->num_tilings());
  1535. EXPECT_EQ(
  1536. viewport,
  1537. pending_layer()->viewport_rect_for_tile_priority_in_content_space());
  1538. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1539. pending_layer()->UpdateTiles();
  1540. int num_visible = 0;
  1541. int num_offscreen = 0;
  1542. std::unique_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
  1543. pending_layer()->picture_layer_tiling_set(), false, false));
  1544. for (; !queue->IsEmpty(); queue->Pop()) {
  1545. const PrioritizedTile& prioritized_tile = queue->Top();
  1546. DCHECK(prioritized_tile.tile());
  1547. if (prioritized_tile.priority().distance_to_visible == 0.f) {
  1548. EXPECT_TRUE(prioritized_tile.tile()->required_for_activation());
  1549. num_visible++;
  1550. } else {
  1551. EXPECT_FALSE(prioritized_tile.tile()->required_for_activation());
  1552. num_offscreen++;
  1553. }
  1554. }
  1555. EXPECT_GT(num_visible, 0);
  1556. EXPECT_GT(num_offscreen, 0);
  1557. }
  1558. TEST_F(NoLowResPictureLayerImplTest,
  1559. TileOutsideOfViewportForTilePriorityNotRequired) {
  1560. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1561. gfx::Size tile_size(100, 100);
  1562. gfx::Size layer_bounds(400, 400);
  1563. gfx::Rect external_viewport_for_tile_priority(400, 200);
  1564. gfx::Rect visible_layer_rect(200, 400);
  1565. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
  1566. ASSERT_EQ(1u, pending_layer()->num_tilings());
  1567. ASSERT_EQ(1.f, pending_layer()->HighResTiling()->contents_scale_key());
  1568. // Set external viewport for tile priority.
  1569. gfx::Transform transform_for_tile_priority;
  1570. host_impl()->SetExternalTilePriorityConstraints(
  1571. external_viewport_for_tile_priority, transform_for_tile_priority);
  1572. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1573. UpdateDrawProperties(host_impl()->pending_tree());
  1574. // Set visible content rect that is different from
  1575. // external_viewport_for_tile_priority.
  1576. pending_layer()->draw_properties().visible_layer_rect = visible_layer_rect;
  1577. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  1578. pending_layer()->UpdateTiles();
  1579. // Intersect the two rects. Any tile outside should not be required for
  1580. // activation.
  1581. gfx::Rect viewport_for_tile_priority =
  1582. pending_layer()->viewport_rect_for_tile_priority_in_content_space();
  1583. viewport_for_tile_priority.Intersect(pending_layer()->visible_layer_rect());
  1584. EXPECT_TRUE(pending_layer()->HighResTiling()->AllTilesForTesting().empty());
  1585. int num_inside = 0;
  1586. int num_outside = 0;
  1587. for (PictureLayerTiling::CoverageIterator iter(
  1588. active_layer()->HighResTiling(), 1.f, gfx::Rect(layer_bounds));
  1589. iter; ++iter) {
  1590. if (!*iter)
  1591. continue;
  1592. Tile* tile = *iter;
  1593. if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
  1594. num_inside++;
  1595. // Mark everything in viewport for tile priority as ready to draw.
  1596. TileDrawInfo& draw_info = tile->draw_info();
  1597. draw_info.SetSolidColorForTesting(SkColors::kRed);
  1598. } else {
  1599. num_outside++;
  1600. EXPECT_FALSE(tile->required_for_activation());
  1601. }
  1602. }
  1603. EXPECT_GT(num_inside, 0);
  1604. EXPECT_GT(num_outside, 0);
  1605. // Activate and draw active layer.
  1606. ActivateTree();
  1607. active_layer()->draw_properties().visible_layer_rect = visible_layer_rect;
  1608. auto render_pass = viz::CompositorRenderPass::Create();
  1609. AppendQuadsData data;
  1610. active_layer()->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  1611. active_layer()->AppendQuads(render_pass.get(), &data);
  1612. active_layer()->DidDraw(nullptr);
  1613. // All tiles in activation rect is ready to draw.
  1614. EXPECT_EQ(0u, data.num_missing_tiles);
  1615. EXPECT_EQ(0u, data.num_incomplete_tiles);
  1616. EXPECT_FALSE(active_layer()->only_used_low_res_last_append_quads());
  1617. }
  1618. TEST_F(LegacySWPictureLayerImplTest, HighResTileIsComplete) {
  1619. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1620. gfx::Size tile_size(100, 100);
  1621. gfx::Size layer_bounds(200, 200);
  1622. scoped_refptr<FakeRasterSource> pending_raster_source =
  1623. FakeRasterSource::CreateFilled(layer_bounds);
  1624. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  1625. ActivateTree();
  1626. // All high res tiles have resources.
  1627. std::vector<Tile*> tiles =
  1628. active_layer()->tilings()->tiling_at(0)->AllTilesForTesting();
  1629. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
  1630. auto render_pass = viz::CompositorRenderPass::Create();
  1631. AppendQuadsData data;
  1632. active_layer()->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  1633. active_layer()->AppendQuads(render_pass.get(), &data);
  1634. active_layer()->DidDraw(nullptr);
  1635. // All high res tiles drew, nothing was incomplete.
  1636. EXPECT_EQ(9u, render_pass->quad_list.size());
  1637. EXPECT_EQ(0u, data.num_missing_tiles);
  1638. EXPECT_EQ(0u, data.num_incomplete_tiles);
  1639. EXPECT_FALSE(active_layer()->only_used_low_res_last_append_quads());
  1640. }
  1641. TEST_F(LegacySWPictureLayerImplTest, HighResTileIsIncomplete) {
  1642. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1643. gfx::Size tile_size(100, 100);
  1644. gfx::Size layer_bounds(200, 200);
  1645. scoped_refptr<FakeRasterSource> pending_raster_source =
  1646. FakeRasterSource::CreateFilled(layer_bounds);
  1647. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  1648. ActivateTree();
  1649. auto render_pass = viz::CompositorRenderPass::Create();
  1650. AppendQuadsData data;
  1651. active_layer()->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  1652. active_layer()->AppendQuads(render_pass.get(), &data);
  1653. active_layer()->DidDraw(nullptr);
  1654. EXPECT_EQ(1u, render_pass->quad_list.size());
  1655. EXPECT_EQ(1u, data.num_missing_tiles);
  1656. EXPECT_EQ(0u, data.num_incomplete_tiles);
  1657. EXPECT_TRUE(active_layer()->only_used_low_res_last_append_quads());
  1658. }
  1659. TEST_F(LegacySWPictureLayerImplTest, HighResTileIsIncompleteLowResComplete) {
  1660. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1661. gfx::Size tile_size(100, 100);
  1662. gfx::Size layer_bounds(200, 200);
  1663. scoped_refptr<FakeRasterSource> pending_raster_source =
  1664. FakeRasterSource::CreateFilled(layer_bounds);
  1665. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  1666. ActivateTree();
  1667. std::vector<Tile*> low_tiles =
  1668. active_layer()->tilings()->tiling_at(1)->AllTilesForTesting();
  1669. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  1670. low_tiles);
  1671. auto render_pass = viz::CompositorRenderPass::Create();
  1672. AppendQuadsData data;
  1673. active_layer()->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  1674. active_layer()->AppendQuads(render_pass.get(), &data);
  1675. active_layer()->DidDraw(nullptr);
  1676. EXPECT_EQ(1u, render_pass->quad_list.size());
  1677. EXPECT_EQ(0u, data.num_missing_tiles);
  1678. EXPECT_EQ(1u, data.num_incomplete_tiles);
  1679. EXPECT_TRUE(active_layer()->only_used_low_res_last_append_quads());
  1680. }
  1681. TEST_F(LegacySWPictureLayerImplTest, LowResTileIsIncomplete) {
  1682. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1683. gfx::Size tile_size(100, 100);
  1684. gfx::Size layer_bounds(200, 200);
  1685. scoped_refptr<FakeRasterSource> pending_raster_source =
  1686. FakeRasterSource::CreateFilled(layer_bounds);
  1687. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  1688. ActivateTree();
  1689. // All high res tiles have resources except one.
  1690. std::vector<Tile*> high_tiles =
  1691. active_layer()->tilings()->tiling_at(0)->AllTilesForTesting();
  1692. high_tiles.erase(high_tiles.begin());
  1693. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  1694. high_tiles);
  1695. // All low res tiles have resources.
  1696. std::vector<Tile*> low_tiles =
  1697. active_layer()->tilings()->tiling_at(1)->AllTilesForTesting();
  1698. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  1699. low_tiles);
  1700. auto render_pass = viz::CompositorRenderPass::Create();
  1701. AppendQuadsData data;
  1702. active_layer()->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  1703. active_layer()->AppendQuads(render_pass.get(), &data);
  1704. active_layer()->DidDraw(nullptr);
  1705. // The missing high res tile was replaced by a low res tile.
  1706. EXPECT_EQ(9u, render_pass->quad_list.size());
  1707. EXPECT_EQ(0u, data.num_missing_tiles);
  1708. EXPECT_EQ(1u, data.num_incomplete_tiles);
  1709. EXPECT_FALSE(active_layer()->only_used_low_res_last_append_quads());
  1710. }
  1711. TEST_F(LegacySWPictureLayerImplTest,
  1712. HighResAndIdealResTileIsCompleteWhenRasterScaleIsNotIdeal) {
  1713. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1714. gfx::Size tile_size(100, 100);
  1715. gfx::Size layer_bounds(200, 200);
  1716. gfx::Size viewport_size(400, 400);
  1717. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  1718. SetInitialDeviceScaleFactor(2.f);
  1719. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
  1720. SetWillChangeTransform(active_layer(), true);
  1721. // One ideal tile exists, this will get used when drawing.
  1722. std::vector<Tile*> ideal_tiles;
  1723. EXPECT_EQ(2.f, active_layer()->HighResTiling()->contents_scale_key());
  1724. ideal_tiles.push_back(active_layer()->HighResTiling()->TileAt(0, 0));
  1725. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  1726. ideal_tiles);
  1727. // Due to layer scale throttling, the raster contents scale is changed to 1,
  1728. // while the ideal is still 2.
  1729. SetupDrawPropertiesAndUpdateTiles(active_layer(), 1.f, 1.f, 1.f);
  1730. SetupDrawPropertiesAndUpdateTiles(active_layer(), 2.f, 1.f, 1.f);
  1731. EXPECT_EQ(1.f, active_layer()->HighResTiling()->contents_scale_key());
  1732. EXPECT_EQ(1.f, active_layer()->raster_contents_scale_key());
  1733. EXPECT_EQ(2.f, active_layer()->ideal_contents_scale_key());
  1734. // Both tilings still exist.
  1735. EXPECT_EQ(2.f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  1736. EXPECT_EQ(1.f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  1737. // All high res tiles have resources.
  1738. std::vector<Tile*> high_tiles =
  1739. active_layer()->HighResTiling()->AllTilesForTesting();
  1740. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  1741. high_tiles);
  1742. auto render_pass = viz::CompositorRenderPass::Create();
  1743. AppendQuadsData data;
  1744. active_layer()->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  1745. active_layer()->AppendQuads(render_pass.get(), &data);
  1746. active_layer()->DidDraw(nullptr);
  1747. // All high res tiles drew, and the one ideal res tile drew.
  1748. ASSERT_GT(render_pass->quad_list.size(), 9u);
  1749. EXPECT_EQ(gfx::Rect(0, 0, 99, 99), render_pass->quad_list.front()->rect);
  1750. EXPECT_EQ(gfx::RectF(0.f, 0.f, 99.f, 99.f),
  1751. viz::TileDrawQuad::MaterialCast(render_pass->quad_list.front())
  1752. ->tex_coord_rect);
  1753. EXPECT_EQ(gfx::Rect(99, 0, 100, 99),
  1754. render_pass->quad_list.ElementAt(1)->rect);
  1755. EXPECT_EQ(gfx::RectF(49.5f, 0.f, 50.f, 49.5f),
  1756. viz::TileDrawQuad::MaterialCast(render_pass->quad_list.ElementAt(1))
  1757. ->tex_coord_rect);
  1758. // Neither the high res nor the ideal tiles were considered as incomplete.
  1759. EXPECT_EQ(0u, data.num_missing_tiles);
  1760. EXPECT_EQ(0u, data.num_incomplete_tiles);
  1761. EXPECT_FALSE(active_layer()->only_used_low_res_last_append_quads());
  1762. }
  1763. TEST_F(LegacySWPictureLayerImplTest, AppendQuadsDataForCheckerboard) {
  1764. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  1765. gfx::Size tile_size(100, 100);
  1766. gfx::Size layer_bounds(200, 200);
  1767. gfx::Rect recorded_viewport(0, 0, 150, 150);
  1768. scoped_refptr<FakeRasterSource> pending_raster_source =
  1769. FakeRasterSource::CreatePartiallyFilled(layer_bounds, recorded_viewport);
  1770. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  1771. ActivateTree();
  1772. auto render_pass = viz::CompositorRenderPass::Create();
  1773. AppendQuadsData data;
  1774. active_layer()->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  1775. active_layer()->AppendQuads(render_pass.get(), &data);
  1776. active_layer()->DidDraw(nullptr);
  1777. EXPECT_EQ(1u, render_pass->quad_list.size());
  1778. EXPECT_EQ(1u, data.num_missing_tiles);
  1779. EXPECT_EQ(0u, data.num_incomplete_tiles);
  1780. EXPECT_EQ(40000, data.checkerboarded_visible_content_area);
  1781. EXPECT_EQ(17500, data.checkerboarded_no_recording_content_area);
  1782. EXPECT_EQ(22500, data.checkerboarded_needs_raster_content_area);
  1783. EXPECT_TRUE(active_layer()->only_used_low_res_last_append_quads());
  1784. }
  1785. TEST_F(LegacySWPictureLayerImplTest, HighResRequiredWhenActiveAllReady) {
  1786. gfx::Size layer_bounds(400, 400);
  1787. gfx::Size tile_size(100, 100);
  1788. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size,
  1789. gfx::Rect(layer_bounds));
  1790. active_layer()->SetAllTilesReady();
  1791. // All active tiles ready, so pending can only activate with all high res
  1792. // tiles.
  1793. pending_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  1794. EXPECT_FALSE(pending_layer()->LowResTiling());
  1795. AssertAllTilesRequired(pending_layer()->HighResTiling());
  1796. }
  1797. TEST_F(LegacySWPictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
  1798. gfx::Size layer_bounds(400, 400);
  1799. gfx::Size tile_size(100, 100);
  1800. // No invalidation.
  1801. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
  1802. // Verify active tree not ready.
  1803. Tile* some_active_tile =
  1804. active_layer()->HighResTiling()->AllTilesForTesting()[0];
  1805. EXPECT_FALSE(some_active_tile->draw_info().IsReadyToDraw());
  1806. // When high res are required, all tiles in active high res tiling should be
  1807. // required for activation.
  1808. host_impl()->SetRequiresHighResToDraw();
  1809. pending_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  1810. EXPECT_FALSE(pending_layer()->LowResTiling());
  1811. active_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  1812. active_layer()->LowResTiling()->UpdateAllRequiredStateForTesting();
  1813. EXPECT_TRUE(pending_layer()->HighResTiling()->AllTilesForTesting().empty());
  1814. AssertAllTilesRequired(active_layer()->HighResTiling());
  1815. AssertNoTilesRequired(active_layer()->LowResTiling());
  1816. }
  1817. TEST_F(LegacySWPictureLayerImplTest, AllHighResRequiredEvenIfNotChanged) {
  1818. gfx::Size layer_bounds(400, 400);
  1819. gfx::Size tile_size(100, 100);
  1820. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
  1821. Tile* some_active_tile =
  1822. active_layer()->HighResTiling()->AllTilesForTesting()[0];
  1823. EXPECT_FALSE(some_active_tile->draw_info().IsReadyToDraw());
  1824. // Since there are no invalidations, pending tree should have no tiles.
  1825. EXPECT_TRUE(pending_layer()->HighResTiling()->AllTilesForTesting().empty());
  1826. EXPECT_FALSE(pending_layer()->LowResTiling());
  1827. active_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  1828. active_layer()->LowResTiling()->UpdateAllRequiredStateForTesting();
  1829. AssertAllTilesRequired(active_layer()->HighResTiling());
  1830. AssertNoTilesRequired(active_layer()->LowResTiling());
  1831. }
  1832. TEST_F(LegacySWPictureLayerImplTest, DisallowRequiredForActivation) {
  1833. gfx::Size layer_bounds(400, 400);
  1834. gfx::Size tile_size(100, 100);
  1835. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
  1836. Tile* some_active_tile =
  1837. active_layer()->HighResTiling()->AllTilesForTesting()[0];
  1838. EXPECT_FALSE(some_active_tile->draw_info().IsReadyToDraw());
  1839. EXPECT_TRUE(pending_layer()->HighResTiling()->AllTilesForTesting().empty());
  1840. EXPECT_FALSE(pending_layer()->LowResTiling());
  1841. active_layer()->HighResTiling()->set_can_require_tiles_for_activation(false);
  1842. active_layer()->LowResTiling()->set_can_require_tiles_for_activation(false);
  1843. pending_layer()->HighResTiling()->set_can_require_tiles_for_activation(false);
  1844. // If we disallow required for activation, no tiles can be required.
  1845. active_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  1846. active_layer()->LowResTiling()->UpdateAllRequiredStateForTesting();
  1847. AssertNoTilesRequired(active_layer()->HighResTiling());
  1848. AssertNoTilesRequired(active_layer()->LowResTiling());
  1849. }
  1850. TEST_F(LegacySWPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
  1851. gfx::Size layer_bounds(400, 400);
  1852. gfx::Size tile_size(100, 100);
  1853. scoped_refptr<FakeRasterSource> pending_raster_source =
  1854. FakeRasterSource::CreateFilled(layer_bounds);
  1855. // This raster source will create tilings, but has no recordings so will not
  1856. // create any tiles. This is attempting to simulate scrolling past the end of
  1857. // recorded content on the active layer, where the recordings are so far away
  1858. // that no tiles are created.
  1859. scoped_refptr<FakeRasterSource> active_raster_source =
  1860. FakeRasterSource::CreatePartiallyFilled(layer_bounds, gfx::Rect());
  1861. SetupTreesWithFixedTileSize(pending_raster_source, active_raster_source,
  1862. tile_size, Region());
  1863. // Active layer has tilings, but no tiles due to missing recordings.
  1864. EXPECT_TRUE(active_layer()->CanHaveTilings());
  1865. EXPECT_EQ(active_layer()->tilings()->num_tilings(), 2u);
  1866. EXPECT_EQ(active_layer()->HighResTiling()->AllTilesForTesting().size(), 0u);
  1867. // Since the active layer has no tiles at all, the pending layer doesn't
  1868. // need content in order to activate.
  1869. pending_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  1870. EXPECT_FALSE(pending_layer()->LowResTiling());
  1871. AssertNoTilesRequired(pending_layer()->HighResTiling());
  1872. }
  1873. TEST_F(LegacySWPictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
  1874. gfx::Size layer_bounds(400, 400);
  1875. gfx::Size tile_size(100, 100);
  1876. scoped_refptr<FakeRasterSource> pending_raster_source =
  1877. FakeRasterSource::CreateFilled(layer_bounds);
  1878. scoped_refptr<FakeRasterSource> active_raster_source =
  1879. FakeRasterSource::CreateEmpty(layer_bounds);
  1880. SetupTreesWithFixedTileSize(pending_raster_source, active_raster_source,
  1881. tile_size, Region());
  1882. // Active layer can't have tiles.
  1883. EXPECT_FALSE(active_layer()->CanHaveTilings());
  1884. // All high res tiles required. This should be considered identical
  1885. // to the case where there is no active layer, to avoid flashing content.
  1886. // This can happen if a layer exists for a while and switches from
  1887. // not being able to have content to having content.
  1888. pending_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  1889. EXPECT_FALSE(pending_layer()->LowResTiling());
  1890. AssertAllTilesRequired(pending_layer()->HighResTiling());
  1891. }
  1892. TEST_F(LegacySWPictureLayerImplTest,
  1893. HighResRequiredWhenActiveHasDifferentBounds) {
  1894. gfx::Size pending_layer_bounds(400, 400);
  1895. gfx::Size active_layer_bounds(200, 200);
  1896. gfx::Size tile_size(100, 100);
  1897. scoped_refptr<FakeRasterSource> pending_raster_source =
  1898. FakeRasterSource::CreateFilled(pending_layer_bounds);
  1899. scoped_refptr<FakeRasterSource> active_raster_source =
  1900. FakeRasterSource::CreateFilled(active_layer_bounds);
  1901. SetupTreesWithFixedTileSize(pending_raster_source, active_raster_source,
  1902. tile_size, Region());
  1903. // Since the active layer has different bounds, the pending layer needs all
  1904. // high res tiles in order to activate.
  1905. pending_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  1906. EXPECT_FALSE(pending_layer()->LowResTiling());
  1907. active_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  1908. active_layer()->LowResTiling()->UpdateAllRequiredStateForTesting();
  1909. AssertAllTilesRequired(pending_layer()->HighResTiling());
  1910. AssertAllTilesRequired(active_layer()->HighResTiling());
  1911. AssertNoTilesRequired(active_layer()->LowResTiling());
  1912. }
  1913. TEST_F(LegacySWPictureLayerImplTest, ActivateUninitializedLayer) {
  1914. gfx::Size layer_bounds(400, 400);
  1915. scoped_refptr<FakeRasterSource> pending_raster_source =
  1916. FakeRasterSource::CreateFilled(layer_bounds);
  1917. host_impl()->CreatePendingTree();
  1918. LayerTreeImpl* pending_tree = host_impl()->pending_tree();
  1919. int kLayerId = 2;
  1920. std::unique_ptr<FakePictureLayerImpl> pending_layer =
  1921. FakePictureLayerImpl::Create(pending_tree, kLayerId,
  1922. pending_raster_source);
  1923. pending_layer->SetDrawsContent(true);
  1924. auto* raw_pending_layer = pending_layer.get();
  1925. SetupRootProperties(raw_pending_layer);
  1926. pending_tree->SetRootLayerForTesting(std::move(pending_layer));
  1927. PrepareForUpdateDrawProperties(pending_tree);
  1928. // Set some state on the pending layer, make sure it is not clobbered
  1929. // by a sync from the active layer. This could happen because if the
  1930. // pending layer has not been post-commit initialized it will attempt
  1931. // to sync from the active layer.
  1932. float raster_page_scale = 10.f * raw_pending_layer->raster_page_scale();
  1933. raw_pending_layer->set_raster_page_scale(raster_page_scale);
  1934. host_impl()->ActivateSyncTree();
  1935. FakePictureLayerImpl* raw_active_layer = static_cast<FakePictureLayerImpl*>(
  1936. host_impl()->active_tree()->LayerById(kLayerId));
  1937. EXPECT_EQ(0u, raw_active_layer->num_tilings());
  1938. EXPECT_EQ(raster_page_scale, raw_active_layer->raster_page_scale());
  1939. }
  1940. TEST_F(LegacySWPictureLayerImplTest, ShareTilesOnNextFrame) {
  1941. gfx::Size layer_bounds(1500, 1500);
  1942. scoped_refptr<FakeRasterSource> pending_raster_source =
  1943. FakeRasterSource::CreateFilled(layer_bounds);
  1944. SetupPendingTree(pending_raster_source);
  1945. PictureLayerTiling* tiling = pending_layer()->HighResTiling();
  1946. gfx::Rect first_invalidate = tiling->TilingDataForTesting().TileBounds(0, 0);
  1947. first_invalidate.Inset(tiling->TilingDataForTesting().border_texels());
  1948. gfx::Rect second_invalidate = tiling->TilingDataForTesting().TileBounds(1, 1);
  1949. second_invalidate.Inset(tiling->TilingDataForTesting().border_texels());
  1950. ActivateTree();
  1951. // Make a pending tree with an invalidated raster tile 0,0.
  1952. SetupPendingTreeWithInvalidation(pending_raster_source, first_invalidate);
  1953. // Activate and make a pending tree with an invalidated raster tile 1,1.
  1954. ActivateTree();
  1955. SetupPendingTreeWithInvalidation(pending_raster_source, second_invalidate);
  1956. PictureLayerTiling* pending_tiling = pending_layer()->tilings()->tiling_at(0);
  1957. PictureLayerTiling* active_tiling = active_layer()->tilings()->tiling_at(0);
  1958. // Tile 0,0 not exist on pending, but tile 1,1 should.
  1959. EXPECT_TRUE(active_tiling->TileAt(0, 0));
  1960. EXPECT_TRUE(active_tiling->TileAt(1, 0));
  1961. EXPECT_TRUE(active_tiling->TileAt(0, 1));
  1962. EXPECT_FALSE(pending_tiling->TileAt(0, 0));
  1963. EXPECT_FALSE(pending_tiling->TileAt(1, 0));
  1964. EXPECT_FALSE(pending_tiling->TileAt(0, 1));
  1965. EXPECT_NE(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
  1966. EXPECT_TRUE(active_tiling->TileAt(1, 1));
  1967. EXPECT_TRUE(pending_tiling->TileAt(1, 1));
  1968. // Drop the tiles on the active tree and recreate them.
  1969. active_layer()->tilings()->UpdateTilePriorities(gfx::Rect(), 1.f, 1.0,
  1970. Occlusion(), true);
  1971. EXPECT_TRUE(active_tiling->AllTilesForTesting().empty());
  1972. active_tiling->CreateAllTilesForTesting();
  1973. // Tile 0,0 not exist on pending, but tile 1,1 should.
  1974. EXPECT_TRUE(active_tiling->TileAt(0, 0));
  1975. EXPECT_TRUE(active_tiling->TileAt(1, 0));
  1976. EXPECT_TRUE(active_tiling->TileAt(0, 1));
  1977. EXPECT_FALSE(pending_tiling->TileAt(0, 0));
  1978. EXPECT_FALSE(pending_tiling->TileAt(1, 0));
  1979. EXPECT_FALSE(pending_tiling->TileAt(0, 1));
  1980. EXPECT_NE(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
  1981. EXPECT_TRUE(active_tiling->TileAt(1, 1));
  1982. EXPECT_TRUE(pending_tiling->TileAt(1, 1));
  1983. }
  1984. TEST_F(LegacySWPictureLayerImplTest, PendingHasNoTilesWithNoInvalidation) {
  1985. SetupDefaultTrees(gfx::Size(1500, 1500));
  1986. EXPECT_GE(active_layer()->num_tilings(), 1u);
  1987. EXPECT_GE(pending_layer()->num_tilings(), 1u);
  1988. // No invalidation.
  1989. PictureLayerTiling* active_tiling = active_layer()->tilings()->tiling_at(0);
  1990. PictureLayerTiling* pending_tiling = pending_layer()->tilings()->tiling_at(0);
  1991. ASSERT_TRUE(active_tiling);
  1992. ASSERT_TRUE(pending_tiling);
  1993. EXPECT_TRUE(active_tiling->TileAt(0, 0));
  1994. EXPECT_TRUE(active_tiling->TileAt(1, 0));
  1995. EXPECT_TRUE(active_tiling->TileAt(0, 1));
  1996. EXPECT_TRUE(active_tiling->TileAt(1, 1));
  1997. EXPECT_FALSE(pending_tiling->TileAt(0, 0));
  1998. EXPECT_FALSE(pending_tiling->TileAt(1, 0));
  1999. EXPECT_FALSE(pending_tiling->TileAt(0, 1));
  2000. EXPECT_FALSE(pending_tiling->TileAt(1, 1));
  2001. }
  2002. TEST_F(LegacySWPictureLayerImplTest, ShareInvalidActiveTreeTiles) {
  2003. gfx::Size layer_bounds(1500, 1500);
  2004. scoped_refptr<FakeRasterSource> pending_raster_source =
  2005. FakeRasterSource::CreateFilled(layer_bounds);
  2006. scoped_refptr<FakeRasterSource> active_raster_source =
  2007. FakeRasterSource::CreateFilled(layer_bounds);
  2008. SetupTreesWithInvalidation(pending_raster_source, active_raster_source,
  2009. gfx::Rect(1, 1));
  2010. // Activate the invalidation.
  2011. ActivateTree();
  2012. // Make another pending tree without any invalidation in it.
  2013. scoped_refptr<FakeRasterSource> pending_raster_source2 =
  2014. FakeRasterSource::CreateFilled(layer_bounds);
  2015. SetupPendingTree(pending_raster_source2);
  2016. EXPECT_GE(active_layer()->num_tilings(), 1u);
  2017. EXPECT_GE(pending_layer()->num_tilings(), 1u);
  2018. // The active tree invalidation was handled by the active tiles.
  2019. PictureLayerTiling* active_tiling = active_layer()->tilings()->tiling_at(0);
  2020. PictureLayerTiling* pending_tiling = pending_layer()->tilings()->tiling_at(0);
  2021. ASSERT_TRUE(active_tiling);
  2022. ASSERT_TRUE(pending_tiling);
  2023. EXPECT_TRUE(active_tiling->TileAt(0, 0));
  2024. EXPECT_TRUE(active_tiling->TileAt(1, 0));
  2025. EXPECT_TRUE(active_tiling->TileAt(0, 1));
  2026. EXPECT_TRUE(active_tiling->TileAt(1, 1));
  2027. EXPECT_FALSE(pending_tiling->TileAt(0, 0));
  2028. EXPECT_FALSE(pending_tiling->TileAt(1, 0));
  2029. EXPECT_FALSE(pending_tiling->TileAt(0, 1));
  2030. EXPECT_FALSE(pending_tiling->TileAt(1, 1));
  2031. }
  2032. TEST_F(LegacySWPictureLayerImplTest, RecreateInvalidPendingTreeTiles) {
  2033. // Set some invalidation on the pending tree. We should replace raster tiles
  2034. // that touch this.
  2035. SetupDefaultTreesWithInvalidation(gfx::Size(1500, 1500), gfx::Rect(1, 1));
  2036. EXPECT_GE(active_layer()->num_tilings(), 1u);
  2037. EXPECT_GE(pending_layer()->num_tilings(), 1u);
  2038. // The pending tree invalidation creates tiles on the pending tree.
  2039. PictureLayerTiling* active_tiling = active_layer()->tilings()->tiling_at(0);
  2040. PictureLayerTiling* pending_tiling = pending_layer()->tilings()->tiling_at(0);
  2041. ASSERT_TRUE(active_tiling);
  2042. ASSERT_TRUE(pending_tiling);
  2043. EXPECT_TRUE(active_tiling->TileAt(0, 0));
  2044. EXPECT_TRUE(active_tiling->TileAt(1, 0));
  2045. EXPECT_TRUE(active_tiling->TileAt(0, 1));
  2046. EXPECT_TRUE(active_tiling->TileAt(1, 1));
  2047. EXPECT_TRUE(pending_tiling->TileAt(0, 0));
  2048. EXPECT_FALSE(pending_tiling->TileAt(1, 0));
  2049. EXPECT_FALSE(pending_tiling->TileAt(0, 1));
  2050. EXPECT_FALSE(pending_tiling->TileAt(1, 1));
  2051. EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
  2052. }
  2053. TEST_F(LegacySWPictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
  2054. // Put 0.5 as high res.
  2055. SetInitialDeviceScaleFactor(0.5f);
  2056. scoped_refptr<FakeRasterSource> pending_raster_source =
  2057. FakeRasterSource::CreateFilled(gfx::Size(10, 10));
  2058. SetupPendingTree(pending_raster_source);
  2059. EXPECT_EQ(1u, pending_layer()->tilings()->num_tilings());
  2060. EXPECT_EQ(1, pending_layer()->tilings()->NumHighResTilings());
  2061. auto* high_res = pending_layer()->tilings()->FindTilingWithScaleKey(0.5f);
  2062. ASSERT_TRUE(high_res);
  2063. EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
  2064. ActivateTree();
  2065. EXPECT_EQ(1u, active_layer()->tilings()->num_tilings());
  2066. EXPECT_EQ(1, active_layer()->tilings()->NumHighResTilings());
  2067. high_res = active_layer()->tilings()->FindTilingWithScaleKey(0.5f);
  2068. ASSERT_TRUE(high_res);
  2069. EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
  2070. // Now, set the bounds to be 1x1, so that minimum contents scale becomes 1.
  2071. pending_raster_source = FakeRasterSource::CreateFilled(gfx::Size(1, 1));
  2072. SetupPendingTree(pending_raster_source);
  2073. EXPECT_EQ(1.f, pending_layer()->MinimumContentsScale());
  2074. // Since the MinContentsScale is 1, the 0.5 tiling should have been replaced
  2075. // by a 1.0 tiling during the UDP in SetupPendingTree.
  2076. EXPECT_EQ(1u, pending_layer()->tilings()->num_tilings());
  2077. high_res = pending_layer()->tilings()->FindTilingWithScaleKey(1.0f);
  2078. ASSERT_TRUE(high_res);
  2079. EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
  2080. ActivateTree();
  2081. EXPECT_EQ(1u, active_layer()->tilings()->num_tilings());
  2082. high_res = active_layer()->tilings()->FindTilingWithScaleKey(1.0f);
  2083. ASSERT_TRUE(high_res);
  2084. EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
  2085. }
  2086. // Tests when directly committing to the active tree, a small raster source
  2087. // makes the minimum scale bigger than the the previous high res tiling scale,
  2088. // causing the high res tiling (if not previously used for quads) to be removed.
  2089. // See crbug.com/1160003.
  2090. TEST_F(LegacySWPictureLayerImplTest,
  2091. HighResCreatedWhenBoundsShrinkOnActiveLayerWithUsedNonIdealScaleTiling) {
  2092. // Put 0.5 as high res.
  2093. SetInitialDeviceScaleFactor(0.5f);
  2094. SetupPendingTree(FakeRasterSource::CreateFilled(gfx::Size(10, 10)));
  2095. ActivateTree();
  2096. active_layer()
  2097. ->AddTiling(gfx::AxisTransform2d(1.0f, gfx::Vector2dF()))
  2098. ->set_resolution(NON_IDEAL_RESOLUTION);
  2099. EXPECT_EQ(2u, active_layer()->tilings()->num_tilings());
  2100. EXPECT_EQ(1, active_layer()->tilings()->NumHighResTilings());
  2101. auto* high_res = active_layer()->tilings()->FindTilingWithScaleKey(0.5f);
  2102. ASSERT_TRUE(high_res);
  2103. EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
  2104. EXPECT_TRUE(active_layer()->tilings()->FindTilingWithScaleKey(1.0f));
  2105. // Now, set the bounds to be 1x1, so that minimum contents scale becomes 1.
  2106. active_layer()->SetBounds(gfx::Size(1, 1));
  2107. active_layer()->SetRasterSource(
  2108. FakeRasterSource::CreateFilled(gfx::Size(1, 1)), Region());
  2109. active_layer()->AddLastAppendQuadsTilingForTesting(
  2110. active_layer()->tilings()->FindTilingWithScaleKey(1.0f));
  2111. active_layer()->UpdateTiles();
  2112. EXPECT_EQ(1.f, active_layer()->MinimumContentsScale());
  2113. // Since the MinContentsScale is 1, the 0.5 tiling should have been replaced
  2114. // by a 1.0 tiling during UpdateRasterSource() and UpdateTiles().
  2115. EXPECT_EQ(1u, active_layer()->tilings()->num_tilings());
  2116. high_res = active_layer()->tilings()->FindTilingWithScaleKey(1.0f);
  2117. ASSERT_TRUE(high_res);
  2118. EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
  2119. }
  2120. TEST_F(LegacySWPictureLayerImplTest, LowResTilingWithoutGpuRasterization) {
  2121. gfx::Size default_tile_size(host_impl()->settings().default_tile_size);
  2122. gfx::Size layer_bounds(default_tile_size.width() * 4,
  2123. default_tile_size.height() * 4);
  2124. SetupDefaultTrees(layer_bounds);
  2125. EXPECT_FALSE(host_impl()->use_gpu_rasterization());
  2126. // Should have only a high-res tiling.
  2127. EXPECT_EQ(1u, pending_layer()->tilings()->num_tilings());
  2128. ActivateTree();
  2129. // Should add a high and a low res for active tree.
  2130. EXPECT_EQ(2u, active_layer()->tilings()->num_tilings());
  2131. }
  2132. TEST_F(CommitToActiveTreePictureLayerImplTest,
  2133. NoLowResTilingWithGpuRasterization) {
  2134. gfx::Size default_tile_size(host_impl()->settings().default_tile_size);
  2135. gfx::Size layer_bounds(default_tile_size.width() * 4,
  2136. default_tile_size.height() * 4);
  2137. host_impl()->CommitComplete();
  2138. SetupDefaultTrees(layer_bounds);
  2139. EXPECT_TRUE(host_impl()->use_gpu_rasterization());
  2140. // Should only have the high-res tiling.
  2141. EXPECT_EQ(1u, pending_layer()->tilings()->num_tilings());
  2142. ActivateTree();
  2143. // Should only have the high-res tiling.
  2144. EXPECT_EQ(1u, active_layer()->tilings()->num_tilings());
  2145. }
  2146. TEST_F(CommitToActiveTreePictureLayerImplTest,
  2147. RequiredTilesWithGpuRasterization) {
  2148. host_impl()->CommitComplete();
  2149. gfx::Size viewport_size(1000, 1000);
  2150. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2151. gfx::Size layer_bounds(4000, 4000);
  2152. SetupDefaultTrees(layer_bounds);
  2153. EXPECT_TRUE(host_impl()->use_gpu_rasterization());
  2154. // Should only have the high-res tiling.
  2155. EXPECT_EQ(1u, active_layer()->tilings()->num_tilings());
  2156. active_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  2157. // High res tiling should have 128 tiles (4x16 tile grid, plus another
  2158. // factor of 2 for half-width tiles).
  2159. EXPECT_EQ(128u, active_layer()->HighResTiling()->AllTilesForTesting().size());
  2160. // Visible viewport should be covered by 8 tiles (4 high, half-width.
  2161. // No other tiles should be required for activation.
  2162. EXPECT_EQ(8u, NumberOfTilesRequired(active_layer()->HighResTiling()));
  2163. }
  2164. TEST_F(CommitToActiveTreePictureLayerImplTest,
  2165. RequiredTilesWithGpuRasterizationAndFractionalDsf) {
  2166. host_impl()->CommitComplete();
  2167. gfx::Size viewport_size(1502, 2560);
  2168. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2169. float dsf = 3.5f;
  2170. gfx::Size layer_bounds = gfx::ScaleToCeiledSize(viewport_size, 1.0f / dsf);
  2171. SetupDefaultTrees(layer_bounds);
  2172. EXPECT_TRUE(host_impl()->use_gpu_rasterization());
  2173. SetContentsScaleOnBothLayers(dsf /* contents_scale */,
  2174. dsf /* device_scale_factor */,
  2175. 1.0f /* page_scale_factor */);
  2176. active_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  2177. // High res tiling should have 4 tiles (1x4 tile grid).
  2178. EXPECT_EQ(4u, active_layer()->HighResTiling()->AllTilesForTesting().size());
  2179. }
  2180. TEST_F(LegacySWPictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
  2181. // Set up layers with tilings.
  2182. SetupDefaultTrees(gfx::Size(10, 10));
  2183. SetContentsScaleOnBothLayers(1.f, 1.f, 1.f);
  2184. pending_layer()->PushPropertiesTo(active_layer());
  2185. EXPECT_TRUE(pending_layer()->draws_content());
  2186. EXPECT_TRUE(pending_layer()->CanHaveTilings());
  2187. EXPECT_GE(pending_layer()->num_tilings(), 0u);
  2188. EXPECT_GE(active_layer()->num_tilings(), 0u);
  2189. // Set content to false, which should make CanHaveTilings return false.
  2190. pending_layer()->SetDrawsContent(false);
  2191. EXPECT_FALSE(pending_layer()->draws_content());
  2192. EXPECT_FALSE(pending_layer()->CanHaveTilings());
  2193. // No tilings should be pushed to active layer.
  2194. pending_layer()->PushPropertiesTo(active_layer());
  2195. EXPECT_EQ(0u, active_layer()->num_tilings());
  2196. }
  2197. TEST_F(LegacySWPictureLayerImplTest, FirstTilingDuringPinch) {
  2198. SetupDefaultTrees(gfx::Size(10, 10));
  2199. // We start with a tiling at scale 1.
  2200. EXPECT_EQ(1.f, pending_layer()->HighResTiling()->contents_scale_key());
  2201. // Since this test simulates a pinch it needs an input handler.
  2202. // TODO(bokan): This is a raster unit test, it shouldn't be using a real
  2203. // input handler.
  2204. InputHandler::Create(static_cast<CompositorDelegateForInput&>(*host_impl()));
  2205. // When we page scale up by 2.3, we get a new tiling that is a power of 2, in
  2206. // this case 4.
  2207. host_impl()->GetInputHandler().PinchGestureBegin(
  2208. gfx::Point(), ui::ScrollInputType::kTouchscreen);
  2209. float high_res_scale = 2.3f;
  2210. SetContentsScaleOnBothLayers(high_res_scale, 1.f, high_res_scale);
  2211. EXPECT_EQ(4.f, pending_layer()->HighResTiling()->contents_scale_key());
  2212. }
  2213. TEST_F(LegacySWPictureLayerImplTest, PinchingTooSmall) {
  2214. SetupDefaultTrees(gfx::Size(10, 10));
  2215. // We start with a tiling at scale 1.
  2216. EXPECT_EQ(1.f, pending_layer()->HighResTiling()->contents_scale_key());
  2217. // Since this test simulates a pinch it needs an input handler.
  2218. // TODO(bokan): This is a raster unit test, it shouldn't be using a real
  2219. // input handler.
  2220. InputHandler::Create(static_cast<CompositorDelegateForInput&>(*host_impl()));
  2221. host_impl()->GetInputHandler().PinchGestureBegin(
  2222. gfx::Point(), ui::ScrollInputType::kTouchscreen);
  2223. float high_res_scale = 0.0001f;
  2224. EXPECT_LT(high_res_scale, pending_layer()->MinimumContentsScale());
  2225. SetContentsScaleOnBothLayers(high_res_scale, 1.f, high_res_scale);
  2226. EXPECT_FLOAT_EQ(pending_layer()->MinimumContentsScale(),
  2227. pending_layer()->HighResTiling()->contents_scale_key());
  2228. }
  2229. TEST_F(LegacySWPictureLayerImplTest, PinchingTooSmallWithContentsScale) {
  2230. SetupDefaultTrees(gfx::Size(10, 10));
  2231. ResetTilingsAndRasterScales();
  2232. float contents_scale = 0.15f;
  2233. SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f);
  2234. ASSERT_GE(pending_layer()->num_tilings(), 0u);
  2235. EXPECT_FLOAT_EQ(contents_scale,
  2236. pending_layer()->HighResTiling()->contents_scale_key());
  2237. // Since this test simulates a pinch it needs an input handler.
  2238. // TODO(bokan): This is a raster unit test, it shouldn't be using a real
  2239. // input handler.
  2240. InputHandler::Create(static_cast<CompositorDelegateForInput&>(*host_impl()));
  2241. host_impl()->GetInputHandler().PinchGestureBegin(
  2242. gfx::Point(), ui::ScrollInputType::kTouchscreen);
  2243. float page_scale = 0.0001f;
  2244. EXPECT_LT(page_scale * contents_scale,
  2245. pending_layer()->MinimumContentsScale());
  2246. SetContentsScaleOnBothLayers(contents_scale * page_scale, 1.f, page_scale);
  2247. ASSERT_GE(pending_layer()->num_tilings(), 0u);
  2248. EXPECT_FLOAT_EQ(pending_layer()->MinimumContentsScale(),
  2249. pending_layer()->HighResTiling()->contents_scale_key());
  2250. }
  2251. TEST_F(LegacySWPictureLayerImplTest, HighResTilingDuringAnimation) {
  2252. gfx::Size viewport_size(1000, 1000);
  2253. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2254. gfx::Size layer_bounds(100, 100);
  2255. SetupDefaultTrees(layer_bounds);
  2256. float contents_scale = 1.f;
  2257. float device_scale = 1.f;
  2258. float page_scale = 1.f;
  2259. float maximum_animation_scale = 1.f;
  2260. bool affected_by_invalid_scale = false;
  2261. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  2262. // Starting an animation should cause tiling resolution to get set to the
  2263. // maximum animation scale factor.
  2264. maximum_animation_scale = 3.f;
  2265. contents_scale = 2.f;
  2266. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2267. page_scale, maximum_animation_scale,
  2268. affected_by_invalid_scale);
  2269. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 3.f);
  2270. // Further changes to scale during the animation should not cause a new
  2271. // high-res tiling to get created.
  2272. contents_scale = 4.f;
  2273. maximum_animation_scale = 5.f;
  2274. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2275. page_scale, maximum_animation_scale,
  2276. affected_by_invalid_scale);
  2277. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 3.f);
  2278. // Once we stop animating, a new high-res tiling should be created.
  2279. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2280. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 4.f);
  2281. // When animating with a maxmium animation scale factor that is so large
  2282. // that the layer grows larger than the viewport at this scale, a new
  2283. // high-res tiling should get created at a source scale that the rasterized
  2284. // layer will not be larger than the viewport, not at its maximum scale.
  2285. contents_scale = 2.f;
  2286. maximum_animation_scale = 11.f;
  2287. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2288. page_scale, maximum_animation_scale,
  2289. affected_by_invalid_scale);
  2290. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 10.f);
  2291. // Once we stop animating, a new high-res tiling should be created.
  2292. contents_scale = 11.f;
  2293. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2294. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 11.f);
  2295. // When animating with a maxmium animation scale factor that is so large
  2296. // that the layer grows larger than the viewport at this scale, and where
  2297. // the initial source scale is < 1, a new high-res tiling should get created
  2298. // at a source scale that the rasterized layer will not be larger than the
  2299. // viewport.
  2300. contents_scale = 0.1f;
  2301. maximum_animation_scale = 11.f;
  2302. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2303. page_scale, maximum_animation_scale,
  2304. affected_by_invalid_scale);
  2305. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 10.f);
  2306. // Once we stop animating, a new high-res tiling should be created.
  2307. contents_scale = 12.f;
  2308. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2309. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 12.f);
  2310. // When animating toward a smaller scale, but that is still so large that the
  2311. // layer grows larger than the viewport at this scale, a new high-res tiling
  2312. // should get created at a source scale that the rasterized layer is not
  2313. // larger than the viewport.
  2314. contents_scale = 11.f;
  2315. maximum_animation_scale = 11.f;
  2316. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2317. page_scale, maximum_animation_scale,
  2318. affected_by_invalid_scale);
  2319. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 10.f);
  2320. // Once we stop animating, a new high-res tiling should be created.
  2321. contents_scale = 11.f;
  2322. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2323. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 11.f);
  2324. }
  2325. TEST_F(LegacySWPictureLayerImplTest, HighResTilingDuringAnimationWideLayer) {
  2326. gfx::Size viewport_size(2048, 2048);
  2327. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2328. gfx::Size layer_bounds(1000000, 32);
  2329. SetupDefaultTrees(layer_bounds);
  2330. float contents_scale = 1.f;
  2331. float device_scale = 1.f;
  2332. float page_scale = 1.f;
  2333. float maximum_animation_scale = 1.f;
  2334. bool affected_by_invalid_scale = false;
  2335. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  2336. // Starting an animation should cause tiling resolution to get set to the
  2337. // maximum animation scale factor.
  2338. maximum_animation_scale = 3.f;
  2339. contents_scale = 2.f;
  2340. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2341. page_scale, maximum_animation_scale,
  2342. affected_by_invalid_scale);
  2343. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 3.f);
  2344. // Further changes to scale during the animation should not cause a new
  2345. // high-res tiling to get created.
  2346. contents_scale = 4.f;
  2347. maximum_animation_scale = 5.f;
  2348. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2349. page_scale, maximum_animation_scale,
  2350. affected_by_invalid_scale);
  2351. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 3.f);
  2352. // Once we stop animating, a new high-res tiling should be created.
  2353. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2354. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 4.f);
  2355. // When animating with a maxmium animation scale factor that is so large
  2356. // that the layer grows larger than the viewport at this scale, a new high-res
  2357. // tiling should get created at a source scale that the rasterized visible
  2358. // rect will not be larger than the viewport, not at its maximum scale.
  2359. contents_scale = 2.f;
  2360. maximum_animation_scale = 11.f;
  2361. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2362. page_scale, maximum_animation_scale,
  2363. affected_by_invalid_scale);
  2364. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 8.f);
  2365. // Once we stop animating, a new high-res tiling should be created.
  2366. contents_scale = 11.f;
  2367. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2368. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 11.f);
  2369. // When animating with a maxmium animation scale factor that is so large
  2370. // that the layer grows larger than the viewport at this scale, and where
  2371. // the initial source scale is < 1, a new high-res tiling should get created
  2372. // at a source scale that the rasterized visible rect will not be larger than
  2373. // the viewport.
  2374. contents_scale = 0.1f;
  2375. maximum_animation_scale = 11.f;
  2376. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2377. page_scale, maximum_animation_scale,
  2378. affected_by_invalid_scale);
  2379. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 8.f);
  2380. // Once we stop animating, a new high-res tiling should be created.
  2381. contents_scale = 12.f;
  2382. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2383. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 12.f);
  2384. // When animating toward a smaller scale, but that is still so large that the
  2385. // layer grows larger than the viewport at this scale, a new high-res tiling
  2386. // should get created at a source scale that the rasterized visible rect is
  2387. // not larger than the viewport.
  2388. contents_scale = 11.f;
  2389. maximum_animation_scale = 11.f;
  2390. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2391. page_scale, maximum_animation_scale,
  2392. affected_by_invalid_scale);
  2393. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 8.f);
  2394. // Once we stop animating, a new high-res tiling should be created.
  2395. contents_scale = 11.f;
  2396. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2397. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 11.f);
  2398. }
  2399. TEST_F(LegacySWPictureLayerImplTest,
  2400. HighResTilingDuringAnimationSmallerAnimationScale) {
  2401. gfx::Size viewport_size(1000, 1000);
  2402. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2403. gfx::Size layer_bounds(100, 100);
  2404. SetupDefaultTrees(layer_bounds);
  2405. float contents_scale = 1.f;
  2406. float device_scale = 1.f;
  2407. float page_scale = 1.f;
  2408. float maximum_animation_scale = 1.f;
  2409. bool affected_by_invalid_scale = false;
  2410. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  2411. // When animating with smaller animation scale factors (i.e. not accurately
  2412. // calculated because some limitations, e.g. nested scales), a new high-res
  2413. // tiling should be created at the current contents scale.
  2414. contents_scale = 0.5f;
  2415. maximum_animation_scale = 0.4f;
  2416. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2417. page_scale, maximum_animation_scale,
  2418. affected_by_invalid_scale);
  2419. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.5f);
  2420. // Increase the contents scale a bit, and the current high-res tiling should
  2421. // still be used.
  2422. contents_scale = 0.6f;
  2423. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2424. page_scale, maximum_animation_scale,
  2425. affected_by_invalid_scale);
  2426. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.5f);
  2427. // A new high-res tiling should be created at the current contents scale if
  2428. // its >1.5x of the current high-res scale.
  2429. contents_scale = 0.8f;
  2430. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2431. page_scale, maximum_animation_scale,
  2432. affected_by_invalid_scale);
  2433. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.8f);
  2434. // Reduce the contents scale, and the current high-res tiling should still be
  2435. // used.
  2436. contents_scale = 0.4f;
  2437. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2438. page_scale, maximum_animation_scale,
  2439. affected_by_invalid_scale);
  2440. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.8f);
  2441. }
  2442. TEST_F(LegacySWPictureLayerImplTest,
  2443. HighResTilingDuringAnimationSmallerAnimationScaleWithInvalidScale) {
  2444. gfx::Size viewport_size(1000, 1000);
  2445. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2446. gfx::Size layer_bounds(100, 100);
  2447. SetupDefaultTrees(layer_bounds);
  2448. float contents_scale = 1.f;
  2449. float device_scale = 1.f;
  2450. float page_scale = 1.f;
  2451. float maximum_animation_scale = 1.f;
  2452. bool affected_by_invalid_scale = true;
  2453. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  2454. // When animating with smaller animation scale factors (i.e. not accurately
  2455. // calculated because some limitations, e.g. nested scales), a new high-res
  2456. // tiling should be created at the current contents scale.
  2457. contents_scale = 0.5f;
  2458. maximum_animation_scale = 0.4f;
  2459. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2460. page_scale, maximum_animation_scale,
  2461. affected_by_invalid_scale);
  2462. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.5f);
  2463. // Increase the contents scale a bit, and the current high-res tiling should
  2464. // still be used.
  2465. contents_scale = 0.6f;
  2466. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2467. page_scale, maximum_animation_scale,
  2468. affected_by_invalid_scale);
  2469. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.5f);
  2470. // Raster scale change during animation should be avoided.
  2471. contents_scale = 1.2f;
  2472. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2473. page_scale, maximum_animation_scale,
  2474. affected_by_invalid_scale);
  2475. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.5f);
  2476. // Raster scale change during animation should be avoided.
  2477. contents_scale = 0.4f;
  2478. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2479. page_scale, maximum_animation_scale,
  2480. affected_by_invalid_scale);
  2481. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.5f);
  2482. // Should update raster scale if the cache is invalidated (simulating that a
  2483. // new property tree is pushed).
  2484. contents_scale = 1.2f;
  2485. SetMaximumAnimationToScreenScale(pending_layer(), maximum_animation_scale,
  2486. affected_by_invalid_scale);
  2487. host_impl()->pending_tree()->property_trees()->ResetCachedData();
  2488. SetupDrawPropertiesAndUpdateTiles(pending_layer(), contents_scale,
  2489. device_scale, page_scale);
  2490. SetMaximumAnimationToScreenScale(active_layer(), maximum_animation_scale,
  2491. affected_by_invalid_scale);
  2492. host_impl()->active_tree()->property_trees()->ResetCachedData();
  2493. SetupDrawPropertiesAndUpdateTiles(active_layer(), contents_scale,
  2494. device_scale, page_scale);
  2495. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.2f);
  2496. }
  2497. TEST_F(LegacySWPictureLayerImplTest, ViewportSizeChangeDuringAnimation) {
  2498. gfx::Size layer_bounds(100, 100);
  2499. SetupDefaultTrees(layer_bounds);
  2500. host_impl()->pending_tree()->SetDeviceViewportRect(gfx::Rect());
  2501. EXPECT_EQ(pending_layer()->HighResTiling()->contents_scale_key(), 1.f);
  2502. float maximum_animation_scale = 20.f;
  2503. // This flag should be ignored on viewport size change.
  2504. bool affected_by_invalid_scale = true;
  2505. // Starting an animation should cause tiling resolution to get set to the
  2506. // maximum animation scale factor, clamped by the viewport size (using default
  2507. // minimum 500x500 as the viewport is empty for now).
  2508. SetMaximumAnimationToScreenScale(pending_layer(), maximum_animation_scale,
  2509. affected_by_invalid_scale);
  2510. pending_layer()->UpdateTiles();
  2511. EXPECT_EQ(pending_layer()->HighResTiling()->contents_scale_key(), 5.f);
  2512. // Setting viewport rect smaller than the minimum won't change raster scale.
  2513. host_impl()->pending_tree()->SetDeviceViewportRect(gfx::Rect(400, 400));
  2514. SetMaximumAnimationToScreenScale(pending_layer(), maximum_animation_scale,
  2515. affected_by_invalid_scale);
  2516. pending_layer()->UpdateTiles();
  2517. EXPECT_EQ(pending_layer()->HighResTiling()->contents_scale_key(), 5.f);
  2518. // For a larger viewport size, the clamped scale is also larger.
  2519. host_impl()->pending_tree()->SetDeviceViewportRect(gfx::Rect(1000, 200));
  2520. SetMaximumAnimationToScreenScale(pending_layer(), maximum_animation_scale,
  2521. affected_by_invalid_scale);
  2522. pending_layer()->UpdateTiles();
  2523. EXPECT_EQ(pending_layer()->HighResTiling()->contents_scale_key(), 10.f);
  2524. }
  2525. TEST_F(LegacySWPictureLayerImplTest,
  2526. AnimationTilingChangesWithWillChangeTransformHint) {
  2527. gfx::Size viewport_size(1000, 1000);
  2528. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2529. gfx::Size layer_bounds(100, 100);
  2530. SetupDefaultTrees(layer_bounds);
  2531. float contents_scale = 1.f;
  2532. float device_scale = 1.f;
  2533. float page_scale = 1.f;
  2534. float maximum_animation_scale = 1.f;
  2535. bool affected_by_invalid_scale = false;
  2536. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  2537. SetWillChangeTransform(active_layer(), true);
  2538. SetWillChangeTransform(pending_layer(), true);
  2539. // Starting an animation should cause tiling resolution to get set to the
  2540. // maximum animation scale factor.
  2541. maximum_animation_scale = 2.f;
  2542. contents_scale = 1.f;
  2543. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2544. page_scale, maximum_animation_scale,
  2545. affected_by_invalid_scale);
  2546. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  2547. // Once we stop animating, because we have a will-change: transform hint
  2548. // we should not reset the scale factor.
  2549. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2550. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  2551. // Starting an animation with a different maximum animation scale should
  2552. // not cause a change either.
  2553. maximum_animation_scale = 1.5f;
  2554. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2555. page_scale, maximum_animation_scale,
  2556. affected_by_invalid_scale);
  2557. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  2558. // Again, stop animating, because we have a will-change: transform hint
  2559. // we should not reset the scale factor.
  2560. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2561. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  2562. // Test that will-change:transform on an ancestor has the same
  2563. // effects. We happen to have the page scale layer as an ancestor, so
  2564. // just use that.
  2565. maximum_animation_scale = 2.f;
  2566. LayerImpl* active_page_scale_layer =
  2567. host_impl()->active_tree()->LayerById(active_layer()->id() - 1);
  2568. LayerImpl* pending_page_scale_layer =
  2569. host_impl()->pending_tree()->LayerById(pending_layer()->id() - 1);
  2570. DCHECK_EQ(GetTransformNode(active_page_scale_layer)->id,
  2571. GetTransformNode(active_layer())->parent_id);
  2572. DCHECK_EQ(GetTransformNode(pending_page_scale_layer)->id,
  2573. GetTransformNode(pending_layer())->parent_id);
  2574. SetWillChangeTransform(active_layer(), false);
  2575. SetWillChangeTransform(pending_layer(), false);
  2576. SetContentsScaleOnBothLayers(contents_scale * 2.f, device_scale, page_scale);
  2577. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2578. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  2579. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2580. page_scale, maximum_animation_scale,
  2581. affected_by_invalid_scale);
  2582. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  2583. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2584. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  2585. SetWillChangeTransform(active_page_scale_layer, true);
  2586. SetWillChangeTransform(pending_page_scale_layer, true);
  2587. // re-set the false so node_or_ancestors_will_change_transform is recomputed
  2588. SetWillChangeTransform(active_layer(), false);
  2589. SetWillChangeTransform(pending_layer(), false);
  2590. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2591. page_scale, maximum_animation_scale,
  2592. affected_by_invalid_scale);
  2593. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  2594. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  2595. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  2596. }
  2597. TEST_F(LegacySWPictureLayerImplTest, HighResTilingDuringAnimationAspectRatio) {
  2598. gfx::Size viewport_size(2000, 1000);
  2599. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2600. gfx::Size layer_bounds(100, 100);
  2601. SetupDefaultTrees(layer_bounds);
  2602. float contents_scale = 1.f;
  2603. float device_scale = 1.f;
  2604. float page_scale = 1.f;
  2605. float maximum_animation_scale = 1.f;
  2606. bool affected_by_invalid_scale = false;
  2607. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  2608. // Allow rastering at maximum scale if the animation size is smaller than
  2609. // the square of the maximum viewport dimension.
  2610. contents_scale = 2.f;
  2611. maximum_animation_scale = 15.f;
  2612. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2613. page_scale, maximum_animation_scale,
  2614. affected_by_invalid_scale);
  2615. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 15.f);
  2616. }
  2617. TEST_F(LegacySWPictureLayerImplTest,
  2618. HighResTilingDuringAnimationAspectRatioTooLarge) {
  2619. gfx::Size viewport_size(2000, 1000);
  2620. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2621. gfx::Size layer_bounds(100, 100);
  2622. SetupDefaultTrees(layer_bounds);
  2623. float contents_scale = 1.f;
  2624. float device_scale = 1.f;
  2625. float page_scale = 1.f;
  2626. float maximum_animation_scale = 1.f;
  2627. bool affected_by_invalid_scale = false;
  2628. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  2629. // The maximum animation scale exceeds the squared size of the maximum
  2630. // viewport dimension, so raster scale should be shrunk to make the
  2631. // rasterized layer not larger than the viewport.
  2632. maximum_animation_scale = 21.f;
  2633. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2634. page_scale, maximum_animation_scale,
  2635. affected_by_invalid_scale);
  2636. EXPECT_NEAR(20.f, pending_layer()->HighResTiling()->contents_scale_key(),
  2637. 0.001f);
  2638. EXPECT_NEAR(20.f, active_layer()->HighResTiling()->contents_scale_key(),
  2639. 0.001f);
  2640. // The clamping logic still works with will-change:transform.
  2641. // Raster source size change forces adjustment of raster scale.
  2642. SetWillChangeTransform(active_layer(), true);
  2643. SetWillChangeTransform(pending_layer(), true);
  2644. layer_bounds = gfx::Size(200, 200);
  2645. Region invalidation;
  2646. // UpdateRasterSource() requires that the pending tree doesn't have tiles.
  2647. pending_layer()->picture_layer_tiling_set()->RemoveAllTiles();
  2648. pending_layer()->SetBounds(layer_bounds);
  2649. pending_layer()->UpdateRasterSource(
  2650. FakeRasterSource::CreateFilled(layer_bounds), &invalidation, nullptr,
  2651. nullptr);
  2652. pending_layer()->PushPropertiesTo(active_layer());
  2653. SetContentsAndAnimationScalesOnBothLayers(contents_scale, device_scale,
  2654. page_scale, maximum_animation_scale,
  2655. affected_by_invalid_scale);
  2656. EXPECT_NEAR(10.f, pending_layer()->HighResTiling()->contents_scale_key(),
  2657. 0.001f);
  2658. EXPECT_NEAR(10.f, active_layer()->HighResTiling()->contents_scale_key(),
  2659. 0.001f);
  2660. }
  2661. TEST_F(LegacySWPictureLayerImplTest, TilingSetRasterQueue) {
  2662. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  2663. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(500, 500));
  2664. gfx::Size layer_bounds(1000, 1000);
  2665. scoped_refptr<FakeRasterSource> pending_raster_source =
  2666. FakeRasterSource::CreateFilled(layer_bounds);
  2667. SetupPendingTree(pending_raster_source);
  2668. EXPECT_EQ(1u, pending_layer()->num_tilings());
  2669. std::set<Tile*> unique_tiles;
  2670. bool reached_prepaint = false;
  2671. int non_ideal_tile_count = 0u;
  2672. int low_res_tile_count = 0u;
  2673. int high_res_tile_count = 0u;
  2674. int high_res_now_tiles = 0u;
  2675. std::unique_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
  2676. pending_layer()->picture_layer_tiling_set(), false, false));
  2677. while (!queue->IsEmpty()) {
  2678. PrioritizedTile prioritized_tile = queue->Top();
  2679. TilePriority priority = prioritized_tile.priority();
  2680. EXPECT_TRUE(prioritized_tile.tile());
  2681. // Non-high res tiles only get visible tiles. Also, prepaint should only
  2682. // come at the end of the iteration.
  2683. if (priority.resolution != HIGH_RESOLUTION) {
  2684. EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
  2685. } else if (reached_prepaint) {
  2686. EXPECT_NE(TilePriority::NOW, priority.priority_bin);
  2687. } else {
  2688. reached_prepaint = priority.priority_bin != TilePriority::NOW;
  2689. if (!reached_prepaint)
  2690. ++high_res_now_tiles;
  2691. }
  2692. non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
  2693. low_res_tile_count += priority.resolution == LOW_RESOLUTION;
  2694. high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
  2695. unique_tiles.insert(prioritized_tile.tile());
  2696. queue->Pop();
  2697. }
  2698. EXPECT_TRUE(reached_prepaint);
  2699. EXPECT_EQ(0, non_ideal_tile_count);
  2700. EXPECT_EQ(0, low_res_tile_count);
  2701. // With layer size being 1000x1000 and default tile size 256x256, we expect to
  2702. // see 4 now tiles out of 16 total high res tiles.
  2703. EXPECT_EQ(16, high_res_tile_count);
  2704. EXPECT_EQ(4, high_res_now_tiles);
  2705. EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
  2706. static_cast<int>(unique_tiles.size()));
  2707. std::unique_ptr<TilingSetRasterQueueRequired> required_queue(
  2708. new TilingSetRasterQueueRequired(
  2709. pending_layer()->picture_layer_tiling_set(),
  2710. RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW));
  2711. EXPECT_TRUE(required_queue->IsEmpty());
  2712. required_queue = std::make_unique<TilingSetRasterQueueRequired>(
  2713. pending_layer()->picture_layer_tiling_set(),
  2714. RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION);
  2715. EXPECT_FALSE(required_queue->IsEmpty());
  2716. int required_for_activation_count = 0;
  2717. while (!required_queue->IsEmpty()) {
  2718. PrioritizedTile prioritized_tile = required_queue->Top();
  2719. EXPECT_TRUE(prioritized_tile.tile()->required_for_activation());
  2720. EXPECT_FALSE(prioritized_tile.tile()->draw_info().IsReadyToDraw());
  2721. ++required_for_activation_count;
  2722. required_queue->Pop();
  2723. }
  2724. // All of the high res tiles should be required for activation, since there is
  2725. // no active twin.
  2726. EXPECT_EQ(high_res_now_tiles, required_for_activation_count);
  2727. // No NOW tiles.
  2728. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  2729. pending_layer()->draw_properties().visible_layer_rect =
  2730. gfx::Rect(1100, 1100, 500, 500);
  2731. pending_layer()->UpdateTiles();
  2732. unique_tiles.clear();
  2733. high_res_tile_count = 0u;
  2734. queue = std::make_unique<TilingSetRasterQueueAll>(
  2735. pending_layer()->picture_layer_tiling_set(), false, false);
  2736. while (!queue->IsEmpty()) {
  2737. PrioritizedTile prioritized_tile = queue->Top();
  2738. TilePriority priority = prioritized_tile.priority();
  2739. EXPECT_TRUE(prioritized_tile.tile());
  2740. // Non-high res tiles only get visible tiles.
  2741. EXPECT_EQ(HIGH_RESOLUTION, priority.resolution);
  2742. EXPECT_NE(TilePriority::NOW, priority.priority_bin);
  2743. high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
  2744. unique_tiles.insert(prioritized_tile.tile());
  2745. queue->Pop();
  2746. }
  2747. EXPECT_EQ(16, high_res_tile_count);
  2748. EXPECT_EQ(high_res_tile_count, static_cast<int>(unique_tiles.size()));
  2749. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  2750. pending_layer()->draw_properties().visible_layer_rect =
  2751. gfx::Rect(0, 0, 500, 500);
  2752. pending_layer()->UpdateTiles();
  2753. std::vector<Tile*> high_res_tiles =
  2754. pending_layer()->HighResTiling()->AllTilesForTesting();
  2755. for (auto tile_it = high_res_tiles.begin(); tile_it != high_res_tiles.end();
  2756. ++tile_it) {
  2757. Tile* tile = *tile_it;
  2758. TileDrawInfo& draw_info = tile->draw_info();
  2759. draw_info.SetSolidColorForTesting(SkColors::kRed);
  2760. }
  2761. queue = std::make_unique<TilingSetRasterQueueAll>(
  2762. pending_layer()->picture_layer_tiling_set(), true, false);
  2763. EXPECT_TRUE(queue->IsEmpty());
  2764. }
  2765. TEST_F(LegacySWPictureLayerImplTest, TilingSetRasterQueueActiveTree) {
  2766. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  2767. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(500, 500));
  2768. gfx::Size layer_bounds(1000, 1000);
  2769. scoped_refptr<FakeRasterSource> pending_raster_source =
  2770. FakeRasterSource::CreateFilled(layer_bounds);
  2771. SetupPendingTree(pending_raster_source);
  2772. ActivateTree();
  2773. EXPECT_EQ(2u, active_layer()->num_tilings());
  2774. std::unique_ptr<TilingSetRasterQueueRequired> queue(
  2775. new TilingSetRasterQueueRequired(
  2776. active_layer()->picture_layer_tiling_set(),
  2777. RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW));
  2778. EXPECT_FALSE(queue->IsEmpty());
  2779. while (!queue->IsEmpty()) {
  2780. PrioritizedTile prioritized_tile = queue->Top();
  2781. EXPECT_TRUE(prioritized_tile.tile()->required_for_draw());
  2782. EXPECT_FALSE(prioritized_tile.tile()->draw_info().IsReadyToDraw());
  2783. queue->Pop();
  2784. }
  2785. queue = std::make_unique<TilingSetRasterQueueRequired>(
  2786. active_layer()->picture_layer_tiling_set(),
  2787. RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION);
  2788. EXPECT_TRUE(queue->IsEmpty());
  2789. }
  2790. TEST_F(LegacySWPictureLayerImplTest, TilingSetRasterQueueRequiredNoHighRes) {
  2791. scoped_refptr<FakeRasterSource> pending_raster_source =
  2792. FakeRasterSource::CreateFilledSolidColor(gfx::Size(1024, 1024));
  2793. SetupPendingTree(pending_raster_source);
  2794. EXPECT_FALSE(
  2795. pending_layer()->picture_layer_tiling_set()->FindTilingWithResolution(
  2796. HIGH_RESOLUTION));
  2797. std::unique_ptr<TilingSetRasterQueueRequired> queue(
  2798. new TilingSetRasterQueueRequired(
  2799. pending_layer()->picture_layer_tiling_set(),
  2800. RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION));
  2801. EXPECT_TRUE(queue->IsEmpty());
  2802. }
  2803. TEST_F(LegacySWPictureLayerImplTest, TilingSetEvictionQueue) {
  2804. gfx::Size layer_bounds(1000, 1000);
  2805. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  2806. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(500, 500));
  2807. scoped_refptr<FakeRasterSource> pending_raster_source =
  2808. FakeRasterSource::CreateFilled(layer_bounds);
  2809. // TODO(vmpstr): Add a test with tilings other than high res on the active
  2810. // tree (crbug.com/519607).
  2811. SetupPendingTree(pending_raster_source);
  2812. EXPECT_EQ(1u, pending_layer()->num_tilings());
  2813. std::vector<Tile*> all_tiles;
  2814. for (size_t i = 0; i < pending_layer()->num_tilings(); ++i) {
  2815. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(i);
  2816. std::vector<Tile*> tiles = tiling->AllTilesForTesting();
  2817. all_tiles.insert(all_tiles.end(), tiles.begin(), tiles.end());
  2818. }
  2819. std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
  2820. bool mark_required = false;
  2821. size_t number_of_marked_tiles = 0u;
  2822. size_t number_of_unmarked_tiles = 0u;
  2823. for (size_t i = 0; i < pending_layer()->num_tilings(); ++i) {
  2824. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(i);
  2825. for (PictureLayerTiling::CoverageIterator iter(
  2826. tiling, 1.f, pending_layer()->visible_layer_rect());
  2827. iter; ++iter) {
  2828. if (mark_required) {
  2829. number_of_marked_tiles++;
  2830. iter->set_required_for_activation(true);
  2831. } else {
  2832. number_of_unmarked_tiles++;
  2833. }
  2834. mark_required = !mark_required;
  2835. }
  2836. }
  2837. // Sanity checks.
  2838. EXPECT_EQ(16u, all_tiles.size());
  2839. EXPECT_EQ(16u, all_tiles_set.size());
  2840. EXPECT_GT(number_of_marked_tiles, 1u);
  2841. EXPECT_GT(number_of_unmarked_tiles, 1u);
  2842. // Tiles don't have resources yet.
  2843. std::unique_ptr<TilingSetEvictionQueue> queue(new TilingSetEvictionQueue(
  2844. pending_layer()->picture_layer_tiling_set(),
  2845. pending_layer()->contributes_to_drawn_render_surface()));
  2846. EXPECT_TRUE(queue->IsEmpty());
  2847. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  2848. all_tiles);
  2849. std::set<Tile*> unique_tiles;
  2850. float expected_scales[] = {low_res_factor, 1.f};
  2851. size_t scale_index = 0;
  2852. bool reached_visible = false;
  2853. PrioritizedTile last_tile;
  2854. size_t distance_decreasing = 0;
  2855. size_t distance_increasing = 0;
  2856. queue = std::make_unique<TilingSetEvictionQueue>(
  2857. pending_layer()->picture_layer_tiling_set(),
  2858. pending_layer()->contributes_to_drawn_render_surface());
  2859. while (!queue->IsEmpty()) {
  2860. PrioritizedTile prioritized_tile = queue->Top();
  2861. Tile* tile = prioritized_tile.tile();
  2862. if (!last_tile.tile())
  2863. last_tile = prioritized_tile;
  2864. EXPECT_TRUE(tile);
  2865. TilePriority priority = prioritized_tile.priority();
  2866. if (priority.priority_bin == TilePriority::NOW) {
  2867. reached_visible = true;
  2868. last_tile = prioritized_tile;
  2869. break;
  2870. }
  2871. EXPECT_FALSE(tile->required_for_activation());
  2872. while (std::abs(tile->contents_scale_key() - expected_scales[scale_index]) >
  2873. std::numeric_limits<float>::epsilon()) {
  2874. ++scale_index;
  2875. ASSERT_LT(scale_index, std::size(expected_scales));
  2876. }
  2877. EXPECT_FLOAT_EQ(tile->contents_scale_key(), expected_scales[scale_index]);
  2878. unique_tiles.insert(tile);
  2879. if (tile->required_for_activation() ==
  2880. last_tile.tile()->required_for_activation() &&
  2881. std::abs(tile->contents_scale_key() -
  2882. last_tile.tile()->contents_scale_key()) <
  2883. std::numeric_limits<float>::epsilon()) {
  2884. if (priority.distance_to_visible <=
  2885. last_tile.priority().distance_to_visible)
  2886. ++distance_decreasing;
  2887. else
  2888. ++distance_increasing;
  2889. }
  2890. last_tile = prioritized_tile;
  2891. queue->Pop();
  2892. }
  2893. // 4 high res tiles are inside the viewport, the rest are evicted.
  2894. EXPECT_TRUE(reached_visible);
  2895. EXPECT_EQ(12u, unique_tiles.size());
  2896. EXPECT_EQ(1u, distance_increasing);
  2897. EXPECT_EQ(11u, distance_decreasing);
  2898. scale_index = 0;
  2899. bool reached_required = false;
  2900. while (!queue->IsEmpty()) {
  2901. PrioritizedTile prioritized_tile = queue->Top();
  2902. Tile* tile = prioritized_tile.tile();
  2903. EXPECT_TRUE(tile);
  2904. TilePriority priority = prioritized_tile.priority();
  2905. EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
  2906. if (reached_required) {
  2907. EXPECT_TRUE(tile->required_for_activation());
  2908. } else if (tile->required_for_activation()) {
  2909. reached_required = true;
  2910. scale_index = 0;
  2911. }
  2912. while (std::abs(tile->contents_scale_key() - expected_scales[scale_index]) >
  2913. std::numeric_limits<float>::epsilon()) {
  2914. ++scale_index;
  2915. ASSERT_LT(scale_index, std::size(expected_scales));
  2916. }
  2917. EXPECT_FLOAT_EQ(tile->contents_scale_key(), expected_scales[scale_index]);
  2918. unique_tiles.insert(tile);
  2919. queue->Pop();
  2920. }
  2921. EXPECT_TRUE(reached_required);
  2922. EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
  2923. }
  2924. TEST_F(LegacySWPictureLayerImplTest, Occlusion) {
  2925. gfx::Size tile_size(102, 102);
  2926. gfx::Size layer_bounds(1000, 1000);
  2927. gfx::Size viewport_size(1000, 1000);
  2928. LayerTreeImplTestBase impl;
  2929. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2930. scoped_refptr<FakeRasterSource> pending_raster_source =
  2931. FakeRasterSource::CreateFilled(layer_bounds);
  2932. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  2933. ActivateTree();
  2934. std::vector<Tile*> tiles =
  2935. active_layer()->HighResTiling()->AllTilesForTesting();
  2936. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
  2937. {
  2938. SCOPED_TRACE("No occlusion");
  2939. gfx::Rect occluded;
  2940. impl.AppendQuadsWithOcclusion(active_layer(), occluded);
  2941. VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect(layer_bounds));
  2942. EXPECT_EQ(100u, impl.quad_list().size());
  2943. }
  2944. {
  2945. SCOPED_TRACE("Full occlusion");
  2946. gfx::Rect occluded(active_layer()->visible_layer_rect());
  2947. impl.AppendQuadsWithOcclusion(active_layer(), occluded);
  2948. VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
  2949. EXPECT_EQ(impl.quad_list().size(), 0u);
  2950. }
  2951. {
  2952. SCOPED_TRACE("Partial occlusion");
  2953. gfx::Rect occluded(150, 0, 200, 1000);
  2954. impl.AppendQuadsWithOcclusion(active_layer(), occluded);
  2955. size_t partially_occluded_count = 0;
  2956. VerifyQuadsAreOccluded(impl.quad_list(), occluded,
  2957. &partially_occluded_count);
  2958. // The layer outputs one quad, which is partially occluded.
  2959. EXPECT_EQ(100u - 10u, impl.quad_list().size());
  2960. EXPECT_EQ(10u + 10u, partially_occluded_count);
  2961. }
  2962. }
  2963. TEST_F(LegacySWPictureLayerImplTest, OcclusionOnSolidColorPictureLayer) {
  2964. gfx::Size layer_bounds(1000, 1000);
  2965. gfx::Size viewport_size(1000, 1000);
  2966. LayerTreeImplTestBase impl;
  2967. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2968. scoped_refptr<FakeRasterSource> pending_raster_source =
  2969. FakeRasterSource::CreateFilledSolidColor(layer_bounds);
  2970. SetupPendingTree(std::move(pending_raster_source), gfx::Size(), Region());
  2971. // Device scale factor should not affect a non-mask solid color layer.
  2972. host_impl()->pending_tree()->SetDeviceScaleFactor(2.f);
  2973. ActivateTree();
  2974. {
  2975. SCOPED_TRACE("Scaled occlusion");
  2976. gfx::Rect occluded(300, 0, 2000, 2000);
  2977. impl.AppendQuadsWithOcclusion(active_layer(), occluded);
  2978. size_t partial_occluded_count = 0;
  2979. VerifyQuadsAreOccluded(impl.quad_list(), occluded, &partial_occluded_count);
  2980. // Because of the implementation of test helper AppendQuadsWithOcclusion,
  2981. // the occlusion will have a scale transform resulted from the device scale
  2982. // factor. A single partially overlapped DrawQuad of 500x500 will be added.
  2983. EXPECT_EQ(1u, impl.quad_list().size());
  2984. EXPECT_EQ(1u, partial_occluded_count);
  2985. }
  2986. }
  2987. TEST_F(LegacySWPictureLayerImplTest, IgnoreOcclusionOnSolidColorMask) {
  2988. gfx::Size layer_bounds(1000, 1000);
  2989. gfx::Size viewport_size(1000, 1000);
  2990. LayerTreeImplTestBase impl;
  2991. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  2992. scoped_refptr<FakeRasterSource> pending_raster_source =
  2993. FakeRasterSource::CreateFilledSolidColor(layer_bounds);
  2994. SetupPendingTree(std::move(pending_raster_source), gfx::Size(), Region());
  2995. host_impl()->pending_tree()->SetDeviceScaleFactor(2.f);
  2996. ActivateTree();
  2997. {
  2998. SCOPED_TRACE("Scaled occlusion");
  2999. gfx::Rect occluded(150, 0, 200, 1000);
  3000. impl.AppendQuadsWithOcclusion(active_layer(), occluded);
  3001. size_t partial_occluded_count = 0;
  3002. VerifyQuadsAreOccluded(impl.quad_list(), gfx::Rect(),
  3003. &partial_occluded_count);
  3004. // None of the quads shall be occluded because mask layers ignores
  3005. // occlusion.
  3006. EXPECT_EQ(1u, impl.quad_list().size());
  3007. EXPECT_EQ(0u, partial_occluded_count);
  3008. }
  3009. }
  3010. TEST_F(LegacySWPictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
  3011. gfx::Size tile_size(host_impl()->settings().default_tile_size);
  3012. SetupDefaultTrees(tile_size);
  3013. ResetTilingsAndRasterScales();
  3014. float contents_scale = 2.f;
  3015. float device_scale = 1.5f;
  3016. float page_scale = 1.f;
  3017. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3018. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  3019. // Changing the source scale without being in an animation will cause
  3020. // the layer to change scale.
  3021. contents_scale = 3.f;
  3022. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3023. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 3.f);
  3024. contents_scale = 0.5f;
  3025. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3026. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.5f);
  3027. // If we change the layer contents scale after setting will change
  3028. // will, then it will be updated if it's below the minimum scale (page scale *
  3029. // device scale).
  3030. SetWillChangeTransform(active_layer(), true);
  3031. SetWillChangeTransform(pending_layer(), true);
  3032. contents_scale = 0.75f;
  3033. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3034. // The scale is clamped to the native scale.
  3035. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.5f);
  3036. // Further changes to the source scale will no longer be reflected in the
  3037. // contents scale.
  3038. contents_scale = 2.f;
  3039. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3040. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.5f);
  3041. // Ditto.
  3042. contents_scale = 20.f;
  3043. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3044. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.5f);
  3045. // Disabling the will-change hint will once again make the raster scale update
  3046. // with the ideal scale.
  3047. SetWillChangeTransform(active_layer(), false);
  3048. SetWillChangeTransform(pending_layer(), false);
  3049. contents_scale = 3.f;
  3050. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3051. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 3.f);
  3052. }
  3053. TEST_F(LegacySWPictureLayerImplTest, TinyRasterScale) {
  3054. gfx::Size tile_size(host_impl()->settings().default_tile_size);
  3055. SetupDefaultTrees(tile_size);
  3056. ResetTilingsAndRasterScales();
  3057. float contents_scale = 0.01f;
  3058. float device_scale = 1.5f;
  3059. float page_scale = 1.f;
  3060. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3061. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.01f);
  3062. // If we change the layer contents scale after setting will change
  3063. // will, then it will be updated if it's below the minimum scale (page scale *
  3064. // device scale).
  3065. SetWillChangeTransform(active_layer(), true);
  3066. SetWillChangeTransform(pending_layer(), true);
  3067. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3068. // The scale is clamped to the native scale.
  3069. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.01f);
  3070. // Further changes to the source scale will no longer be reflected in the
  3071. // contents scale.
  3072. contents_scale = 0.02f;
  3073. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3074. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.01f);
  3075. // ... unless the difference is very big.
  3076. contents_scale = 0.12f;
  3077. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3078. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 0.12f);
  3079. // Bigger scale will be clamped to the native scale.
  3080. contents_scale = 0.5f;
  3081. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3082. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.5f);
  3083. }
  3084. TEST_F(LegacySWPictureLayerImplTest,
  3085. ForceAdjustRasterScaleWillChangeTransform) {
  3086. gfx::Size layer_bounds(100, 100);
  3087. SetupDefaultTrees(layer_bounds);
  3088. ResetTilingsAndRasterScales();
  3089. float contents_scale = 1.f;
  3090. float device_scale = 1.f;
  3091. float page_scale = 1.f;
  3092. SetWillChangeTransform(active_layer(), true);
  3093. SetWillChangeTransform(pending_layer(), true);
  3094. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3095. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  3096. contents_scale = 2.f;
  3097. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3098. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 1.f);
  3099. // Raster source size change forces adjustment of raster scale.
  3100. layer_bounds = gfx::Size(200, 200);
  3101. Region invalidation;
  3102. // UpdateRasterSource() requires that the pending tree doesn't have tiles.
  3103. pending_layer()->picture_layer_tiling_set()->RemoveAllTiles();
  3104. pending_layer()->SetBounds(layer_bounds);
  3105. pending_layer()->UpdateRasterSource(
  3106. FakeRasterSource::CreateFilled(layer_bounds), &invalidation, nullptr,
  3107. nullptr);
  3108. pending_layer()->PushPropertiesTo(active_layer());
  3109. SetContentsScaleOnBothLayers(contents_scale, device_scale, page_scale);
  3110. EXPECT_BOTH_EQ(HighResTiling()->contents_scale_key(), 2.f);
  3111. }
  3112. TEST_F(LegacySWPictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
  3113. gfx::Size tile_size(100, 100);
  3114. gfx::Size layer_bounds(1000, 1000);
  3115. // Make sure pending tree has tiles.
  3116. gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
  3117. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
  3118. // All pending layer tiles required are not ready.
  3119. EXPECT_FALSE(host_impl()->tile_manager()->IsReadyToActivate());
  3120. // Initialize all low-res tiles.
  3121. EXPECT_FALSE(pending_layer()->LowResTiling());
  3122. pending_layer()->SetAllTilesReadyInTiling(active_layer()->LowResTiling());
  3123. // Low-res tiles should not be enough.
  3124. EXPECT_FALSE(host_impl()->tile_manager()->IsReadyToActivate());
  3125. // Initialize remaining tiles.
  3126. pending_layer()->SetAllTilesReady();
  3127. active_layer()->SetAllTilesReady();
  3128. EXPECT_TRUE(host_impl()->tile_manager()->IsReadyToActivate());
  3129. }
  3130. TEST_F(LegacySWPictureLayerImplTest, HighResReadyToDrawEnoughToActivate) {
  3131. gfx::Size tile_size(100, 100);
  3132. gfx::Size layer_bounds(1000, 1000);
  3133. // Make sure pending tree has tiles.
  3134. gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
  3135. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
  3136. // All pending layer tiles required are not ready.
  3137. EXPECT_FALSE(host_impl()->tile_manager()->IsReadyToActivate());
  3138. // Initialize all high-res tiles.
  3139. pending_layer()->SetAllTilesReadyInTiling(pending_layer()->HighResTiling());
  3140. active_layer()->SetAllTilesReadyInTiling(active_layer()->HighResTiling());
  3141. // High-res tiles should be enough, since they cover everything visible.
  3142. EXPECT_TRUE(host_impl()->tile_manager()->IsReadyToActivate());
  3143. }
  3144. TEST_F(LegacySWPictureLayerImplTest, ActiveHighResReadyNotEnoughToActivate) {
  3145. gfx::Size tile_size(100, 100);
  3146. gfx::Size layer_bounds(1000, 1000);
  3147. // Make sure pending tree has tiles.
  3148. gfx::Rect invalidation(gfx::Point(50, 50), tile_size);
  3149. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, invalidation);
  3150. // Initialize all high-res tiles in the active layer.
  3151. active_layer()->SetAllTilesReadyInTiling(active_layer()->HighResTiling());
  3152. // The pending high-res tiles are not ready, so we cannot activate.
  3153. EXPECT_FALSE(host_impl()->tile_manager()->IsReadyToActivate());
  3154. // When the pending high-res tiles are ready, we can activate.
  3155. pending_layer()->SetAllTilesReadyInTiling(pending_layer()->HighResTiling());
  3156. EXPECT_TRUE(host_impl()->tile_manager()->IsReadyToActivate());
  3157. }
  3158. TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
  3159. gfx::Size layer_bounds(1300, 1900);
  3160. SetupDefaultTrees(layer_bounds);
  3161. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  3162. EXPECT_LT(low_res_factor, 1.f);
  3163. ResetTilingsAndRasterScales();
  3164. SetupDrawPropertiesAndUpdateTiles(active_layer(),
  3165. 6.f, // ideal contents scale
  3166. 3.f, // device scale
  3167. 2.f); // page scale
  3168. ASSERT_EQ(1u, active_layer()->tilings()->num_tilings());
  3169. EXPECT_FLOAT_EQ(
  3170. 6.f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  3171. // If we change the page scale factor, then we should get new tilings.
  3172. SetupDrawPropertiesAndUpdateTiles(active_layer(),
  3173. 6.6f, // ideal contents scale
  3174. 3.f, // device scale
  3175. 2.2f); // page scale
  3176. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  3177. EXPECT_FLOAT_EQ(
  3178. 6.6f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  3179. // If we change the device scale factor, then we should get new tilings.
  3180. SetupDrawPropertiesAndUpdateTiles(active_layer(),
  3181. 7.26f, // ideal contents scale
  3182. 3.3f, // device scale
  3183. 2.2f); // page scale
  3184. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  3185. EXPECT_FLOAT_EQ(
  3186. 7.26f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  3187. // If we change the device scale factor, but end up at the same total scale
  3188. // factor somehow, then we don't get new tilings.
  3189. SetupDrawPropertiesAndUpdateTiles(active_layer(),
  3190. 7.26f, // ideal contents scale
  3191. 2.2f, // device scale
  3192. 3.3f); // page scale
  3193. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  3194. EXPECT_FLOAT_EQ(
  3195. 7.26f, active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  3196. }
  3197. TEST_F(NoLowResPictureLayerImplTest, PendingLayerOnlyHasHighResTiling) {
  3198. gfx::Size layer_bounds(1300, 1900);
  3199. SetupDefaultTrees(layer_bounds);
  3200. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  3201. EXPECT_LT(low_res_factor, 1.f);
  3202. ResetTilingsAndRasterScales();
  3203. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  3204. 6.f, // ideal contents scale
  3205. 3.f, // device scale
  3206. 2.f); // page scale
  3207. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  3208. EXPECT_FLOAT_EQ(
  3209. 6.f, pending_layer()->tilings()->tiling_at(0)->contents_scale_key());
  3210. // If we change the page scale factor, then we should get new tilings.
  3211. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  3212. 6.6f, // ideal contents scale
  3213. 3.f, // device scale
  3214. 2.2f); // page scale
  3215. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  3216. EXPECT_FLOAT_EQ(
  3217. 6.6f, pending_layer()->tilings()->tiling_at(0)->contents_scale_key());
  3218. // If we change the device scale factor, then we should get new tilings.
  3219. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  3220. 7.26f, // ideal contents scale
  3221. 3.3f, // device scale
  3222. 2.2f); // page scale
  3223. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  3224. EXPECT_FLOAT_EQ(
  3225. 7.26f, pending_layer()->tilings()->tiling_at(0)->contents_scale_key());
  3226. // If we change the device scale factor, but end up at the same total scale
  3227. // factor somehow, then we don't get new tilings.
  3228. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  3229. 7.26f, // ideal contents scale
  3230. 2.2f, // device scale
  3231. 3.3f); // page scale
  3232. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  3233. EXPECT_FLOAT_EQ(
  3234. 7.26f, pending_layer()->tilings()->tiling_at(0)->contents_scale_key());
  3235. }
  3236. TEST_F(NoLowResPictureLayerImplTest, AllHighResRequiredEvenIfNotChanged) {
  3237. gfx::Size layer_bounds(400, 400);
  3238. gfx::Size tile_size(100, 100);
  3239. SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size, Region());
  3240. Tile* some_active_tile =
  3241. active_layer()->HighResTiling()->AllTilesForTesting()[0];
  3242. EXPECT_FALSE(some_active_tile->draw_info().IsReadyToDraw());
  3243. // Since there is no invalidation, pending tree should have no tiles.
  3244. EXPECT_TRUE(pending_layer()->HighResTiling()->AllTilesForTesting().empty());
  3245. if (host_impl()->settings().create_low_res_tiling)
  3246. EXPECT_TRUE(pending_layer()->LowResTiling()->AllTilesForTesting().empty());
  3247. active_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  3248. if (host_impl()->settings().create_low_res_tiling)
  3249. active_layer()->LowResTiling()->UpdateAllRequiredStateForTesting();
  3250. AssertAllTilesRequired(active_layer()->HighResTiling());
  3251. if (host_impl()->settings().create_low_res_tiling)
  3252. AssertNoTilesRequired(active_layer()->LowResTiling());
  3253. }
  3254. TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
  3255. gfx::Size layer_bounds(400, 400);
  3256. gfx::Size tile_size(100, 100);
  3257. scoped_refptr<FakeRasterSource> pending_raster_source =
  3258. FakeRasterSource::CreateFilled(layer_bounds);
  3259. // This raster source will create tilings, but has no recordings so will not
  3260. // create any tiles. This is attempting to simulate scrolling past the end of
  3261. // recorded content on the active layer, where the recordings are so far away
  3262. // that no tiles are created.
  3263. scoped_refptr<FakeRasterSource> active_raster_source =
  3264. FakeRasterSource::CreatePartiallyFilled(layer_bounds, gfx::Rect());
  3265. SetupTreesWithFixedTileSize(pending_raster_source, active_raster_source,
  3266. tile_size, Region());
  3267. // Active layer has tilings, but no tiles due to missing recordings.
  3268. EXPECT_TRUE(active_layer()->CanHaveTilings());
  3269. EXPECT_EQ(active_layer()->tilings()->num_tilings(),
  3270. host_impl()->settings().create_low_res_tiling ? 2u : 1u);
  3271. EXPECT_EQ(active_layer()->HighResTiling()->AllTilesForTesting().size(), 0u);
  3272. // Since the active layer has no tiles at all, the pending layer doesn't
  3273. // need content in order to activate.
  3274. pending_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  3275. if (host_impl()->settings().create_low_res_tiling)
  3276. pending_layer()->LowResTiling()->UpdateAllRequiredStateForTesting();
  3277. AssertNoTilesRequired(pending_layer()->HighResTiling());
  3278. if (host_impl()->settings().create_low_res_tiling)
  3279. AssertNoTilesRequired(pending_layer()->LowResTiling());
  3280. }
  3281. TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
  3282. gfx::Size layer_bounds(1300, 1900);
  3283. std::vector<PictureLayerTiling*> used_tilings;
  3284. SetupDefaultTrees(layer_bounds);
  3285. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  3286. EXPECT_LT(low_res_factor, 1.f);
  3287. // Set the device scale and page scale so that the minimum that we would clamp
  3288. // to is small. This test isn't testing the clamping. See
  3289. // RasterScaleChangeWithoutAnimation for this test.
  3290. float device_scale = 0.5f;
  3291. float page_scale = 1.f;
  3292. float scale = 1.f;
  3293. SetWillChangeTransform(active_layer(), true);
  3294. ResetTilingsAndRasterScales();
  3295. SetContentsScaleOnBothLayers(scale, device_scale, page_scale);
  3296. ASSERT_EQ(1u, active_layer()->tilings()->num_tilings());
  3297. // Ensure UpdateTiles won't remove any tilings. Note this is unrelated to
  3298. // |used_tilings| variable, and it's here only to ensure that active_layer()
  3299. // won't remove tilings before the test has a chance to verify behavior.
  3300. active_layer()->MarkAllTilingsUsed();
  3301. // We only have ideal tilings, so they aren't removed.
  3302. used_tilings.clear();
  3303. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  3304. ASSERT_EQ(1u, active_layer()->tilings()->num_tilings());
  3305. // Since this test simulates a pinch it needs an input handler.
  3306. // TODO(bokan): This is a raster unit test, it shouldn't be using a real
  3307. // input handler.
  3308. InputHandler::Create(static_cast<CompositorDelegateForInput&>(*host_impl()));
  3309. host_impl()->GetInputHandler().PinchGestureBegin(
  3310. gfx::Point(), ui::ScrollInputType::kTouchscreen);
  3311. // Changing the ideal but not creating new tilings.
  3312. scale *= 1.5f;
  3313. page_scale *= 1.5f;
  3314. SetContentsScaleOnBothLayers(scale, device_scale, page_scale);
  3315. ASSERT_EQ(1u, active_layer()->tilings()->num_tilings());
  3316. // The tilings are still our target scale, so they aren't removed.
  3317. used_tilings.clear();
  3318. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  3319. ASSERT_EQ(1u, active_layer()->tilings()->num_tilings());
  3320. host_impl()->GetInputHandler().PinchGestureEnd(gfx::Point());
  3321. // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
  3322. scale /= 4.f;
  3323. page_scale /= 4.f;
  3324. SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale);
  3325. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  3326. EXPECT_FLOAT_EQ(
  3327. 1.f, active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  3328. // Ensure UpdateTiles won't remove any tilings.
  3329. active_layer()->MarkAllTilingsUsed();
  3330. // Mark the non-ideal tilings as used. They won't be removed.
  3331. used_tilings.clear();
  3332. used_tilings.push_back(active_layer()->tilings()->tiling_at(1));
  3333. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  3334. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  3335. // Now move the ideal scale to 0.5. Our target stays 1.2.
  3336. SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale);
  3337. // The high resolution tiling is between target and ideal, so is not
  3338. // removed. The low res tiling for the old ideal=1.0 scale is removed.
  3339. used_tilings.clear();
  3340. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  3341. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  3342. // Now move the ideal scale to 1.0. Our target stays 1.2.
  3343. SetContentsScaleOnBothLayers(1.f, device_scale, page_scale);
  3344. // All the tilings are between are target and the ideal, so they are not
  3345. // removed.
  3346. used_tilings.clear();
  3347. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  3348. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  3349. // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
  3350. SetupDrawPropertiesAndUpdateTiles(active_layer(), 1.1f, device_scale,
  3351. page_scale);
  3352. // Because the pending layer's ideal scale is still 1.0, our tilings fall
  3353. // in the range [1.0,1.2] and are kept.
  3354. used_tilings.clear();
  3355. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  3356. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  3357. // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
  3358. // 1.2 still.
  3359. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.1f, device_scale,
  3360. page_scale);
  3361. // Our 1.0 tiling now falls outside the range between our ideal scale and our
  3362. // target raster scale. But it is in our used tilings set, so nothing is
  3363. // deleted.
  3364. used_tilings.clear();
  3365. used_tilings.push_back(active_layer()->tilings()->tiling_at(1));
  3366. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  3367. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  3368. // If we remove it from our used tilings set, it is outside the range to keep
  3369. // so it is deleted.
  3370. used_tilings.clear();
  3371. active_layer()->CleanUpTilingsOnActiveLayer(used_tilings);
  3372. ASSERT_EQ(1u, active_layer()->tilings()->num_tilings());
  3373. }
  3374. TEST_F(NoLowResPictureLayerImplTest, ReleaseTileResources) {
  3375. gfx::Size layer_bounds(1300, 1900);
  3376. SetupDefaultTrees(layer_bounds);
  3377. EXPECT_EQ(1u, pending_layer()->tilings()->num_tilings());
  3378. EXPECT_EQ(1u, active_layer()->tilings()->num_tilings());
  3379. // All tilings should be removed when losing output surface.
  3380. active_layer()->ReleaseTileResources();
  3381. EXPECT_TRUE(active_layer()->tilings());
  3382. EXPECT_EQ(0u, active_layer()->num_tilings());
  3383. active_layer()->RecreateTileResources();
  3384. EXPECT_EQ(0u, active_layer()->num_tilings());
  3385. pending_layer()->ReleaseTileResources();
  3386. EXPECT_TRUE(pending_layer()->tilings());
  3387. EXPECT_EQ(0u, pending_layer()->num_tilings());
  3388. pending_layer()->RecreateTileResources();
  3389. EXPECT_EQ(0u, pending_layer()->num_tilings());
  3390. // This should create new tilings.
  3391. SetupDrawPropertiesAndUpdateTiles(pending_layer(),
  3392. 1.3f, // ideal contents scale
  3393. 2.7f, // device scale
  3394. 3.2f); // page scale
  3395. EXPECT_EQ(1u, pending_layer()->tilings()->num_tilings());
  3396. }
  3397. TEST_F(LegacySWPictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
  3398. auto render_pass = viz::CompositorRenderPass::Create();
  3399. gfx::Size layer_bounds(1000, 2000);
  3400. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(10000, 20000));
  3401. SetupDefaultTrees(layer_bounds);
  3402. ResetTilingsAndRasterScales();
  3403. SetupDrawPropertiesAndUpdateTiles(active_layer(), 2.5f, 1.f, 1.f);
  3404. float max_contents_scale = active_layer()->MaximumTilingContentsScale();
  3405. EXPECT_EQ(2.5f, max_contents_scale);
  3406. gfx::Transform scaled_draw_transform = active_layer()->DrawTransform();
  3407. scaled_draw_transform.Scale(SK_Scalar1 / max_contents_scale,
  3408. SK_Scalar1 / max_contents_scale);
  3409. AppendQuadsData data;
  3410. active_layer()->AppendQuads(render_pass.get(), &data);
  3411. // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
  3412. EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
  3413. // The quad_to_target_transform should be scaled by the
  3414. // MaximumTilingContentsScale on the layer.
  3415. EXPECT_EQ(scaled_draw_transform.ToString(),
  3416. render_pass->shared_quad_state_list.front()
  3417. ->quad_to_target_transform.ToString());
  3418. // The content_bounds should be scaled by the
  3419. // MaximumTilingContentsScale on the layer.
  3420. EXPECT_EQ(
  3421. gfx::Rect(2500u, 5000u).ToString(),
  3422. render_pass->shared_quad_state_list.front()->quad_layer_rect.ToString());
  3423. // The visible_layer_rect should be scaled by the
  3424. // MaximumTilingContentsScale on the layer.
  3425. EXPECT_EQ(gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
  3426. render_pass->shared_quad_state_list.front()
  3427. ->visible_quad_layer_rect.ToString());
  3428. }
  3429. class PictureLayerImplTestWithDelegatingRenderer
  3430. : public LegacySWPictureLayerImplTest {
  3431. public:
  3432. std::unique_ptr<LayerTreeFrameSink> CreateLayerTreeFrameSink() override {
  3433. return FakeLayerTreeFrameSink::Create3d();
  3434. }
  3435. };
  3436. TEST_F(PictureLayerImplTestWithDelegatingRenderer,
  3437. DelegatingRendererWithTileOOM) {
  3438. // This test is added for crbug.com/402321, where quad should be produced when
  3439. // raster on demand is not allowed and tile is OOM.
  3440. gfx::Size layer_bounds(1000, 1000);
  3441. // Create tiles.
  3442. scoped_refptr<FakeRasterSource> pending_raster_source =
  3443. FakeRasterSource::CreateFilled(layer_bounds);
  3444. SetupPendingTree(pending_raster_source);
  3445. pending_layer()->SetBounds(layer_bounds);
  3446. ActivateTree();
  3447. UpdateDrawProperties(host_impl()->active_tree());
  3448. std::vector<Tile*> tiles =
  3449. active_layer()->HighResTiling()->AllTilesForTesting();
  3450. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
  3451. // Force tiles after max_tiles to be OOM. TileManager uses
  3452. // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
  3453. // directly set state to host_impl_, so we set policy that would change the
  3454. // state. We also need to update tree priority separately.
  3455. GlobalStateThatImpactsTilePriority state;
  3456. size_t max_tiles = 1;
  3457. gfx::Size tile_size(host_impl()->settings().default_tile_size);
  3458. size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
  3459. size_t resource_limit = max_tiles;
  3460. ManagedMemoryPolicy policy(memory_limit,
  3461. gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
  3462. resource_limit);
  3463. host_impl()->SetMemoryPolicy(policy);
  3464. host_impl()->SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
  3465. host_impl()->PrepareTiles();
  3466. auto render_pass = viz::CompositorRenderPass::Create();
  3467. AppendQuadsData data;
  3468. active_layer()->WillDraw(DRAW_MODE_HARDWARE, nullptr);
  3469. active_layer()->AppendQuads(render_pass.get(), &data);
  3470. active_layer()->DidDraw(nullptr);
  3471. // Even when OOM, quads should be produced, and should be different material
  3472. // from quads with resource.
  3473. EXPECT_LT(max_tiles, render_pass->quad_list.size());
  3474. EXPECT_EQ(viz::DrawQuad::Material::kTiledContent,
  3475. render_pass->quad_list.front()->material);
  3476. EXPECT_EQ(viz::DrawQuad::Material::kSolidColor,
  3477. render_pass->quad_list.back()->material);
  3478. }
  3479. class OcclusionTrackingPictureLayerImplTest
  3480. : public LegacySWPictureLayerImplTest {
  3481. public:
  3482. LayerTreeSettings CreateSettings() override {
  3483. LayerTreeSettings settings = LegacySWPictureLayerImplTest::CreateSettings();
  3484. settings.use_occlusion_for_tile_prioritization = true;
  3485. return settings;
  3486. }
  3487. void VerifyEvictionConsidersOcclusion(FakePictureLayerImpl* layer,
  3488. WhichTree tree,
  3489. size_t expected_occluded_tile_count,
  3490. int source_line) {
  3491. size_t occluded_tile_count = 0u;
  3492. PrioritizedTile last_tile;
  3493. std::unique_ptr<TilingSetEvictionQueue> queue(new TilingSetEvictionQueue(
  3494. layer->picture_layer_tiling_set(),
  3495. layer->contributes_to_drawn_render_surface()));
  3496. while (!queue->IsEmpty()) {
  3497. PrioritizedTile prioritized_tile = queue->Top();
  3498. Tile* tile = prioritized_tile.tile();
  3499. if (!last_tile.tile())
  3500. last_tile = prioritized_tile;
  3501. // The only way we will encounter an occluded tile after an unoccluded
  3502. // tile is if the priorty bin decreased, the tile is required for
  3503. // activation, or the scale changed.
  3504. bool tile_is_occluded = prioritized_tile.is_occluded();
  3505. if (tile_is_occluded) {
  3506. occluded_tile_count++;
  3507. bool last_tile_is_occluded = last_tile.is_occluded();
  3508. if (!last_tile_is_occluded) {
  3509. TilePriority::PriorityBin tile_priority_bin =
  3510. prioritized_tile.priority().priority_bin;
  3511. TilePriority::PriorityBin last_tile_priority_bin =
  3512. last_tile.priority().priority_bin;
  3513. EXPECT_TRUE(tile_priority_bin < last_tile_priority_bin ||
  3514. tile->required_for_activation() ||
  3515. tile->contents_scale_key() !=
  3516. last_tile.tile()->contents_scale_key())
  3517. << "line: " << source_line;
  3518. }
  3519. }
  3520. last_tile = prioritized_tile;
  3521. queue->Pop();
  3522. }
  3523. EXPECT_EQ(expected_occluded_tile_count, occluded_tile_count)
  3524. << "line: " << source_line;
  3525. }
  3526. void TestOcclusionForScale(float scale, int expected_occluded_count);
  3527. };
  3528. TEST_F(OcclusionTrackingPictureLayerImplTest,
  3529. OccludedTilesSkippedDuringRasterization) {
  3530. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  3531. gfx::Size tile_size(102, 102);
  3532. gfx::Size layer_bounds(1000, 1000);
  3533. gfx::Size viewport_size(500, 500);
  3534. gfx::Vector2dF occluding_layer_position(310.f, 0.f);
  3535. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  3536. scoped_refptr<FakeRasterSource> pending_raster_source =
  3537. FakeRasterSource::CreateFilled(layer_bounds);
  3538. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  3539. // No occlusion.
  3540. int unoccluded_tile_count = 0;
  3541. std::unique_ptr<TilingSetRasterQueueAll> queue(new TilingSetRasterQueueAll(
  3542. pending_layer()->picture_layer_tiling_set(), false, false));
  3543. while (!queue->IsEmpty()) {
  3544. PrioritizedTile prioritized_tile = queue->Top();
  3545. Tile* tile = prioritized_tile.tile();
  3546. // Occluded tiles should not be iterated over.
  3547. EXPECT_FALSE(prioritized_tile.is_occluded());
  3548. // Some tiles may not be visible (i.e. outside the viewport). The rest are
  3549. // visible and at least partially unoccluded, verified by the above expect.
  3550. bool tile_is_visible =
  3551. tile->content_rect().Intersects(pending_layer()->visible_layer_rect());
  3552. if (tile_is_visible)
  3553. unoccluded_tile_count++;
  3554. queue->Pop();
  3555. }
  3556. EXPECT_EQ(unoccluded_tile_count, 25);
  3557. // Partial occlusion.
  3558. LayerImpl* layer1 = AddLayer<LayerImpl>(host_impl()->pending_tree());
  3559. layer1->SetBounds(layer_bounds);
  3560. layer1->SetDrawsContent(true);
  3561. layer1->SetContentsOpaque(true);
  3562. CopyProperties(pending_layer(), layer1);
  3563. layer1->SetOffsetToTransformParent(occluding_layer_position);
  3564. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  3565. UpdateDrawProperties(host_impl()->pending_tree());
  3566. unoccluded_tile_count = 0;
  3567. queue = std::make_unique<TilingSetRasterQueueAll>(
  3568. pending_layer()->picture_layer_tiling_set(), false, false);
  3569. while (!queue->IsEmpty()) {
  3570. PrioritizedTile prioritized_tile = queue->Top();
  3571. Tile* tile = prioritized_tile.tile();
  3572. EXPECT_FALSE(prioritized_tile.is_occluded());
  3573. bool tile_is_visible =
  3574. tile->content_rect().Intersects(pending_layer()->visible_layer_rect());
  3575. if (tile_is_visible)
  3576. unoccluded_tile_count++;
  3577. queue->Pop();
  3578. }
  3579. EXPECT_EQ(20, unoccluded_tile_count);
  3580. // Full occlusion.
  3581. layer1->SetOffsetToTransformParent(gfx::Vector2dF());
  3582. layer1->NoteLayerPropertyChanged();
  3583. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  3584. UpdateDrawProperties(host_impl()->pending_tree());
  3585. unoccluded_tile_count = 0;
  3586. queue = std::make_unique<TilingSetRasterQueueAll>(
  3587. pending_layer()->picture_layer_tiling_set(), false, false);
  3588. while (!queue->IsEmpty()) {
  3589. PrioritizedTile prioritized_tile = queue->Top();
  3590. Tile* tile = prioritized_tile.tile();
  3591. EXPECT_FALSE(prioritized_tile.is_occluded());
  3592. bool tile_is_visible =
  3593. tile->content_rect().Intersects(pending_layer()->visible_layer_rect());
  3594. if (tile_is_visible)
  3595. unoccluded_tile_count++;
  3596. queue->Pop();
  3597. }
  3598. EXPECT_EQ(unoccluded_tile_count, 0);
  3599. }
  3600. TEST_F(OcclusionTrackingPictureLayerImplTest,
  3601. OccludedTilesNotMarkedAsRequired) {
  3602. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  3603. gfx::Size tile_size(102, 102);
  3604. gfx::Size layer_bounds(1000, 1000);
  3605. gfx::Size viewport_size(500, 500);
  3606. gfx::Vector2dF occluding_layer_position(310.f, 0.f);
  3607. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  3608. scoped_refptr<FakeRasterSource> pending_raster_source =
  3609. FakeRasterSource::CreateFilled(layer_bounds);
  3610. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  3611. // No occlusion.
  3612. int occluded_tile_count = 0;
  3613. for (size_t i = 0; i < pending_layer()->num_tilings(); ++i) {
  3614. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(i);
  3615. auto prioritized_tiles =
  3616. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  3617. occluded_tile_count = 0;
  3618. for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
  3619. gfx::Rect(layer_bounds));
  3620. iter; ++iter) {
  3621. if (!*iter)
  3622. continue;
  3623. const Tile* tile = *iter;
  3624. // Fully occluded tiles are not required for activation.
  3625. if (prioritized_tiles[tile].is_occluded()) {
  3626. EXPECT_FALSE(tile->required_for_activation());
  3627. occluded_tile_count++;
  3628. }
  3629. }
  3630. EXPECT_EQ(occluded_tile_count, 0);
  3631. }
  3632. // Partial occlusion.
  3633. LayerImpl* layer1 = AddLayer<LayerImpl>(host_impl()->pending_tree());
  3634. layer1->SetBounds(layer_bounds);
  3635. layer1->SetDrawsContent(true);
  3636. layer1->SetContentsOpaque(true);
  3637. CopyProperties(pending_layer(), layer1);
  3638. layer1->SetOffsetToTransformParent(occluding_layer_position);
  3639. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  3640. UpdateDrawProperties(host_impl()->pending_tree());
  3641. for (size_t i = 0; i < pending_layer()->num_tilings(); ++i) {
  3642. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(i);
  3643. auto prioritized_tiles =
  3644. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  3645. occluded_tile_count = 0;
  3646. for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
  3647. gfx::Rect(layer_bounds));
  3648. iter; ++iter) {
  3649. if (!*iter)
  3650. continue;
  3651. const Tile* tile = *iter;
  3652. if (prioritized_tiles[tile].is_occluded()) {
  3653. EXPECT_FALSE(tile->required_for_activation());
  3654. occluded_tile_count++;
  3655. }
  3656. }
  3657. switch (i) {
  3658. case 0:
  3659. EXPECT_EQ(occluded_tile_count, 5);
  3660. break;
  3661. case 1:
  3662. EXPECT_EQ(occluded_tile_count, 2);
  3663. break;
  3664. default:
  3665. NOTREACHED();
  3666. }
  3667. }
  3668. // Full occlusion.
  3669. layer1->SetOffsetToTransformParent(gfx::Vector2dF());
  3670. layer1->NoteLayerPropertyChanged();
  3671. host_impl()->AdvanceToNextFrame(base::Milliseconds(200));
  3672. UpdateDrawProperties(host_impl()->pending_tree());
  3673. for (size_t i = 0; i < pending_layer()->num_tilings(); ++i) {
  3674. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(i);
  3675. auto prioritized_tiles =
  3676. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  3677. occluded_tile_count = 0;
  3678. for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
  3679. gfx::Rect(layer_bounds));
  3680. iter; ++iter) {
  3681. if (!*iter)
  3682. continue;
  3683. const Tile* tile = *iter;
  3684. if (prioritized_tiles[tile].is_occluded()) {
  3685. EXPECT_FALSE(tile->required_for_activation());
  3686. occluded_tile_count++;
  3687. }
  3688. }
  3689. switch (i) {
  3690. case 0:
  3691. EXPECT_EQ(25, occluded_tile_count);
  3692. break;
  3693. case 1:
  3694. EXPECT_EQ(4, occluded_tile_count);
  3695. break;
  3696. default:
  3697. NOTREACHED();
  3698. }
  3699. }
  3700. }
  3701. void OcclusionTrackingPictureLayerImplTest::TestOcclusionForScale(
  3702. float scale,
  3703. int expected_occluded_count) {
  3704. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  3705. gfx::Size tile_size(102, 102);
  3706. gfx::Size layer_bounds(1000, 1000);
  3707. gfx::Size viewport_size(500, 500);
  3708. gfx::Vector2dF occluding_layer_position(310.f, 0.f);
  3709. scoped_refptr<FakeRasterSource> pending_raster_source =
  3710. FakeRasterSource::CreateFilled(layer_bounds);
  3711. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  3712. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  3713. ActivateTree();
  3714. LayerImpl* layer1 = AddLayer<LayerImpl>(host_impl()->active_tree());
  3715. layer1->SetBounds(layer_bounds);
  3716. layer1->SetDrawsContent(true);
  3717. layer1->SetContentsOpaque(true);
  3718. CopyProperties(active_layer(), layer1);
  3719. layer1->SetOffsetToTransformParent(occluding_layer_position);
  3720. ASSERT_TRUE(active_layer()->CanHaveTilings());
  3721. active_layer()->SetContentsScaleForTesting(scale);
  3722. active_layer()->tilings()->RemoveAllTilings();
  3723. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  3724. active_layer()
  3725. ->AddTiling(gfx::AxisTransform2d(low_res_factor, gfx::Vector2dF()))
  3726. ->set_resolution(LOW_RESOLUTION);
  3727. active_layer()
  3728. ->AddTiling(gfx::AxisTransform2d(scale, gfx::Vector2dF()))
  3729. ->set_resolution(HIGH_RESOLUTION);
  3730. ASSERT_EQ(2u, active_layer()->num_tilings());
  3731. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  3732. // UpdateDrawProperties with the occluding layer.
  3733. UpdateDrawProperties(host_impl()->active_tree());
  3734. ASSERT_EQ(2u, active_layer()->num_tilings());
  3735. int occluded_tile_count = 0;
  3736. for (size_t i = 0; i < active_layer()->num_tilings(); ++i) {
  3737. PictureLayerTiling* tiling = active_layer()->tilings()->tiling_at(i);
  3738. auto prioritized_tiles =
  3739. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  3740. std::vector<Tile*> tiles = tiling->AllTilesForTesting();
  3741. occluded_tile_count = 0;
  3742. for (size_t j = 0; j < tiles.size(); ++j) {
  3743. if (prioritized_tiles[tiles[j]].is_occluded()) {
  3744. gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
  3745. tiles[j]->content_rect(), 1.f / tiles[j]->contents_scale_key());
  3746. EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
  3747. occluded_tile_count++;
  3748. }
  3749. }
  3750. if (i == 0) {
  3751. EXPECT_EQ(scale, tiling->contents_scale_key());
  3752. EXPECT_EQ(occluded_tile_count, expected_occluded_count);
  3753. } else {
  3754. ASSERT_EQ(1u, i);
  3755. EXPECT_EQ(occluded_tile_count, 2);
  3756. }
  3757. }
  3758. }
  3759. TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForScale0_3) {
  3760. TestOcclusionForScale(0.3f, 2);
  3761. }
  3762. TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForScale0_7) {
  3763. TestOcclusionForScale(0.7f, 4);
  3764. }
  3765. TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForScale1) {
  3766. TestOcclusionForScale(1.0f, 5);
  3767. }
  3768. TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForScale2) {
  3769. TestOcclusionForScale(2.0f, 30);
  3770. }
  3771. TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
  3772. gfx::Size layer_bounds(1000, 1000);
  3773. gfx::Size viewport_size(1000, 1000);
  3774. gfx::Vector2dF occluding_layer_position(310.f, 0.f);
  3775. gfx::Rect invalidation_rect(230, 230, 102, 102);
  3776. scoped_refptr<FakeRasterSource> pending_raster_source =
  3777. FakeRasterSource::CreateFilled(layer_bounds);
  3778. scoped_refptr<FakeRasterSource> active_raster_source =
  3779. FakeRasterSource::CreateFilled(layer_bounds);
  3780. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  3781. SetupPendingTree(active_raster_source);
  3782. // Partially occlude the active layer.
  3783. LayerImpl* layer1 = AddLayer<LayerImpl>(host_impl()->pending_tree());
  3784. layer1->SetBounds(layer_bounds);
  3785. layer1->SetDrawsContent(true);
  3786. layer1->SetContentsOpaque(true);
  3787. CopyProperties(pending_layer(), layer1);
  3788. layer1->SetOffsetToTransformParent(occluding_layer_position);
  3789. ActivateTree();
  3790. for (size_t i = 0; i < active_layer()->num_tilings(); ++i) {
  3791. PictureLayerTiling* tiling = active_layer()->tilings()->tiling_at(i);
  3792. auto prioritized_tiles =
  3793. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  3794. for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
  3795. gfx::Rect(layer_bounds));
  3796. iter; ++iter) {
  3797. if (!*iter)
  3798. continue;
  3799. const Tile* tile = *iter;
  3800. gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
  3801. tile->content_rect(), 1.f / tile->contents_scale_key());
  3802. // Tiles are occluded on the active tree iff they lie beneath the
  3803. // occluding layer.
  3804. EXPECT_EQ(prioritized_tiles[tile].is_occluded(),
  3805. scaled_content_rect.x() >= occluding_layer_position.x());
  3806. }
  3807. }
  3808. // Partially invalidate the pending layer.
  3809. SetupPendingTreeWithInvalidation(pending_raster_source, invalidation_rect);
  3810. for (size_t i = 0; i < active_layer()->num_tilings(); ++i) {
  3811. PictureLayerTiling* tiling = active_layer()->tilings()->tiling_at(i);
  3812. auto prioritized_tiles =
  3813. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  3814. for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f,
  3815. gfx::Rect(layer_bounds));
  3816. iter; ++iter) {
  3817. if (!*iter)
  3818. continue;
  3819. const Tile* tile = *iter;
  3820. EXPECT_TRUE(tile);
  3821. // All tiles are unoccluded, because the pending tree has no occlusion.
  3822. EXPECT_FALSE(prioritized_tiles[tile].is_occluded());
  3823. if (tiling->resolution() == LOW_RESOLUTION) {
  3824. EXPECT_FALSE(active_layer()->GetPendingOrActiveTwinTiling(tiling));
  3825. continue;
  3826. }
  3827. Tile* twin_tile =
  3828. active_layer()->GetPendingOrActiveTwinTiling(tiling)->TileAt(
  3829. iter.i(), iter.j());
  3830. gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
  3831. tile->content_rect(), 1.f / tile->contents_scale_key());
  3832. if (scaled_content_rect.Intersects(invalidation_rect)) {
  3833. // Tiles inside the invalidation rect exist on both trees.
  3834. EXPECT_TRUE(twin_tile);
  3835. EXPECT_NE(tile, twin_tile);
  3836. } else {
  3837. // Tiles outside the invalidation rect only exist on the active tree.
  3838. EXPECT_FALSE(twin_tile);
  3839. }
  3840. }
  3841. }
  3842. }
  3843. TEST_F(OcclusionTrackingPictureLayerImplTest,
  3844. OccludedTilesConsideredDuringEviction) {
  3845. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  3846. gfx::Size tile_size(102, 102);
  3847. gfx::Size layer_bounds(1000, 1000);
  3848. gfx::Size viewport_size(1000, 1000);
  3849. gfx::Vector2dF pending_occluding_layer_position(310.f, 0.f);
  3850. gfx::Vector2dF active_occluding_layer_position(0.f, 310.f);
  3851. gfx::Rect invalidation_rect(230, 230, 152, 152);
  3852. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  3853. SetInitialDeviceScaleFactor(2.f);
  3854. scoped_refptr<FakeRasterSource> pending_raster_source =
  3855. FakeRasterSource::CreateFilled(layer_bounds);
  3856. scoped_refptr<FakeRasterSource> active_raster_source =
  3857. FakeRasterSource::CreateFilled(layer_bounds);
  3858. SetupPendingTreeWithFixedTileSize(active_raster_source, tile_size, Region());
  3859. // Partially occlude the active layer.
  3860. LayerImpl* active_occluding_layer =
  3861. AddLayer<LayerImpl>(host_impl()->pending_tree());
  3862. active_occluding_layer->SetBounds(layer_bounds);
  3863. active_occluding_layer->SetDrawsContent(true);
  3864. active_occluding_layer->SetContentsOpaque(true);
  3865. CopyProperties(pending_layer(), active_occluding_layer);
  3866. active_occluding_layer->SetOffsetToTransformParent(
  3867. active_occluding_layer_position);
  3868. ActivateTree();
  3869. // Partially invalidate the pending layer. Tiles inside the invalidation rect
  3870. // are created.
  3871. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size,
  3872. invalidation_rect);
  3873. // Partially occlude the pending layer in a different way.
  3874. LayerImpl* pending_occluding_layer =
  3875. host_impl()->pending_tree()->LayerById(active_occluding_layer->id());
  3876. ASSERT_EQ(active_occluding_layer->bounds(),
  3877. pending_occluding_layer->bounds());
  3878. ASSERT_TRUE(pending_occluding_layer->draws_content());
  3879. ASSERT_TRUE(pending_occluding_layer->contents_opaque());
  3880. pending_occluding_layer->SetOffsetToTransformParent(
  3881. pending_occluding_layer_position);
  3882. pending_occluding_layer->NoteLayerPropertyChanged();
  3883. EXPECT_EQ(1u, pending_layer()->num_tilings());
  3884. EXPECT_EQ(2u, active_layer()->num_tilings());
  3885. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  3886. // UpdateDrawProperties with the occluding layer.
  3887. UpdateDrawProperties(host_impl()->pending_tree());
  3888. float dest_scale = std::max(active_layer()->MaximumTilingContentsScale(),
  3889. pending_layer()->MaximumTilingContentsScale());
  3890. gfx::Rect dest_layer_bounds =
  3891. gfx::ScaleToEnclosingRect(gfx::Rect(layer_bounds), dest_scale);
  3892. gfx::Rect dest_invalidation_rect =
  3893. gfx::ScaleToEnclosingRect(invalidation_rect, dest_scale);
  3894. // The expected number of occluded tiles on each of the 2 tilings for each of
  3895. // the 3 tree priorities.
  3896. size_t expected_occluded_tile_count_on_pending[] = {4u, 0u};
  3897. size_t expected_occluded_tile_count_on_active[] = {12u, 3u};
  3898. size_t total_expected_occluded_tile_count_on_trees[] = {15u, 4u};
  3899. // Verify number of occluded tiles on the pending layer for each tiling.
  3900. for (size_t i = 0; i < pending_layer()->num_tilings(); ++i) {
  3901. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(i);
  3902. auto prioritized_tiles =
  3903. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  3904. size_t occluded_tile_count_on_pending = 0u;
  3905. for (PictureLayerTiling::CoverageIterator iter(tiling, dest_scale,
  3906. dest_layer_bounds);
  3907. iter; ++iter) {
  3908. Tile* tile = *iter;
  3909. if (dest_invalidation_rect.Intersects(iter.geometry_rect()))
  3910. EXPECT_TRUE(tile);
  3911. else
  3912. EXPECT_FALSE(tile);
  3913. if (!tile)
  3914. continue;
  3915. if (prioritized_tiles[tile].is_occluded())
  3916. occluded_tile_count_on_pending++;
  3917. }
  3918. EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
  3919. occluded_tile_count_on_pending)
  3920. << tiling->contents_scale_key();
  3921. }
  3922. // Verify number of occluded tiles on the active layer for each tiling.
  3923. for (size_t i = 0; i < active_layer()->num_tilings(); ++i) {
  3924. PictureLayerTiling* tiling = active_layer()->tilings()->tiling_at(i);
  3925. auto prioritized_tiles =
  3926. tiling->UpdateAndGetAllPrioritizedTilesForTesting();
  3927. size_t occluded_tile_count_on_active = 0u;
  3928. for (PictureLayerTiling::CoverageIterator iter(tiling, dest_scale,
  3929. dest_layer_bounds);
  3930. iter; ++iter) {
  3931. Tile* tile = *iter;
  3932. if (!tile)
  3933. continue;
  3934. if (prioritized_tiles[tile].is_occluded())
  3935. occluded_tile_count_on_active++;
  3936. }
  3937. EXPECT_EQ(expected_occluded_tile_count_on_active[i],
  3938. occluded_tile_count_on_active)
  3939. << i;
  3940. }
  3941. std::vector<Tile*> all_tiles;
  3942. for (size_t i = 0; i < pending_layer()->num_tilings(); ++i) {
  3943. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(i);
  3944. std::vector<Tile*> tiles = tiling->AllTilesForTesting();
  3945. all_tiles.insert(all_tiles.end(), tiles.begin(), tiles.end());
  3946. }
  3947. for (size_t i = 0; i < active_layer()->num_tilings(); ++i) {
  3948. PictureLayerTiling* tiling = active_layer()->tilings()->tiling_at(i);
  3949. std::vector<Tile*> tiles = tiling->AllTilesForTesting();
  3950. all_tiles.insert(all_tiles.end(), tiles.begin(), tiles.end());
  3951. }
  3952. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  3953. all_tiles);
  3954. VerifyEvictionConsidersOcclusion(
  3955. pending_layer(), PENDING_TREE,
  3956. total_expected_occluded_tile_count_on_trees[PENDING_TREE], __LINE__);
  3957. VerifyEvictionConsidersOcclusion(
  3958. active_layer(), ACTIVE_TREE,
  3959. total_expected_occluded_tile_count_on_trees[ACTIVE_TREE], __LINE__);
  3960. // Repeat the tests without valid active tree priorities.
  3961. active_layer()->set_has_valid_tile_priorities(false);
  3962. VerifyEvictionConsidersOcclusion(
  3963. pending_layer(), PENDING_TREE,
  3964. total_expected_occluded_tile_count_on_trees[PENDING_TREE], __LINE__);
  3965. VerifyEvictionConsidersOcclusion(
  3966. active_layer(), ACTIVE_TREE,
  3967. total_expected_occluded_tile_count_on_trees[ACTIVE_TREE], __LINE__);
  3968. active_layer()->set_has_valid_tile_priorities(true);
  3969. // Repeat the tests without valid pending tree priorities.
  3970. pending_layer()->set_has_valid_tile_priorities(false);
  3971. VerifyEvictionConsidersOcclusion(
  3972. active_layer(), ACTIVE_TREE,
  3973. total_expected_occluded_tile_count_on_trees[ACTIVE_TREE], __LINE__);
  3974. VerifyEvictionConsidersOcclusion(
  3975. pending_layer(), PENDING_TREE,
  3976. total_expected_occluded_tile_count_on_trees[PENDING_TREE], __LINE__);
  3977. pending_layer()->set_has_valid_tile_priorities(true);
  3978. }
  3979. TEST_F(LegacySWPictureLayerImplTest, PendingOrActiveTwinLayer) {
  3980. gfx::Size layer_bounds(1000, 1000);
  3981. scoped_refptr<FakeRasterSource> raster_source =
  3982. FakeRasterSource::CreateFilled(layer_bounds);
  3983. SetupPendingTree(raster_source);
  3984. EXPECT_FALSE(pending_layer()->GetPendingOrActiveTwinLayer());
  3985. ActivateTree();
  3986. EXPECT_FALSE(active_layer()->GetPendingOrActiveTwinLayer());
  3987. SetupPendingTree(raster_source);
  3988. EXPECT_TRUE(pending_layer()->GetPendingOrActiveTwinLayer());
  3989. EXPECT_TRUE(active_layer()->GetPendingOrActiveTwinLayer());
  3990. EXPECT_EQ(pending_layer(), active_layer()->GetPendingOrActiveTwinLayer());
  3991. EXPECT_EQ(active_layer(), pending_layer()->GetPendingOrActiveTwinLayer());
  3992. ActivateTree();
  3993. EXPECT_FALSE(active_layer()->GetPendingOrActiveTwinLayer());
  3994. // Make an empty pending tree.
  3995. host_impl()->CreatePendingTree();
  3996. host_impl()->pending_tree()->DetachLayers();
  3997. EXPECT_FALSE(active_layer()->GetPendingOrActiveTwinLayer());
  3998. }
  3999. void GetClientDataAndUpdateInvalidation(RecordingSource* recording_source,
  4000. FakeContentLayerClient* client,
  4001. Region invalidation,
  4002. gfx::Size layer_bounds) {
  4003. gfx::Rect new_recorded_viewport = client->PaintableRegion();
  4004. scoped_refptr<DisplayItemList> display_list =
  4005. client->PaintContentsToDisplayList();
  4006. recording_source->UpdateAndExpandInvalidation(&invalidation, layer_bounds,
  4007. new_recorded_viewport);
  4008. recording_source->UpdateDisplayItemList(display_list,
  4009. 1.f /** recording_scale_factor */);
  4010. }
  4011. void PictureLayerImplTest::TestQuadsForSolidColor(bool test_for_solid,
  4012. bool partial_opaque) {
  4013. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  4014. gfx::Size tile_size(100, 100);
  4015. gfx::Size layer_bounds(200, 200);
  4016. gfx::Rect layer_rect(layer_bounds);
  4017. FakeContentLayerClient client;
  4018. client.set_bounds(layer_bounds);
  4019. scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
  4020. FakeLayerTreeHostClient host_client;
  4021. TestTaskGraphRunner task_graph_runner;
  4022. auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  4023. std::unique_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(
  4024. &host_client, &task_graph_runner, animation_host.get());
  4025. host->SetRootLayer(layer);
  4026. RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
  4027. client.set_fill_with_nonsolid_color(!test_for_solid);
  4028. PaintFlags flags;
  4029. flags.setColor(SK_ColorRED);
  4030. if (test_for_solid)
  4031. client.add_draw_rect(layer_rect, flags);
  4032. Region invalidation(layer_rect);
  4033. GetClientDataAndUpdateInvalidation(recording_source, &client, invalidation,
  4034. layer_bounds);
  4035. scoped_refptr<RasterSource> pending_raster_source =
  4036. recording_source->CreateRasterSource();
  4037. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size, Region());
  4038. ActivateTree();
  4039. if (test_for_solid) {
  4040. EXPECT_EQ(0u, active_layer()->tilings()->num_tilings());
  4041. } else {
  4042. ASSERT_TRUE(active_layer()->tilings());
  4043. ASSERT_GT(active_layer()->tilings()->num_tilings(), 0u);
  4044. std::vector<Tile*> tiles =
  4045. active_layer()->HighResTiling()->AllTilesForTesting();
  4046. EXPECT_FALSE(tiles.empty());
  4047. std::vector<Tile*> resource_tiles;
  4048. if (!partial_opaque) {
  4049. resource_tiles = tiles;
  4050. } else {
  4051. size_t i = 0;
  4052. for (auto it = tiles.begin(); it != tiles.end(); ++it, ++i) {
  4053. if (i < 5) {
  4054. TileDrawInfo& draw_info = (*it)->draw_info();
  4055. draw_info.SetSolidColorForTesting(SkColors::kTransparent);
  4056. } else {
  4057. resource_tiles.push_back(*it);
  4058. }
  4059. }
  4060. }
  4061. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  4062. resource_tiles);
  4063. }
  4064. auto render_pass = viz::CompositorRenderPass::Create();
  4065. AppendQuadsData data;
  4066. active_layer()->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  4067. active_layer()->AppendQuads(render_pass.get(), &data);
  4068. active_layer()->DidDraw(nullptr);
  4069. // Transparent quads should be eliminated.
  4070. if (partial_opaque)
  4071. EXPECT_EQ(4u, render_pass->quad_list.size());
  4072. viz::DrawQuad::Material expected =
  4073. test_for_solid ? viz::DrawQuad::Material::kSolidColor
  4074. : viz::DrawQuad::Material::kTiledContent;
  4075. EXPECT_EQ(expected, render_pass->quad_list.front()->material);
  4076. }
  4077. TEST_F(LegacySWPictureLayerImplTest, DrawSolidQuads) {
  4078. TestQuadsForSolidColor(true, false);
  4079. }
  4080. TEST_F(LegacySWPictureLayerImplTest, DrawNonSolidQuads) {
  4081. TestQuadsForSolidColor(false, false);
  4082. }
  4083. TEST_F(LegacySWPictureLayerImplTest, DrawTransparentQuads) {
  4084. TestQuadsForSolidColor(false, true);
  4085. }
  4086. TEST_F(LegacySWPictureLayerImplTest, NonSolidToSolidNoTilings) {
  4087. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  4088. gfx::Size layer_bounds(200, 200);
  4089. gfx::Rect layer_rect(layer_bounds);
  4090. FakeContentLayerClient client;
  4091. client.set_bounds(layer_bounds);
  4092. scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
  4093. FakeLayerTreeHostClient host_client;
  4094. TestTaskGraphRunner task_graph_runner;
  4095. auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  4096. std::unique_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(
  4097. &host_client, &task_graph_runner, animation_host.get());
  4098. host->SetRootLayer(layer);
  4099. RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
  4100. client.set_fill_with_nonsolid_color(true);
  4101. recording_source->SetNeedsDisplayRect(layer_rect);
  4102. Region invalidation1;
  4103. GetClientDataAndUpdateInvalidation(recording_source, &client, invalidation1,
  4104. layer_bounds);
  4105. scoped_refptr<RasterSource> raster_source1 =
  4106. recording_source->CreateRasterSource();
  4107. SetupPendingTree(raster_source1);
  4108. ActivateTree();
  4109. // We've started with a solid layer that contains some tilings.
  4110. ASSERT_TRUE(active_layer()->tilings());
  4111. EXPECT_NE(0u, active_layer()->tilings()->num_tilings());
  4112. client.set_fill_with_nonsolid_color(false);
  4113. recording_source->SetNeedsDisplayRect(layer_rect);
  4114. Region invalidation2;
  4115. GetClientDataAndUpdateInvalidation(recording_source, &client, invalidation2,
  4116. layer_bounds);
  4117. scoped_refptr<RasterSource> raster_source2 =
  4118. recording_source->CreateRasterSource();
  4119. SetupPendingTree(raster_source2);
  4120. ActivateTree();
  4121. // We've switched to a solid color, so we should end up with no tilings.
  4122. ASSERT_TRUE(active_layer()->tilings());
  4123. EXPECT_EQ(0u, active_layer()->tilings()->num_tilings());
  4124. }
  4125. TEST_F(LegacySWPictureLayerImplTest, ChangeInViewportAllowsTilingUpdates) {
  4126. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  4127. gfx::Size layer_bounds(400, 4000);
  4128. SetupDefaultTrees(layer_bounds);
  4129. Region invalidation;
  4130. gfx::Rect viewport = gfx::Rect(0, 0, 100, 100);
  4131. gfx::Transform transform;
  4132. host_impl()->SetRequiresHighResToDraw();
  4133. // Update tiles.
  4134. pending_layer()->draw_properties().visible_layer_rect = viewport;
  4135. pending_layer()->draw_properties().screen_space_transform = transform;
  4136. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.f, 1.f, 1.f);
  4137. pending_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  4138. // Ensure we can't activate.
  4139. EXPECT_FALSE(host_impl()->tile_manager()->IsReadyToActivate());
  4140. // Now in the same frame, move the viewport (this can happen during
  4141. // animation).
  4142. viewport = gfx::Rect(0, 2000, 100, 100);
  4143. // Update tiles.
  4144. pending_layer()->draw_properties().visible_layer_rect = viewport;
  4145. pending_layer()->draw_properties().screen_space_transform = transform;
  4146. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.f, 1.f, 1.f);
  4147. pending_layer()->HighResTiling()->UpdateAllRequiredStateForTesting();
  4148. // Make sure all viewport tiles (viewport from the tiling) are ready to draw.
  4149. std::vector<Tile*> tiles;
  4150. for (PictureLayerTiling::CoverageIterator iter(
  4151. pending_layer()->HighResTiling(), 1.f,
  4152. pending_layer()->HighResTiling()->GetCurrentVisibleRectForTesting());
  4153. iter; ++iter) {
  4154. if (*iter)
  4155. tiles.push_back(*iter);
  4156. }
  4157. for (PictureLayerTiling::CoverageIterator iter(
  4158. active_layer()->HighResTiling(), 1.f,
  4159. active_layer()->HighResTiling()->GetCurrentVisibleRectForTesting());
  4160. iter; ++iter) {
  4161. if (*iter)
  4162. tiles.push_back(*iter);
  4163. }
  4164. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
  4165. // Ensure we can activate.
  4166. EXPECT_TRUE(host_impl()->tile_manager()->IsReadyToActivate());
  4167. }
  4168. TEST_F(LegacySWPictureLayerImplTest, CloneMissingRecordings) {
  4169. gfx::Size tile_size(100, 100);
  4170. gfx::Size layer_bounds(400, 400);
  4171. scoped_refptr<FakeRasterSource> filled_raster_source =
  4172. FakeRasterSource::CreateFilled(layer_bounds);
  4173. scoped_refptr<FakeRasterSource> partial_raster_source =
  4174. FakeRasterSource::CreatePartiallyFilled(layer_bounds,
  4175. gfx::Rect(100, 100, 300, 300));
  4176. SetupPendingTreeWithFixedTileSize(filled_raster_source, tile_size, Region());
  4177. ActivateTree();
  4178. PictureLayerTiling* pending_tiling = old_pending_layer()->HighResTiling();
  4179. PictureLayerTiling* active_tiling = active_layer()->HighResTiling();
  4180. // We should have all tiles on active, and none on pending.
  4181. EXPECT_EQ(0u, pending_tiling->AllTilesForTesting().size());
  4182. EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
  4183. // Now put a partially-recorded raster source on the pending tree (and
  4184. // invalidate everything, since the main thread recording will invalidate
  4185. // dropped recordings). This will cause us to be missing some tiles.
  4186. SetupPendingTreeWithFixedTileSize(partial_raster_source, tile_size,
  4187. Region(gfx::Rect(layer_bounds)));
  4188. EXPECT_EQ(4u * 4u, pending_tiling->AllTilesForTesting().size());
  4189. EXPECT_FALSE(pending_tiling->TileAt(0, 0));
  4190. EXPECT_TRUE(pending_tiling->TileAt(1, 1));
  4191. EXPECT_TRUE(pending_tiling->TileAt(2, 2));
  4192. // Active is not affected yet.
  4193. EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
  4194. // Activate the tree. The same tiles go missing on the active tree.
  4195. ActivateTree();
  4196. EXPECT_EQ(4u * 4u, active_tiling->AllTilesForTesting().size());
  4197. EXPECT_FALSE(active_tiling->TileAt(0, 0));
  4198. EXPECT_TRUE(active_tiling->TileAt(1, 1));
  4199. EXPECT_TRUE(active_tiling->TileAt(2, 2));
  4200. // Now put a full recording on the pending tree again. We'll get all our tiles
  4201. // back.
  4202. SetupPendingTreeWithFixedTileSize(filled_raster_source, tile_size,
  4203. Region(gfx::Rect(layer_bounds)));
  4204. EXPECT_EQ(5u * 5u, pending_tiling->AllTilesForTesting().size());
  4205. Tile::Id tile00 = pending_tiling->TileAt(0, 0)->id();
  4206. Tile::Id tile11 = pending_tiling->TileAt(1, 1)->id();
  4207. Tile::Id tile22 = pending_tiling->TileAt(2, 2)->id();
  4208. // Active is not affected yet.
  4209. EXPECT_EQ(4u * 4u, active_tiling->AllTilesForTesting().size());
  4210. // Activate the tree. The tiles are moved to the active tree.
  4211. ActivateTree();
  4212. EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
  4213. EXPECT_EQ(tile00, active_tiling->TileAt(0, 0)->id());
  4214. EXPECT_EQ(tile11, active_tiling->TileAt(1, 1)->id());
  4215. EXPECT_EQ(tile22, active_tiling->TileAt(2, 2)->id());
  4216. }
  4217. TEST_F(LegacySWPictureLayerImplTest,
  4218. DirectlyCompositedImageRasterSourceCoverage) {
  4219. gfx::Size tile_size(100, 100);
  4220. gfx::Size layer_bounds(400, 400);
  4221. scoped_refptr<FakeRasterSource> filled_raster_source =
  4222. FakeRasterSource::CreateFilled(layer_bounds);
  4223. scoped_refptr<FakeRasterSource> partial_raster_source =
  4224. FakeRasterSource::CreatePartiallyFilled(layer_bounds,
  4225. gfx::Rect(150, 150, 100, 100));
  4226. SetupPendingTreeWithFixedTileSize(filled_raster_source, tile_size, Region());
  4227. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4228. gfx::Vector2dF(1, 1));
  4229. ActivateTree();
  4230. PictureLayerTiling* pending_tiling = old_pending_layer()->HighResTiling();
  4231. PictureLayerTiling* active_tiling = active_layer()->HighResTiling();
  4232. // We should have all tiles on active, and none on pending.
  4233. EXPECT_EQ(0u, pending_tiling->AllTilesForTesting().size());
  4234. EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
  4235. // Now put a partially-recorded raster source on the pending tree (and
  4236. // invalidate everything, since the main thread recording will invalidate
  4237. // dropped recordings). Because the layer is a directly composited image, all
  4238. // tiles should be created.
  4239. SetupPendingTreeWithFixedTileSize(partial_raster_source, tile_size,
  4240. Region(gfx::Rect(layer_bounds)));
  4241. EXPECT_EQ(5u * 5u, pending_tiling->AllTilesForTesting().size());
  4242. // Activate the tree. The same tiles should have been moved to active tree.
  4243. EXPECT_EQ(5u * 5u, pending_tiling->AllTilesForTesting().size());
  4244. Tile::Id tile00 = pending_tiling->TileAt(0, 0)->id();
  4245. Tile::Id tile11 = pending_tiling->TileAt(1, 1)->id();
  4246. Tile::Id tile22 = pending_tiling->TileAt(2, 2)->id();
  4247. // Activate the tree. The tiles are moved to the active tree.
  4248. ActivateTree();
  4249. EXPECT_EQ(5u * 5u, active_tiling->AllTilesForTesting().size());
  4250. EXPECT_EQ(tile00, active_tiling->TileAt(0, 0)->id());
  4251. EXPECT_EQ(tile11, active_tiling->TileAt(1, 1)->id());
  4252. EXPECT_EQ(tile22, active_tiling->TileAt(2, 2)->id());
  4253. }
  4254. TEST_F(LegacySWPictureLayerImplTest, ScrollPastLiveTilesRectAndBack) {
  4255. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  4256. gfx::Size tile_size(102, 102);
  4257. gfx::Size layer_bounds(100, 100);
  4258. gfx::Size viewport_size(100, 100);
  4259. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  4260. SetInitialDeviceScaleFactor(1.f);
  4261. scoped_refptr<FakeRasterSource> pending_raster_source =
  4262. FakeRasterSource::CreateFilled(layer_bounds);
  4263. scoped_refptr<FakeRasterSource> active_raster_source =
  4264. FakeRasterSource::CreateFilled(layer_bounds);
  4265. SetupPendingTreeWithFixedTileSize(active_raster_source, tile_size, Region());
  4266. ActivateTree();
  4267. EXPECT_TRUE(active_layer()->HighResTiling()->has_tiles());
  4268. host_impl()->SetExternalTilePriorityConstraints(gfx::Rect(0, 5000, 100, 100),
  4269. gfx::Transform());
  4270. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size,
  4271. gfx::Rect());
  4272. EXPECT_FALSE(pending_layer()->HighResTiling()->has_tiles());
  4273. EXPECT_TRUE(pending_layer()->HighResTiling()->live_tiles_rect().IsEmpty());
  4274. ActivateTree();
  4275. EXPECT_FALSE(active_layer()->HighResTiling()->has_tiles());
  4276. EXPECT_TRUE(active_layer()->HighResTiling()->live_tiles_rect().IsEmpty());
  4277. host_impl()->SetExternalTilePriorityConstraints(gfx::Rect(0, 110, 100, 100),
  4278. gfx::Transform());
  4279. SetupPendingTreeWithFixedTileSize(pending_raster_source, tile_size,
  4280. gfx::Rect());
  4281. EXPECT_FALSE(pending_layer()->HighResTiling()->has_tiles());
  4282. EXPECT_FALSE(pending_layer()->HighResTiling()->live_tiles_rect().IsEmpty());
  4283. ActivateTree();
  4284. EXPECT_TRUE(active_layer()->HighResTiling()->has_tiles());
  4285. EXPECT_FALSE(active_layer()->HighResTiling()->live_tiles_rect().IsEmpty());
  4286. }
  4287. TEST_F(LegacySWPictureLayerImplTest, ScrollPropagatesToPending) {
  4288. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  4289. gfx::Size layer_bounds(1000, 1000);
  4290. gfx::Size viewport_size(100, 100);
  4291. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(viewport_size));
  4292. SetInitialDeviceScaleFactor(1.f);
  4293. SetupDefaultTrees(layer_bounds);
  4294. active_layer()->SetCurrentScrollOffset(gfx::PointF(0.0, 50.0));
  4295. UpdateDrawProperties(host_impl()->active_tree());
  4296. EXPECT_EQ("0,50 100x100", active_layer()
  4297. ->HighResTiling()
  4298. ->GetCurrentVisibleRectForTesting()
  4299. .ToString());
  4300. EXPECT_EQ("0,0 100x100", pending_layer()
  4301. ->HighResTiling()
  4302. ->GetCurrentVisibleRectForTesting()
  4303. .ToString());
  4304. // Scroll offset in property trees is not propagated from the active tree to
  4305. // the pending tree.
  4306. SetScrollOffset(pending_layer(), CurrentScrollOffset(active_layer()));
  4307. UpdateDrawProperties(host_impl()->pending_tree());
  4308. EXPECT_EQ("0,50 100x100", pending_layer()
  4309. ->HighResTiling()
  4310. ->GetCurrentVisibleRectForTesting()
  4311. .ToString());
  4312. }
  4313. TEST_F(LegacySWPictureLayerImplTest, UpdateLCDTextInvalidatesPendingTree) {
  4314. gfx::Size layer_bounds(100, 100);
  4315. SetupPendingTree(FakeRasterSource::CreateFilledWithText(layer_bounds));
  4316. EXPECT_TRUE(pending_layer()->can_use_lcd_text());
  4317. EXPECT_TRUE(pending_layer()->HighResTiling()->has_tiles());
  4318. std::vector<Tile*> tiles =
  4319. pending_layer()->HighResTiling()->AllTilesForTesting();
  4320. for (Tile* tile : tiles)
  4321. EXPECT_TRUE(tile->can_use_lcd_text());
  4322. pending_layer()->SetContentsOpaque(false);
  4323. pending_layer()->UpdateTiles();
  4324. EXPECT_FALSE(pending_layer()->can_use_lcd_text());
  4325. EXPECT_TRUE(pending_layer()->HighResTiling()->has_tiles());
  4326. for (Tile* tile : pending_layer()->HighResTiling()->AllTilesForTesting())
  4327. EXPECT_FALSE(tile->can_use_lcd_text());
  4328. // Change of the specific LCD text disallowed reason should not invalidate
  4329. // tilings.
  4330. pending_layer()->SetContentsOpaque(true);
  4331. FilterOperations blur_filter;
  4332. blur_filter.Append(FilterOperation::CreateBlurFilter(4.0f));
  4333. SetFilter(pending_layer(), blur_filter);
  4334. UpdateDrawProperties(host_impl()->pending_tree());
  4335. EXPECT_FALSE(pending_layer()->can_use_lcd_text());
  4336. EXPECT_TRUE(pending_layer()->HighResTiling()->has_tiles());
  4337. for (Tile* tile : pending_layer()->HighResTiling()->AllTilesForTesting())
  4338. EXPECT_FALSE(tile->can_use_lcd_text());
  4339. SetFilter(pending_layer(), FilterOperations());
  4340. UpdateDrawProperties(host_impl()->pending_tree());
  4341. EXPECT_TRUE(pending_layer()->can_use_lcd_text());
  4342. EXPECT_TRUE(pending_layer()->HighResTiling()->has_tiles());
  4343. for (Tile* tile : pending_layer()->HighResTiling()->AllTilesForTesting())
  4344. EXPECT_TRUE(tile->can_use_lcd_text());
  4345. }
  4346. TEST_F(LegacySWPictureLayerImplTest, UpdateLCDTextPushToActiveTree) {
  4347. SetupPendingTree(FakeRasterSource::CreateFilledWithText(gfx::Size(200, 200)));
  4348. float page_scale = 4.f;
  4349. SetupDrawPropertiesAndUpdateTiles(pending_layer(), page_scale, 1.0f,
  4350. page_scale);
  4351. EXPECT_TRUE(pending_layer()->can_use_lcd_text());
  4352. EXPECT_TRUE(pending_layer()->HighResTiling()->can_use_lcd_text());
  4353. ActivateTree();
  4354. EXPECT_TRUE(active_layer()->can_use_lcd_text());
  4355. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  4356. ASSERT_TRUE(active_layer()->HighResTiling()->has_tiles());
  4357. std::vector<Tile*> tiles =
  4358. active_layer()->HighResTiling()->AllTilesForTesting();
  4359. for (Tile* tile : tiles)
  4360. EXPECT_TRUE(tile->can_use_lcd_text());
  4361. for (Tile* tile : active_layer()->LowResTiling()->AllTilesForTesting())
  4362. EXPECT_FALSE(tile->can_use_lcd_text());
  4363. SetupPendingTree(FakeRasterSource::CreateFilledWithText(gfx::Size(200, 200)));
  4364. SetupDrawPropertiesAndUpdateTiles(pending_layer(), page_scale, 1.0f,
  4365. page_scale);
  4366. pending_layer()->SetContentsOpaque(false);
  4367. pending_layer()->UpdateTiles();
  4368. EXPECT_FALSE(pending_layer()->can_use_lcd_text());
  4369. EXPECT_FALSE(pending_layer()->HighResTiling()->can_use_lcd_text());
  4370. ActivateTree();
  4371. EXPECT_FALSE(active_layer()->can_use_lcd_text());
  4372. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  4373. ASSERT_TRUE(active_layer()->HighResTiling()->has_tiles());
  4374. for (Tile* tile : active_layer()->HighResTiling()->AllTilesForTesting())
  4375. EXPECT_FALSE(tile->can_use_lcd_text());
  4376. for (Tile* tile : active_layer()->LowResTiling()->AllTilesForTesting())
  4377. EXPECT_FALSE(tile->can_use_lcd_text());
  4378. }
  4379. TEST_F(LegacySWPictureLayerImplTest, UpdateLCDTextPushToActiveTreeWith2dScale) {
  4380. SetupPendingTree(FakeRasterSource::CreateFilledWithText(gfx::Size(200, 200)));
  4381. gfx::Vector2dF ideal_scale(4.f, 2.f);
  4382. float page_scale = 4.f;
  4383. SetupDrawPropertiesAndUpdateTiles(pending_layer(), ideal_scale, 1.0f,
  4384. page_scale);
  4385. EXPECT_TRUE(pending_layer()->can_use_lcd_text());
  4386. EXPECT_TRUE(pending_layer()->HighResTiling()->can_use_lcd_text());
  4387. ActivateTree();
  4388. EXPECT_TRUE(active_layer()->can_use_lcd_text());
  4389. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  4390. ASSERT_TRUE(active_layer()->HighResTiling()->has_tiles());
  4391. std::vector<Tile*> tiles =
  4392. active_layer()->HighResTiling()->AllTilesForTesting();
  4393. for (Tile* tile : tiles)
  4394. EXPECT_TRUE(tile->can_use_lcd_text());
  4395. for (Tile* tile : active_layer()->LowResTiling()->AllTilesForTesting())
  4396. EXPECT_FALSE(tile->can_use_lcd_text());
  4397. SetupPendingTree(FakeRasterSource::CreateFilledWithText(gfx::Size(200, 200)));
  4398. SetupDrawPropertiesAndUpdateTiles(pending_layer(), ideal_scale, 1.0f,
  4399. page_scale);
  4400. pending_layer()->SetContentsOpaque(false);
  4401. pending_layer()->UpdateTiles();
  4402. EXPECT_FALSE(pending_layer()->can_use_lcd_text());
  4403. EXPECT_FALSE(pending_layer()->HighResTiling()->can_use_lcd_text());
  4404. ActivateTree();
  4405. EXPECT_FALSE(active_layer()->can_use_lcd_text());
  4406. ASSERT_EQ(2u, active_layer()->tilings()->num_tilings());
  4407. ASSERT_TRUE(active_layer()->HighResTiling()->has_tiles());
  4408. for (Tile* tile : active_layer()->HighResTiling()->AllTilesForTesting())
  4409. EXPECT_FALSE(tile->can_use_lcd_text());
  4410. for (Tile* tile : active_layer()->LowResTiling()->AllTilesForTesting())
  4411. EXPECT_FALSE(tile->can_use_lcd_text());
  4412. }
  4413. TEST_F(LegacySWPictureLayerImplTest, TilingAllTilesDone) {
  4414. gfx::Size tile_size = host_impl()->settings().default_tile_size;
  4415. size_t tile_mem = 4 * tile_size.width() * tile_size.height();
  4416. gfx::Size layer_bounds(1000, 1000);
  4417. // Create tiles.
  4418. scoped_refptr<FakeRasterSource> pending_raster_source =
  4419. FakeRasterSource::CreateFilled(layer_bounds);
  4420. SetupPendingTree(pending_raster_source);
  4421. pending_layer()->SetBounds(layer_bounds);
  4422. ActivateTree();
  4423. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  4424. active_layer()->HighResTiling()->AllTilesForTesting());
  4425. host_impl()->SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
  4426. EXPECT_FALSE(active_layer()->HighResTiling()->all_tiles_done());
  4427. {
  4428. // Set a memory policy that will fit all tiles.
  4429. size_t max_tiles = 16;
  4430. size_t memory_limit = max_tiles * tile_mem;
  4431. ManagedMemoryPolicy policy(memory_limit,
  4432. gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
  4433. max_tiles);
  4434. host_impl()->SetMemoryPolicy(policy);
  4435. host_impl()->PrepareTiles();
  4436. EXPECT_TRUE(active_layer()->HighResTiling()->all_tiles_done());
  4437. }
  4438. {
  4439. // Set a memory policy that will cause tile eviction.
  4440. size_t max_tiles = 1;
  4441. size_t memory_limit = max_tiles * tile_mem;
  4442. ManagedMemoryPolicy policy(memory_limit,
  4443. gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
  4444. max_tiles);
  4445. host_impl()->SetMemoryPolicy(policy);
  4446. host_impl()->PrepareTiles();
  4447. EXPECT_FALSE(active_layer()->HighResTiling()->all_tiles_done());
  4448. }
  4449. }
  4450. class TileSizeTest : public PictureLayerImplTest {
  4451. public:
  4452. LayerTreeSettings CreateSettings() override {
  4453. LayerTreeSettings settings = PictureLayerImplTest::CreateSettings();
  4454. settings.default_tile_size = gfx::Size(100, 100);
  4455. settings.max_untiled_layer_size = gfx::Size(200, 200);
  4456. return settings;
  4457. }
  4458. };
  4459. class LegacySWTileSizeTest : public TileSizeTest {
  4460. public:
  4461. LayerTreeSettings CreateSettings() override {
  4462. LayerTreeSettings settings = TileSizeTest::CreateSettings();
  4463. settings.gpu_rasterization_disabled = true;
  4464. return settings;
  4465. }
  4466. };
  4467. TEST_F(LegacySWTileSizeTest, SWTileSizes) {
  4468. SetupPendingTree();
  4469. auto* layer = pending_layer();
  4470. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(1000, 1000));
  4471. gfx::Size result;
  4472. host_impl()->CommitComplete();
  4473. EXPECT_EQ(host_impl()->gpu_rasterization_status(),
  4474. GpuRasterizationStatus::OFF_DEVICE);
  4475. host_impl()->NotifyReadyToActivate();
  4476. // Default tile-size for large layers.
  4477. result = layer->CalculateTileSize(gfx::Size(10000, 10000));
  4478. EXPECT_EQ(result.width(), 100);
  4479. EXPECT_EQ(result.height(), 100);
  4480. // Don't tile and round-up, when under max_untiled_layer_size.
  4481. result = layer->CalculateTileSize(gfx::Size(42, 42));
  4482. EXPECT_EQ(result.width(), 64);
  4483. EXPECT_EQ(result.height(), 64);
  4484. result = layer->CalculateTileSize(gfx::Size(191, 191));
  4485. EXPECT_EQ(result.width(), 192);
  4486. EXPECT_EQ(result.height(), 192);
  4487. result = layer->CalculateTileSize(gfx::Size(199, 199));
  4488. EXPECT_EQ(result.width(), 200);
  4489. EXPECT_EQ(result.height(), 200);
  4490. }
  4491. TEST_F(TileSizeTest, GPUTileSizes) {
  4492. SetupPendingTree();
  4493. auto* layer = pending_layer();
  4494. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(1000, 1000));
  4495. gfx::Size result;
  4496. host_impl()->CommitComplete();
  4497. // Gpu-rasterization uses 25% viewport-height tiles.
  4498. // The +2's below are for border texels.
  4499. EXPECT_EQ(host_impl()->gpu_rasterization_status(),
  4500. GpuRasterizationStatus::ON);
  4501. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(2000, 2000));
  4502. host_impl()->NotifyReadyToActivate();
  4503. layer->set_gpu_raster_max_texture_size(
  4504. host_impl()->active_tree()->GetDeviceViewport().size());
  4505. result = layer->CalculateTileSize(gfx::Size(10000, 10000));
  4506. EXPECT_EQ(result.width(),
  4507. MathUtil::UncheckedRoundUp(
  4508. 1000 + 2 * PictureLayerTiling::kBorderTexels, 32));
  4509. EXPECT_EQ(result.height(), 512); // 500 + 2, 32-byte aligned.
  4510. // Clamp and round-up, when smaller than viewport.
  4511. // Tile-height doubles to 50% when width shrinks to <= 50%.
  4512. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(1000, 1000));
  4513. layer->set_gpu_raster_max_texture_size(
  4514. host_impl()->active_tree()->GetDeviceViewport().size());
  4515. result = layer->CalculateTileSize(gfx::Size(447, 10000));
  4516. EXPECT_EQ(result.width(), 448);
  4517. EXPECT_EQ(result.height(), 512); // 500 + 2, 32-byte aliged.
  4518. // Largest layer is 50% of viewport width (rounded up), and
  4519. // 50% of viewport in height.
  4520. result = layer->CalculateTileSize(gfx::Size(447, 400));
  4521. EXPECT_EQ(result.width(), 448);
  4522. EXPECT_EQ(result.height(), 448);
  4523. result = layer->CalculateTileSize(gfx::Size(500, 499));
  4524. EXPECT_EQ(result.width(), 512);
  4525. EXPECT_EQ(result.height(), 512); // 500 + 2, 32-byte aligned.
  4526. }
  4527. TEST_F(TileSizeTest, RawDrawTileSizes) {
  4528. base::test::ScopedFeatureList scoped_feature_list(features::kRawDraw);
  4529. SetupPendingTree();
  4530. auto* layer = pending_layer();
  4531. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(1000, 1000));
  4532. gfx::Size result;
  4533. host_impl()->CommitComplete();
  4534. constexpr int kMaxTextureSize = 2048;
  4535. // Gpu-RawDraw-rasterization uses 100%
  4536. // std::max({viewport-width, viewport-height, 2280}) tiles.
  4537. EXPECT_EQ(host_impl()->gpu_rasterization_status(),
  4538. GpuRasterizationStatus::ON);
  4539. gfx::Rect viewport_rect(2000, 2000);
  4540. host_impl()->active_tree()->SetDeviceViewportRect(viewport_rect);
  4541. layer->set_gpu_raster_max_texture_size(viewport_rect.size());
  4542. host_impl()->NotifyReadyToActivate();
  4543. gfx::Size content_bounds(10000, 10000);
  4544. result = layer->CalculateTileSize(content_bounds);
  4545. EXPECT_EQ(result, gfx::Size(kMaxTextureSize, kMaxTextureSize));
  4546. viewport_rect = gfx::Rect(1000, 1000);
  4547. host_impl()->active_tree()->SetDeviceViewportRect(viewport_rect);
  4548. layer->set_gpu_raster_max_texture_size(gfx::Size());
  4549. host_impl()->NotifyReadyToActivate();
  4550. content_bounds = gfx::Size(1600, 100);
  4551. result = layer->CalculateTileSize(content_bounds);
  4552. EXPECT_EQ(result, gfx::Size(1600, 128));
  4553. viewport_rect = gfx::Rect(1000, 1000);
  4554. host_impl()->active_tree()->SetDeviceViewportRect(viewport_rect);
  4555. layer->set_gpu_raster_max_texture_size(gfx::Size());
  4556. host_impl()->NotifyReadyToActivate();
  4557. content_bounds = gfx::Size(std::numeric_limits<int>::max(),
  4558. std::numeric_limits<int>::max());
  4559. result = layer->CalculateTileSize(content_bounds);
  4560. EXPECT_EQ(result, gfx::Size(kMaxTextureSize, kMaxTextureSize));
  4561. }
  4562. class HalfWidthTileTest : public PictureLayerImplTest {};
  4563. TEST_F(HalfWidthTileTest, TileSizes) {
  4564. SetupPendingTree();
  4565. auto* layer = pending_layer();
  4566. gfx::Size result;
  4567. host_impl()->CommitComplete();
  4568. EXPECT_EQ(host_impl()->gpu_rasterization_status(),
  4569. GpuRasterizationStatus::ON);
  4570. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(2000, 2000));
  4571. host_impl()->NotifyReadyToActivate();
  4572. // Basic test.
  4573. layer->set_gpu_raster_max_texture_size(
  4574. host_impl()->active_tree()->GetDeviceViewport().size());
  4575. result = layer->CalculateTileSize(gfx::Size(10000, 10000));
  4576. EXPECT_EQ(result.width(),
  4577. MathUtil::UncheckedRoundUp(
  4578. 2000 / 2 + 2 * PictureLayerTiling::kBorderTexels, 32));
  4579. EXPECT_EQ(result.height(), 512);
  4580. // When using odd sized viewport bounds, we should round up.
  4581. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(509, 1000));
  4582. layer->set_gpu_raster_max_texture_size(
  4583. host_impl()->active_tree()->GetDeviceViewport().size());
  4584. result = layer->CalculateTileSize(gfx::Size(10000, 10000));
  4585. EXPECT_EQ(result.width(), 288);
  4586. EXPECT_EQ(result.height(), 256);
  4587. // If content would fit in a single tile after rounding, we shouldn't halve
  4588. // the tile width.
  4589. host_impl()->active_tree()->SetDeviceViewportRect(gfx::Rect(511, 1000));
  4590. layer->set_gpu_raster_max_texture_size(
  4591. host_impl()->active_tree()->GetDeviceViewport().size());
  4592. result = layer->CalculateTileSize(gfx::Size(530, 10000));
  4593. EXPECT_EQ(result.width(), 544);
  4594. EXPECT_EQ(result.height(), 256);
  4595. }
  4596. TEST_F(NoLowResPictureLayerImplTest, LowResWasHighResCollision) {
  4597. gfx::Size layer_bounds(1300, 1900);
  4598. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  4599. SetupDefaultTrees(layer_bounds);
  4600. ResetTilingsAndRasterScales();
  4601. float page_scale = 2.f;
  4602. SetContentsScaleOnBothLayers(page_scale, 1.0f, page_scale);
  4603. EXPECT_BOTH_EQ(num_tilings(), 1u);
  4604. EXPECT_BOTH_EQ(tilings()->tiling_at(0)->contents_scale_key(), page_scale);
  4605. // Since this test simulates a pinch it needs an input handler.
  4606. // TODO(bokan): This is a raster unit test, it shouldn't be using a real
  4607. // input handler.
  4608. InputHandler::Create(static_cast<CompositorDelegateForInput&>(*host_impl()));
  4609. host_impl()->GetInputHandler().PinchGestureBegin(
  4610. gfx::Point(), ui::ScrollInputType::kTouchscreen);
  4611. // Zoom out to exactly the low res factor so that the previous high res
  4612. // would be equal to the current low res (if it were possible to have one).
  4613. float zoomed = page_scale / low_res_factor;
  4614. SetContentsScaleOnBothLayers(zoomed, 1.0f, zoomed);
  4615. EXPECT_EQ(1u, pending_layer()->num_tilings());
  4616. EXPECT_EQ(zoomed,
  4617. pending_layer()->tilings()->tiling_at(0)->contents_scale_key());
  4618. }
  4619. TEST_F(LegacySWPictureLayerImplTest, HighResWasLowResCollision) {
  4620. gfx::Size layer_bounds(1300, 1900);
  4621. float low_res_factor = host_impl()->settings().low_res_contents_scale_factor;
  4622. SetupDefaultTrees(layer_bounds);
  4623. ResetTilingsAndRasterScales();
  4624. float page_scale = 4.f;
  4625. float low_res = page_scale * low_res_factor;
  4626. float extra_low_res = low_res * low_res_factor;
  4627. SetupDrawPropertiesAndUpdateTiles(active_layer(), page_scale, 1.0f,
  4628. page_scale);
  4629. EXPECT_EQ(2u, active_layer()->tilings()->num_tilings());
  4630. EXPECT_EQ(page_scale,
  4631. active_layer()->tilings()->tiling_at(0)->contents_scale_key());
  4632. EXPECT_EQ(low_res,
  4633. active_layer()->tilings()->tiling_at(1)->contents_scale_key());
  4634. // Grab a current low res tile.
  4635. PictureLayerTiling* old_low_res_tiling =
  4636. active_layer()->tilings()->tiling_at(1);
  4637. Tile::Id old_low_res_tile_id =
  4638. active_layer()->tilings()->tiling_at(1)->TileAt(0, 0)->id();
  4639. // The tiling knows it has low res content.
  4640. EXPECT_TRUE(active_layer()
  4641. ->tilings()
  4642. ->tiling_at(1)
  4643. ->may_contain_low_resolution_tiles());
  4644. host_impl()->AdvanceToNextFrame(base::Milliseconds(1));
  4645. // Zoom in to exactly the low res factor so that the previous low res
  4646. // would be equal to the current high res.
  4647. SetupDrawPropertiesAndUpdateTiles(active_layer(), low_res, 1.0f, low_res);
  4648. // 3 tilings. The old high res, the new high res (old low res) and the new low
  4649. // res.
  4650. EXPECT_EQ(3u, active_layer()->num_tilings());
  4651. PictureLayerTilingSet* tilings = active_layer()->tilings();
  4652. EXPECT_EQ(page_scale, tilings->tiling_at(0)->contents_scale_key());
  4653. EXPECT_EQ(low_res, tilings->tiling_at(1)->contents_scale_key());
  4654. EXPECT_EQ(extra_low_res, tilings->tiling_at(2)->contents_scale_key());
  4655. EXPECT_EQ(NON_IDEAL_RESOLUTION, tilings->tiling_at(0)->resolution());
  4656. EXPECT_EQ(HIGH_RESOLUTION, tilings->tiling_at(1)->resolution());
  4657. EXPECT_EQ(LOW_RESOLUTION, tilings->tiling_at(2)->resolution());
  4658. // The old low res tile was destroyed and replaced.
  4659. EXPECT_EQ(old_low_res_tiling, tilings->tiling_at(1));
  4660. EXPECT_NE(old_low_res_tile_id, tilings->tiling_at(1)->TileAt(0, 0)->id());
  4661. EXPECT_TRUE(tilings->tiling_at(1)->TileAt(0, 0));
  4662. // New high res tiling.
  4663. EXPECT_FALSE(tilings->tiling_at(0)->may_contain_low_resolution_tiles());
  4664. // New low res tiling.
  4665. EXPECT_TRUE(tilings->tiling_at(2)->may_contain_low_resolution_tiles());
  4666. // This tiling will be high res now, it won't contain low res content since it
  4667. // was all destroyed.
  4668. EXPECT_FALSE(tilings->tiling_at(1)->may_contain_low_resolution_tiles());
  4669. }
  4670. TEST_F(LegacySWPictureLayerImplTest, CompositedImageCalculateContentsScale) {
  4671. gfx::Size layer_bounds(400, 400);
  4672. gfx::Rect layer_rect(layer_bounds);
  4673. host_impl()->active_tree()->SetDeviceViewportRect(layer_rect);
  4674. scoped_refptr<FakeRasterSource> pending_raster_source =
  4675. FakeRasterSource::CreateFilled(layer_bounds);
  4676. SetupPendingTree(pending_raster_source);
  4677. LayerTreeImpl* pending_tree = host_impl()->pending_tree();
  4678. const int kLayerId = 100;
  4679. std::unique_ptr<FakePictureLayerImpl> pending_layer =
  4680. FakePictureLayerImpl::Create(pending_tree, kLayerId,
  4681. pending_raster_source);
  4682. pending_layer->SetDirectlyCompositedImageDefaultRasterScale(
  4683. gfx::Vector2dF(1, 1));
  4684. pending_layer->SetDrawsContent(true);
  4685. FakePictureLayerImpl* pending_layer_ptr = pending_layer.get();
  4686. pending_tree->AddLayer(std::move(pending_layer));
  4687. CopyProperties(pending_tree->root_layer(), pending_layer_ptr);
  4688. UpdateDrawProperties(pending_tree);
  4689. SetupDrawPropertiesAndUpdateTiles(pending_layer_ptr, 2.f, 3.f, 4.f);
  4690. EXPECT_FLOAT_EQ(1.f, pending_layer_ptr->MaximumTilingContentsScale());
  4691. }
  4692. TEST_F(LegacySWPictureLayerImplTest, CompositedImageIgnoreIdealContentsScale) {
  4693. gfx::Size layer_bounds(400, 400);
  4694. gfx::Rect layer_rect(layer_bounds);
  4695. host_impl()->active_tree()->SetDeviceViewportRect(layer_rect);
  4696. scoped_refptr<FakeRasterSource> pending_raster_source =
  4697. FakeRasterSource::CreateFilled(layer_bounds);
  4698. SetupPendingTree(pending_raster_source);
  4699. LayerTreeImpl* pending_tree = host_impl()->pending_tree();
  4700. const int kLayerId = 100;
  4701. std::unique_ptr<FakePictureLayerImpl> pending_layer =
  4702. FakePictureLayerImpl::Create(pending_tree, kLayerId,
  4703. pending_raster_source);
  4704. pending_layer->SetDirectlyCompositedImageDefaultRasterScale(
  4705. gfx::Vector2dF(1, 1));
  4706. pending_layer->SetDrawsContent(true);
  4707. FakePictureLayerImpl* pending_layer_ptr = pending_layer.get();
  4708. pending_tree->AddLayer(std::move(pending_layer));
  4709. CopyProperties(pending_tree->root_layer(), pending_layer_ptr);
  4710. UpdateDrawProperties(pending_tree);
  4711. // Set PictureLayerImpl::ideal_contents_scale_ to 2.f.
  4712. const float suggested_ideal_contents_scale = 2.f;
  4713. const float device_scale_factor = 3.f;
  4714. const float page_scale_factor = 4.f;
  4715. SetupDrawPropertiesAndUpdateTiles(pending_layer_ptr,
  4716. suggested_ideal_contents_scale,
  4717. device_scale_factor, page_scale_factor);
  4718. EXPECT_EQ(1.f,
  4719. pending_layer_ptr->tilings()->tiling_at(0)->contents_scale_key());
  4720. // Push to active layer.
  4721. host_impl()->ActivateSyncTree();
  4722. FakePictureLayerImpl* active_layer = static_cast<FakePictureLayerImpl*>(
  4723. host_impl()->active_tree()->LayerById(kLayerId));
  4724. SetupDrawPropertiesAndUpdateTiles(active_layer,
  4725. suggested_ideal_contents_scale,
  4726. device_scale_factor, page_scale_factor);
  4727. EXPECT_EQ(1.f, active_layer->tilings()->tiling_at(0)->contents_scale_key());
  4728. active_layer->set_visible_layer_rect(layer_rect);
  4729. // Create resources for the tiles.
  4730. host_impl()->tile_manager()->InitializeTilesWithResourcesForTesting(
  4731. active_layer->tilings()->tiling_at(0)->AllTilesForTesting());
  4732. // Draw.
  4733. auto render_pass = viz::CompositorRenderPass::Create();
  4734. AppendQuadsData data;
  4735. active_layer->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
  4736. active_layer->AppendQuads(render_pass.get(), &data);
  4737. active_layer->DidDraw(nullptr);
  4738. ASSERT_FALSE(render_pass->quad_list.empty());
  4739. EXPECT_EQ(viz::DrawQuad::Material::kTiledContent,
  4740. render_pass->quad_list.front()->material);
  4741. // Tiles are ready at correct scale, so should not set had_incomplete_tile.
  4742. EXPECT_EQ(0, data.num_incomplete_tiles);
  4743. }
  4744. TEST_F(LegacySWPictureLayerImplTest, CompositedImageRasterScaleChanges) {
  4745. gfx::Size layer_bounds(400, 400);
  4746. scoped_refptr<FakeRasterSource> pending_raster_source =
  4747. FakeRasterSource::CreateFilled(layer_bounds);
  4748. SetupPendingTree(pending_raster_source);
  4749. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4750. gfx::Vector2dF(1, 1));
  4751. float expected_contents_scale = 0.125f;
  4752. for (int i = 1; i < 30; ++i) {
  4753. float ideal_contents_scale = 0.1f * i - 1e-6;
  4754. switch (i) {
  4755. // Scale 0.2.
  4756. case 2:
  4757. expected_contents_scale = 0.25f;
  4758. break;
  4759. // Scale 0.3.
  4760. case 3:
  4761. expected_contents_scale = 0.5f;
  4762. break;
  4763. // Scale 0.6.
  4764. case 6:
  4765. expected_contents_scale = 1.f;
  4766. break;
  4767. }
  4768. SetupDrawPropertiesAndUpdateTiles(pending_layer(), ideal_contents_scale,
  4769. 1.f, 1.f);
  4770. EXPECT_FLOAT_EQ(expected_contents_scale,
  4771. pending_layer()
  4772. ->picture_layer_tiling_set()
  4773. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4774. ->contents_scale_key())
  4775. << "ideal_contents_scale: " << ideal_contents_scale;
  4776. }
  4777. expected_contents_scale = 1.f;
  4778. for (int i = 30; i >= 1; --i) {
  4779. float ideal_contents_scale = 0.1f * i - 1e-6;
  4780. switch (i) {
  4781. // Scale 0.2.
  4782. case 2:
  4783. expected_contents_scale = 0.25f;
  4784. break;
  4785. }
  4786. SetupDrawPropertiesAndUpdateTiles(pending_layer(), ideal_contents_scale,
  4787. 1.f, 1.f);
  4788. EXPECT_FLOAT_EQ(expected_contents_scale,
  4789. pending_layer()
  4790. ->picture_layer_tiling_set()
  4791. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4792. ->contents_scale_key())
  4793. << "ideal_contents_scale: " << ideal_contents_scale;
  4794. }
  4795. }
  4796. TEST_F(LegacySWPictureLayerImplTest, CompositedImageRasterOnChange) {
  4797. gfx::Size layer_bounds(400, 400);
  4798. scoped_refptr<FakeRasterSource> pending_raster_source =
  4799. FakeRasterSource::CreateFilled(layer_bounds);
  4800. SetupPendingTree(pending_raster_source);
  4801. // The image is smaller than the layer bounds.
  4802. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4803. gfx::Vector2dF(0.5f, 0.5f));
  4804. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.f, 1.f, 1.f);
  4805. EXPECT_FLOAT_EQ(0.5f, pending_layer()
  4806. ->picture_layer_tiling_set()
  4807. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4808. ->contents_scale_key());
  4809. // The image is much larger than the layer bounds (5x). Verify that the
  4810. // scaling down code is triggered (we should halve the raster scale until it
  4811. // is less than 2x the ideal source scale).
  4812. pending_layer()->SetBounds(layer_bounds);
  4813. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4814. gfx::Vector2dF(5, 5));
  4815. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.f, 1.f, 1.f);
  4816. EXPECT_FLOAT_EQ(1.25f, pending_layer()
  4817. ->picture_layer_tiling_set()
  4818. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4819. ->contents_scale_key());
  4820. // Update the bounds and and bump up the ideal scale so that the scale down
  4821. // restriction is lifted.
  4822. pending_layer()->SetBounds(layer_bounds);
  4823. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 4.f, 1.f, 1.f);
  4824. EXPECT_FLOAT_EQ(5.f, pending_layer()
  4825. ->picture_layer_tiling_set()
  4826. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4827. ->contents_scale_key());
  4828. // Lower the ideal scale to see that the clamping still applied as it is
  4829. // lowered.
  4830. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 0.5f, 1.f, 1.f);
  4831. EXPECT_FLOAT_EQ(0.625f, pending_layer()
  4832. ->picture_layer_tiling_set()
  4833. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4834. ->contents_scale_key());
  4835. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 0.25f, 1.f, 1.f);
  4836. EXPECT_FLOAT_EQ(0.625f, pending_layer()
  4837. ->picture_layer_tiling_set()
  4838. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4839. ->contents_scale_key());
  4840. }
  4841. TEST_F(LegacySWPictureLayerImplTest, CompositedImageTinyRasterScale) {
  4842. gfx::Size layer_bounds(512, 512);
  4843. scoped_refptr<FakeRasterSource> pending_raster_source =
  4844. FakeRasterSource::CreateFilled(layer_bounds);
  4845. SetupPendingTree(pending_raster_source);
  4846. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4847. gfx::Vector2dF(1e-20f, 1e-20f));
  4848. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.f, 1.f, 1.f);
  4849. EXPECT_FLOAT_EQ(1.f / 512, pending_layer()
  4850. ->picture_layer_tiling_set()
  4851. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4852. ->contents_scale_key());
  4853. }
  4854. TEST_F(LegacySWPictureLayerImplTest, CompositedImageHugeRasterScale) {
  4855. gfx::Size layer_bounds(400, 400);
  4856. scoped_refptr<FakeRasterSource> pending_raster_source =
  4857. FakeRasterSource::CreateFilled(layer_bounds);
  4858. SetupPendingTree(pending_raster_source);
  4859. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4860. gfx::Vector2dF(1e20f, 1e20f));
  4861. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.f, 1.f, 1.f);
  4862. EXPECT_FLOAT_EQ(1.0f, pending_layer()
  4863. ->picture_layer_tiling_set()
  4864. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4865. ->contents_scale_key());
  4866. }
  4867. TEST_F(LegacySWPictureLayerImplTest, CompositedImageRasterHighResScreen) {
  4868. gfx::Size layer_bounds(100, 100);
  4869. scoped_refptr<FakeRasterSource> pending_raster_source =
  4870. FakeRasterSource::CreateFilled(layer_bounds);
  4871. SetupPendingTree(pending_raster_source);
  4872. // Verify that device_scale=2.0 is handled correctly. The image is expected to
  4873. // be downscaled not more than 4x, so content scale is above 1.0.
  4874. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4875. gfx::Vector2dF(5, 5));
  4876. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 1.f, 2.f, 1.f);
  4877. EXPECT_FLOAT_EQ(1.25f, pending_layer()
  4878. ->picture_layer_tiling_set()
  4879. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4880. ->contents_scale_key());
  4881. }
  4882. TEST_F(LegacySWPictureLayerImplTest, CompositedImageRasterOptOutTransitions) {
  4883. gfx::Size layer_bounds(6, 6);
  4884. scoped_refptr<FakeRasterSource> pending_raster_source =
  4885. FakeRasterSource::CreateFilled(layer_bounds);
  4886. SetupPendingTree(pending_raster_source);
  4887. // Start with image and bounds matching to have this layer initially opted
  4888. // in to directly composited images.
  4889. pending_layer()->SetBounds(layer_bounds);
  4890. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4891. gfx::Vector2dF(1, 1));
  4892. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 0.3f, 1.f, 1.f);
  4893. EXPECT_FLOAT_EQ(0.5f, pending_layer()
  4894. ->picture_layer_tiling_set()
  4895. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4896. ->contents_scale_key());
  4897. // Change the image and bounds to values that make the layer not eligible for
  4898. // direct compositing. This must be reflected by a |contents_scale_key()| of
  4899. // 0.2f (matching the ideal source scale).
  4900. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4901. gfx::Vector2dF(50, 50));
  4902. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 0.2f, 1.f, 1.f);
  4903. EXPECT_FLOAT_EQ(0.2f, pending_layer()
  4904. ->picture_layer_tiling_set()
  4905. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4906. ->contents_scale_key());
  4907. // Ensure we get back to a directly composited image if the input values
  4908. // change such that the optimization should apply.
  4909. pending_layer()->SetBounds(ScaleToFlooredSize(layer_bounds, 2));
  4910. pending_layer()->SetDirectlyCompositedImageDefaultRasterScale(
  4911. gfx::Vector2dF(2, 2));
  4912. SetupDrawPropertiesAndUpdateTiles(pending_layer(), 0.2f, 1.f, 1.f);
  4913. EXPECT_FLOAT_EQ(0.25f, pending_layer()
  4914. ->picture_layer_tiling_set()
  4915. ->FindTilingWithResolution(HIGH_RESOLUTION)
  4916. ->contents_scale_key());
  4917. }
  4918. TEST_F(LegacySWPictureLayerImplTest,
  4919. ChangeRasterTranslationNukePendingLayerTiles) {
  4920. gfx::Size layer_bounds(200, 200);
  4921. gfx::Size tile_size(256, 256);
  4922. auto raster_source = FakeRasterSource::CreateFilledWithText(layer_bounds);
  4923. SetupTreesWithFixedTileSize(raster_source, raster_source, tile_size,
  4924. Region());
  4925. // Start with scale & translation of * 2.25 + (0.25, 0.5).
  4926. SetupDrawProperties(pending_layer(), 2.25f, 1.5f, 1.f);
  4927. gfx::Transform translate1;
  4928. translate1.Translate(0.25f, 0.5f);
  4929. pending_layer()->draw_properties().screen_space_transform.ConcatTransform(
  4930. translate1);
  4931. pending_layer()->draw_properties().target_space_transform =
  4932. pending_layer()->draw_properties().screen_space_transform;
  4933. pending_layer()->UpdateTiles();
  4934. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  4935. {
  4936. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(0);
  4937. EXPECT_EQ(gfx::AxisTransform2d(2.25f, gfx::Vector2dF(0.25f, 0.5f)),
  4938. tiling->raster_transform());
  4939. EXPECT_EQ(4u, tiling->AllTilesForTesting().size());
  4940. for (auto* tile : tiling->AllTilesForTesting())
  4941. EXPECT_EQ(tile->raster_transform(), tiling->raster_transform());
  4942. }
  4943. // Change to scale & translation of * 2.25 + (0.75, 0.25).
  4944. // Verifies that layer movement updates raster translation.
  4945. SetupDrawProperties(pending_layer(), 2.25f, 1.5f, 1.f);
  4946. gfx::Transform translate2;
  4947. translate2.Translate(0.75f, 0.25f);
  4948. pending_layer()->draw_properties().screen_space_transform.ConcatTransform(
  4949. translate2);
  4950. pending_layer()->draw_properties().target_space_transform =
  4951. pending_layer()->draw_properties().screen_space_transform;
  4952. pending_layer()->UpdateTiles();
  4953. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  4954. {
  4955. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(0);
  4956. EXPECT_EQ(gfx::AxisTransform2d(2.25f, gfx::Vector2dF(0.75f, 0.25f)),
  4957. tiling->raster_transform());
  4958. EXPECT_EQ(4u, tiling->AllTilesForTesting().size());
  4959. for (auto* tile : tiling->AllTilesForTesting())
  4960. EXPECT_EQ(tile->raster_transform(), tiling->raster_transform());
  4961. }
  4962. // Now change the device scale factor but keep the same total scale. Old tiles
  4963. // with the same scale would become non-ideal and deleted on pending layers.
  4964. SetupDrawProperties(pending_layer(), 2.25f, 1.0f, 1.f);
  4965. pending_layer()->draw_properties().screen_space_transform.ConcatTransform(
  4966. translate2);
  4967. pending_layer()->draw_properties().target_space_transform =
  4968. pending_layer()->draw_properties().screen_space_transform;
  4969. pending_layer()->UpdateTiles();
  4970. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  4971. {
  4972. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(0);
  4973. EXPECT_EQ(gfx::AxisTransform2d(2.25f, gfx::Vector2dF(0.75f, 0.25f)),
  4974. tiling->raster_transform());
  4975. EXPECT_EQ(4u, tiling->AllTilesForTesting().size());
  4976. for (auto* tile : tiling->AllTilesForTesting())
  4977. EXPECT_EQ(tile->raster_transform(), tiling->raster_transform());
  4978. }
  4979. }
  4980. TEST_F(LegacySWPictureLayerImplTest,
  4981. ChangeRasterTranslationNukeActiveLayerTiles) {
  4982. gfx::Size layer_bounds(200, 200);
  4983. gfx::Size tile_size(256, 256);
  4984. auto raster_source = FakeRasterSource::CreateFilledWithText(layer_bounds);
  4985. SetupTreesWithFixedTileSize(raster_source, raster_source, tile_size,
  4986. Region());
  4987. // Start with scale & translation of * 2.25 + (0.25, 0.5) on the active layer.
  4988. SetupDrawProperties(active_layer(), 2.25f, 1.5f, 1.f);
  4989. gfx::Transform translate1;
  4990. translate1.Translate(0.25f, 0.5f);
  4991. active_layer()->draw_properties().screen_space_transform.ConcatTransform(
  4992. translate1);
  4993. active_layer()->draw_properties().target_space_transform =
  4994. active_layer()->draw_properties().screen_space_transform;
  4995. active_layer()->UpdateTiles();
  4996. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  4997. {
  4998. PictureLayerTiling* tiling =
  4999. active_layer()->tilings()->FindTilingWithScaleKey(2.25f);
  5000. EXPECT_EQ(gfx::AxisTransform2d(2.25f, gfx::Vector2dF(0.25f, 0.5f)),
  5001. tiling->raster_transform());
  5002. EXPECT_EQ(4u, tiling->AllTilesForTesting().size());
  5003. for (auto* tile : tiling->AllTilesForTesting())
  5004. EXPECT_EQ(tile->raster_transform(), tiling->raster_transform());
  5005. }
  5006. // Create a pending layer with the same scale but different translation.
  5007. SetupDrawProperties(pending_layer(), 2.25f, 1.5f, 1.f);
  5008. gfx::Transform translate2;
  5009. translate2.Translate(0.75f, 0.25f);
  5010. pending_layer()->draw_properties().screen_space_transform.ConcatTransform(
  5011. translate2);
  5012. pending_layer()->draw_properties().target_space_transform =
  5013. pending_layer()->draw_properties().screen_space_transform;
  5014. pending_layer()->UpdateTiles();
  5015. ASSERT_EQ(1u, pending_layer()->tilings()->num_tilings());
  5016. {
  5017. PictureLayerTiling* tiling = pending_layer()->tilings()->tiling_at(0);
  5018. EXPECT_EQ(gfx::AxisTransform2d(2.25f, gfx::Vector2dF(0.75f, 0.25f)),
  5019. tiling->raster_transform());
  5020. EXPECT_EQ(4u, tiling->AllTilesForTesting().size());
  5021. for (auto* tile : tiling->AllTilesForTesting())
  5022. EXPECT_EQ(tile->raster_transform(), tiling->raster_transform());
  5023. }
  5024. // Now push to the active layer.
  5025. // Verifies the active tiles get evicted due to slot conflict.
  5026. host_impl()->ActivateSyncTree();
  5027. ASSERT_EQ(3u, active_layer()->tilings()->num_tilings());
  5028. {
  5029. PictureLayerTiling* tiling =
  5030. active_layer()->tilings()->FindTilingWithScaleKey(2.25f);
  5031. EXPECT_EQ(gfx::AxisTransform2d(2.25f, gfx::Vector2dF(0.75f, 0.25f)),
  5032. tiling->raster_transform());
  5033. EXPECT_EQ(4u, tiling->AllTilesForTesting().size());
  5034. for (auto* tile : tiling->AllTilesForTesting())
  5035. EXPECT_EQ(tile->raster_transform(), tiling->raster_transform());
  5036. }
  5037. }
  5038. TEST_F(LegacySWPictureLayerImplTest, AnimatedImages) {
  5039. gfx::Size layer_bounds(1000, 1000);
  5040. // Set up a raster source with 2 animated images.
  5041. auto recording_source = FakeRecordingSource::CreateRecordingSource(
  5042. gfx::Rect(layer_bounds), layer_bounds);
  5043. std::vector<FrameMetadata> frames = {
  5044. FrameMetadata(true, base::Milliseconds(1)),
  5045. FrameMetadata(true, base::Milliseconds(1))};
  5046. PaintImage image1 = CreateAnimatedImage(gfx::Size(200, 200), frames);
  5047. PaintImage image2 = CreateAnimatedImage(gfx::Size(200, 200), frames);
  5048. recording_source->add_draw_image(image1, gfx::Point(100, 100));
  5049. recording_source->add_draw_image(image2, gfx::Point(500, 500));
  5050. recording_source->Rerecord();
  5051. scoped_refptr<RasterSource> raster_source =
  5052. recording_source->CreateRasterSource();
  5053. // All images should be registered on the pending layer.
  5054. SetupPendingTree(raster_source, gfx::Size(), Region(gfx::Rect(layer_bounds)));
  5055. auto* controller = host_impl()->image_animation_controller();
  5056. EXPECT_EQ(controller->GetDriversForTesting(image1.stable_id())
  5057. .count(pending_layer()),
  5058. 1u);
  5059. EXPECT_EQ(controller->GetDriversForTesting(image2.stable_id())
  5060. .count(pending_layer()),
  5061. 1u);
  5062. // Make only the first image visible and verify that only this image is
  5063. // animated.
  5064. gfx::Rect visible_rect(0, 0, 300, 300);
  5065. pending_layer()->set_visible_layer_rect(visible_rect);
  5066. EXPECT_TRUE(pending_layer()->ShouldAnimate(image1.stable_id()));
  5067. EXPECT_FALSE(pending_layer()->ShouldAnimate(image2.stable_id()));
  5068. // Now activate and make sure the active layer is registered as well.
  5069. ActivateTree();
  5070. active_layer()->set_visible_layer_rect(visible_rect);
  5071. EXPECT_EQ(controller->GetDriversForTesting(image1.stable_id())
  5072. .count(active_layer()),
  5073. 1u);
  5074. EXPECT_EQ(controller->GetDriversForTesting(image2.stable_id())
  5075. .count(active_layer()),
  5076. 1u);
  5077. // Once activated, only the active layer should drive animations for these
  5078. // images. Since DrawProperties are not updated on the recycle tree, it has
  5079. // stale state for visibility of images.
  5080. ASSERT_EQ(old_pending_layer()->visible_layer_rect(), visible_rect);
  5081. EXPECT_FALSE(old_pending_layer()->ShouldAnimate(image1.stable_id()));
  5082. EXPECT_FALSE(old_pending_layer()->ShouldAnimate(image2.stable_id()));
  5083. EXPECT_TRUE(active_layer()->ShouldAnimate(image1.stable_id()));
  5084. EXPECT_FALSE(active_layer()->ShouldAnimate(image2.stable_id()));
  5085. }
  5086. TEST_F(LegacySWPictureLayerImplTest, PaintWorkletInputs) {
  5087. gfx::Size layer_bounds(1000, 1000);
  5088. // Set up a raster source with 2 PaintWorkletInputs.
  5089. auto recording_source = FakeRecordingSource::CreateRecordingSource(
  5090. gfx::Rect(layer_bounds), layer_bounds);
  5091. scoped_refptr<TestPaintWorkletInput> input1 =
  5092. base::MakeRefCounted<TestPaintWorkletInput>(gfx::SizeF(100, 100));
  5093. PaintImage image1 = CreatePaintWorkletPaintImage(input1);
  5094. scoped_refptr<TestPaintWorkletInput> input2 =
  5095. base::MakeRefCounted<TestPaintWorkletInput>(gfx::SizeF(50, 50));
  5096. PaintImage image2 = CreatePaintWorkletPaintImage(input2);
  5097. recording_source->add_draw_image(image1, gfx::Point(100, 100));
  5098. recording_source->add_draw_image(image2, gfx::Point(500, 500));
  5099. recording_source->Rerecord();
  5100. scoped_refptr<RasterSource> raster_source =
  5101. recording_source->CreateRasterSource();
  5102. // All inputs should be registered on the pending layer.
  5103. SetupPendingTree(raster_source, gfx::Size(), Region(gfx::Rect(layer_bounds)));
  5104. EXPECT_EQ(pending_layer()->GetPaintWorkletRecordMap().size(), 2u);
  5105. EXPECT_TRUE(pending_layer()->GetPaintWorkletRecordMap().contains(input1));
  5106. EXPECT_TRUE(pending_layer()->GetPaintWorkletRecordMap().contains(input2));
  5107. // Specify a record for one of the inputs.
  5108. sk_sp<PaintRecord> record1 = sk_make_sp<PaintOpBuffer>();
  5109. pending_layer()->SetPaintWorkletRecord(input1, record1);
  5110. // Now activate and make sure the active layer is registered as well, with the
  5111. // appropriate record.
  5112. ActivateTree();
  5113. EXPECT_EQ(active_layer()->GetPaintWorkletRecordMap().size(), 2u);
  5114. auto it = active_layer()->GetPaintWorkletRecordMap().find(input1);
  5115. ASSERT_NE(it, active_layer()->GetPaintWorkletRecordMap().end());
  5116. EXPECT_EQ(it->second.second, record1);
  5117. EXPECT_TRUE(active_layer()->GetPaintWorkletRecordMap().contains(input2));
  5118. // Committing new PaintWorkletInputs (in a new raster source) should replace
  5119. // the previous ones.
  5120. recording_source = FakeRecordingSource::CreateRecordingSource(
  5121. gfx::Rect(layer_bounds), layer_bounds);
  5122. scoped_refptr<TestPaintWorkletInput> input3 =
  5123. base::MakeRefCounted<TestPaintWorkletInput>(gfx::SizeF(12, 12));
  5124. PaintImage image3 = CreatePaintWorkletPaintImage(input3);
  5125. recording_source->add_draw_image(image3, gfx::Point(10, 10));
  5126. recording_source->Rerecord();
  5127. raster_source = recording_source->CreateRasterSource();
  5128. SetupPendingTree(raster_source, gfx::Size(), Region(gfx::Rect(layer_bounds)));
  5129. EXPECT_EQ(pending_layer()->GetPaintWorkletRecordMap().size(), 1u);
  5130. EXPECT_TRUE(pending_layer()->GetPaintWorkletRecordMap().contains(input3));
  5131. }
  5132. TEST_F(LegacySWPictureLayerImplTest, NoTilingsUsesScaleOne) {
  5133. auto render_pass = viz::CompositorRenderPass::Create();
  5134. gfx::Size layer_bounds(1000, 10000);
  5135. scoped_refptr<FakeRasterSource> active_raster_source =
  5136. FakeRasterSource::CreateEmpty(layer_bounds);
  5137. SetupPendingTree(active_raster_source);
  5138. ActivateTree();
  5139. active_layer()->SetContentsOpaque(true);
  5140. active_layer()->draw_properties().visible_layer_rect =
  5141. gfx::Rect(0, 0, 1000, 1000);
  5142. active_layer()->UpdateTiles();
  5143. ASSERT_FALSE(active_layer()->HighResTiling());
  5144. AppendQuadsData data;
  5145. active_layer()->WillDraw(DRAW_MODE_HARDWARE, nullptr);
  5146. active_layer()->AppendQuads(render_pass.get(), &data);
  5147. active_layer()->DidDraw(nullptr);
  5148. // One checkerboard quad.
  5149. EXPECT_EQ(1u, render_pass->quad_list.size());
  5150. auto* shared_quad_state = render_pass->quad_list.begin()->shared_quad_state;
  5151. // We should use scale 1 here, so the layer rect should be full layer bounds
  5152. // and the transform should be identity.
  5153. EXPECT_EQ(gfx::Rect(1000, 10000), shared_quad_state->quad_layer_rect);
  5154. EXPECT_TRUE(shared_quad_state->quad_to_target_transform.IsIdentity());
  5155. }
  5156. TEST_F(LegacySWPictureLayerImplTest,
  5157. TransformedRasterizationAndContentsOpaqueAndLCDText) {
  5158. auto raster_source =
  5159. FakeRasterSource::CreateFilledWithText(gfx::Size(200, 200));
  5160. SetupTreesWithInvalidation(raster_source, raster_source, Region());
  5161. pending_layer()->SetBackgroundColor(SkColors::kWhite);
  5162. pending_layer()->SetContentsOpaque(true);
  5163. pending_layer()->SetOffsetToTransformParent(gfx::Vector2dF(0.2, 0.3));
  5164. host_impl()->pending_tree()->set_needs_update_draw_properties();
  5165. UpdateDrawProperties(host_impl()->pending_tree());
  5166. EXPECT_TRUE(pending_layer()->contents_opaque());
  5167. EXPECT_TRUE(pending_layer()->contents_opaque_for_text());
  5168. EXPECT_EQ(LCDTextDisallowedReason::kNone,
  5169. pending_layer()->ComputeLCDTextDisallowedReasonForTesting());
  5170. ASSERT_TRUE(pending_layer()->HighResTiling());
  5171. EXPECT_EQ(gfx::Vector2dF(0.2, 0.3),
  5172. pending_layer()->HighResTiling()->raster_transform().translation());
  5173. // Adding will-change:transform will keep the current raster translation.
  5174. SetWillChangeTransform(pending_layer(), true);
  5175. host_impl()->pending_tree()->set_needs_update_draw_properties();
  5176. UpdateDrawProperties(host_impl()->pending_tree());
  5177. EXPECT_TRUE(pending_layer()->contents_opaque());
  5178. EXPECT_TRUE(pending_layer()->contents_opaque_for_text());
  5179. EXPECT_EQ(LCDTextDisallowedReason::kWillChangeTransform,
  5180. pending_layer()->ComputeLCDTextDisallowedReasonForTesting());
  5181. ASSERT_TRUE(pending_layer()->HighResTiling());
  5182. EXPECT_EQ(gfx::Vector2dF(0.2, 0.3),
  5183. pending_layer()->HighResTiling()->raster_transform().translation());
  5184. // We should not update raster translation when there is
  5185. // will-change:transform.
  5186. pending_layer()->SetOffsetToTransformParent(gfx::Vector2dF(0.4, 0.5));
  5187. host_impl()->pending_tree()->set_needs_update_draw_properties();
  5188. UpdateDrawProperties(host_impl()->pending_tree());
  5189. EXPECT_TRUE(pending_layer()->contents_opaque());
  5190. EXPECT_TRUE(pending_layer()->contents_opaque_for_text());
  5191. EXPECT_EQ(LCDTextDisallowedReason::kWillChangeTransform,
  5192. pending_layer()->ComputeLCDTextDisallowedReasonForTesting());
  5193. ASSERT_TRUE(pending_layer()->HighResTiling());
  5194. EXPECT_EQ(gfx::Vector2dF(0.2, 0.3),
  5195. pending_layer()->HighResTiling()->raster_transform().translation());
  5196. // Removing will-change:transform will update raster translation.
  5197. SetWillChangeTransform(pending_layer(), false);
  5198. host_impl()->pending_tree()->set_needs_update_draw_properties();
  5199. UpdateDrawProperties(host_impl()->pending_tree());
  5200. EXPECT_TRUE(pending_layer()->contents_opaque());
  5201. EXPECT_TRUE(pending_layer()->contents_opaque_for_text());
  5202. EXPECT_EQ(LCDTextDisallowedReason::kNone,
  5203. pending_layer()->ComputeLCDTextDisallowedReasonForTesting());
  5204. ASSERT_TRUE(pending_layer()->HighResTiling());
  5205. EXPECT_EQ(gfx::Vector2dF(0.4, 0.5),
  5206. pending_layer()->HighResTiling()->raster_transform().translation());
  5207. }
  5208. TEST_F(LegacySWPictureLayerImplTest,
  5209. TransformedRasterizationAndLCDTextWithoutText) {
  5210. auto raster_source = FakeRasterSource::CreateFilled(gfx::Size(200, 200));
  5211. SetupTreesWithInvalidation(raster_source, raster_source, Region());
  5212. pending_layer()->SetBackgroundColor(SkColors::kWhite);
  5213. pending_layer()->SetContentsOpaque(true);
  5214. pending_layer()->SetOffsetToTransformParent(gfx::Vector2dF(0.2, 0.3));
  5215. host_impl()->pending_tree()->set_needs_update_draw_properties();
  5216. UpdateDrawProperties(host_impl()->pending_tree());
  5217. EXPECT_TRUE(pending_layer()->contents_opaque());
  5218. EXPECT_TRUE(pending_layer()->contents_opaque_for_text());
  5219. EXPECT_EQ(LCDTextDisallowedReason::kNoText,
  5220. pending_layer()->ComputeLCDTextDisallowedReasonForTesting());
  5221. ASSERT_TRUE(pending_layer()->HighResTiling());
  5222. EXPECT_EQ(gfx::Vector2dF(),
  5223. pending_layer()->HighResTiling()->raster_transform().translation());
  5224. }
  5225. enum {
  5226. kCanUseLCDText = 1 << 0,
  5227. kLayersAlwaysAllowedLCDText = 1 << 1,
  5228. };
  5229. class LCDTextTest : public PictureLayerImplTest,
  5230. public testing::WithParamInterface<unsigned> {
  5231. protected:
  5232. LayerTreeSettings CreateSettings() override {
  5233. auto settings = PictureLayerImplTest::CreateSettings();
  5234. settings.can_use_lcd_text = GetParam() & kCanUseLCDText;
  5235. settings.layers_always_allowed_lcd_text =
  5236. GetParam() & kLayersAlwaysAllowedLCDText;
  5237. return settings;
  5238. }
  5239. void SetUp() override {
  5240. PictureLayerImplTest::SetUp();
  5241. auto raster_source =
  5242. FakeRasterSource::CreateFilledWithText(gfx::Size(200, 200));
  5243. SetupPendingTree(raster_source);
  5244. tree_ = host_impl()->pending_tree();
  5245. layer_ = pending_layer();
  5246. descendant_ = AddLayer<PictureLayerImpl>(tree_);
  5247. layer_->SetContentsOpaque(true);
  5248. layer_->SetDrawsContent(true);
  5249. layer_->SetBounds(gfx::Size(200, 200));
  5250. ASSERT_TRUE(layer_->CanHaveTilings());
  5251. descendant_->SetContentsOpaque(true);
  5252. descendant_->SetDrawsContent(true);
  5253. descendant_->SetBounds(gfx::Size(200, 200));
  5254. Region invalidation;
  5255. descendant_->UpdateRasterSource(raster_source, &invalidation, nullptr,
  5256. nullptr);
  5257. ASSERT_TRUE(layer_->CanHaveTilings());
  5258. CreateTransformNode(layer_);
  5259. CreateEffectNode(layer_);
  5260. CopyProperties(layer_, descendant_);
  5261. CreateTransformNode(descendant_);
  5262. CreateEffectNode(descendant_);
  5263. }
  5264. void CheckCanUseLCDText(LCDTextDisallowedReason expected_disallowed_reason,
  5265. const char* description,
  5266. PictureLayerImpl* layer = nullptr) {
  5267. UpdateDrawProperties(tree_);
  5268. if (GetParam() & kLayersAlwaysAllowedLCDText)
  5269. expected_disallowed_reason = LCDTextDisallowedReason::kNone;
  5270. else if (!(GetParam() & kCanUseLCDText))
  5271. expected_disallowed_reason = LCDTextDisallowedReason::kSetting;
  5272. SCOPED_TRACE(description);
  5273. if (layer) {
  5274. EXPECT_EQ(expected_disallowed_reason,
  5275. layer->ComputeLCDTextDisallowedReasonForTesting());
  5276. } else {
  5277. EXPECT_EQ(expected_disallowed_reason,
  5278. layer_->ComputeLCDTextDisallowedReasonForTesting());
  5279. EXPECT_EQ(expected_disallowed_reason,
  5280. descendant_->ComputeLCDTextDisallowedReasonForTesting());
  5281. }
  5282. }
  5283. raw_ptr<LayerTreeImpl> tree_ = nullptr;
  5284. raw_ptr<PictureLayerImpl> layer_ = nullptr;
  5285. raw_ptr<PictureLayerImpl> descendant_ = nullptr;
  5286. };
  5287. INSTANTIATE_TEST_SUITE_P(All,
  5288. LCDTextTest,
  5289. testing::Values(0,
  5290. kCanUseLCDText,
  5291. kLayersAlwaysAllowedLCDText,
  5292. kCanUseLCDText |
  5293. kLayersAlwaysAllowedLCDText));
  5294. TEST_P(LCDTextTest, IdentityTransform) {
  5295. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "identity transform");
  5296. }
  5297. TEST_P(LCDTextTest, IntegralTransform) {
  5298. gfx::Transform integral_translation;
  5299. integral_translation.Translate(1.0, 2.0);
  5300. SetTransform(layer_.get(), integral_translation);
  5301. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "integral transform");
  5302. }
  5303. TEST_P(LCDTextTest, NonIntegralTranslation) {
  5304. // Non-integral translation.
  5305. gfx::Transform non_integral_translation;
  5306. non_integral_translation.Translate(1.5, 2.5);
  5307. SetTransform(layer_.get(), non_integral_translation);
  5308. // We can use LCD-text as raster translation can align the text to physical
  5309. // pixels for fragtional transform in the render target.
  5310. CheckCanUseLCDText(LCDTextDisallowedReason::kNone,
  5311. "non-integeral translation");
  5312. SetTransform(layer_.get(), gfx::Transform());
  5313. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "identity transform");
  5314. }
  5315. TEST_P(LCDTextTest, NonIntegralTranslationAboveRenderTarget) {
  5316. // Non-integral translation above render target.
  5317. gfx::Transform non_integral_translation;
  5318. non_integral_translation.Translate(1.5, 2.5);
  5319. SetTransform(layer_.get(), non_integral_translation);
  5320. SetRenderSurfaceReason(layer_.get(), RenderSurfaceReason::kTest);
  5321. // Raster translation can't handle fractional transform above the render
  5322. // target, so LCD text is not allowed.
  5323. CheckCanUseLCDText(LCDTextDisallowedReason::kNonIntegralTranslation,
  5324. "non-integeral translation above render target");
  5325. SetTransform(layer_.get(), gfx::Transform());
  5326. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "identity transform");
  5327. }
  5328. TEST_P(LCDTextTest, NonTranslation) {
  5329. // Rotation.
  5330. gfx::Transform rotation;
  5331. rotation.Rotate(10.0);
  5332. SetTransform(layer_.get(), rotation);
  5333. CheckCanUseLCDText(LCDTextDisallowedReason::kNonIntegralTranslation,
  5334. "Rotation transform");
  5335. // Scale. LCD text is allowed.
  5336. gfx::Transform scale;
  5337. scale.Scale(2.0, 2.0);
  5338. SetTransform(layer_.get(), scale);
  5339. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "Scale transform");
  5340. // Skew.
  5341. gfx::Transform skew;
  5342. skew.Skew(10.0, 0.0);
  5343. SetTransform(layer_.get(), skew);
  5344. CheckCanUseLCDText(LCDTextDisallowedReason::kNonIntegralTranslation,
  5345. "Skew transform");
  5346. SetTransform(layer_.get(), gfx::Transform());
  5347. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "identity transform");
  5348. }
  5349. TEST_P(LCDTextTest, NonTranslationAboveRenderTarget) {
  5350. SetRenderSurfaceReason(layer_.get(), RenderSurfaceReason::kTest);
  5351. // Rotation.
  5352. gfx::Transform rotation;
  5353. rotation.Rotate(10.0);
  5354. SetTransform(layer_.get(), rotation);
  5355. CheckCanUseLCDText(LCDTextDisallowedReason::kNonIntegralTranslation,
  5356. "rotation transform above render target");
  5357. // Scale. LCD-text is allowed.
  5358. gfx::Transform scale;
  5359. scale.Scale(2.0, 2.0);
  5360. // Apply perspective and rotation to prevent the scale from applying
  5361. // to the layers below the render target.
  5362. scale.ApplyPerspectiveDepth(10);
  5363. scale.RotateAboutXAxis(15.0);
  5364. SetTransform(layer_.get(), scale);
  5365. CheckCanUseLCDText(LCDTextDisallowedReason::kNonIntegralTranslation,
  5366. "scale transform above render target");
  5367. // Skew.
  5368. gfx::Transform skew;
  5369. skew.Skew(10.0, 0.0);
  5370. SetTransform(layer_.get(), skew);
  5371. CheckCanUseLCDText(LCDTextDisallowedReason::kNonIntegralTranslation,
  5372. "skew transform above render target");
  5373. SetTransform(layer_.get(), gfx::Transform());
  5374. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "identity transform");
  5375. }
  5376. TEST_P(LCDTextTest, Opacity) {
  5377. // LCD-text is allowed with opacity paint property.
  5378. SetOpacity(layer_.get(), 0.5f);
  5379. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "opacity: 0.5");
  5380. SetOpacity(layer_.get(), 1.f);
  5381. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "opacity: 1.0");
  5382. }
  5383. TEST_P(LCDTextTest, ContentsNotOpaque) {
  5384. // Non-opaque content and opaque background.
  5385. layer_->SetContentsOpaque(false);
  5386. layer_->SetBackgroundColor(SkColors::kGreen);
  5387. CheckCanUseLCDText(LCDTextDisallowedReason::kContentsNotOpaque,
  5388. "contents not opaque", layer_);
  5389. CheckCanUseLCDText(LCDTextDisallowedReason::kNone,
  5390. "descedant of contents not opaque", descendant_);
  5391. // Non-opaque content and non-opaque background.
  5392. layer_->SetBackgroundColor({1.0f, 1.0f, 1.0f, 0.5f});
  5393. CheckCanUseLCDText(LCDTextDisallowedReason::kBackgroundColorNotOpaque,
  5394. "background not opaque", layer_);
  5395. CheckCanUseLCDText(LCDTextDisallowedReason::kNone,
  5396. "descendant of contents not opaque", descendant_);
  5397. layer_->SetContentsOpaque(true);
  5398. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "contents opaque");
  5399. }
  5400. TEST_P(LCDTextTest, WillChangeTransform) {
  5401. SetWillChangeTransform(layer_.get(), true);
  5402. CheckCanUseLCDText(LCDTextDisallowedReason::kWillChangeTransform,
  5403. "will-change:transform");
  5404. SetWillChangeTransform(layer_.get(), false);
  5405. CheckCanUseLCDText(LCDTextDisallowedReason::kNone,
  5406. "no will-change: transform");
  5407. }
  5408. TEST_P(LCDTextTest, Filter) {
  5409. FilterOperations blur_filter;
  5410. blur_filter.Append(FilterOperation::CreateBlurFilter(4.0f));
  5411. SetFilter(layer_.get(), blur_filter);
  5412. CheckCanUseLCDText(LCDTextDisallowedReason::kPixelOrColorEffect, "filter");
  5413. SetFilter(layer_.get(), FilterOperations());
  5414. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "no filter");
  5415. }
  5416. TEST_P(LCDTextTest, FilterAnimation) {
  5417. FilterOperations blur_filter;
  5418. blur_filter.Append(FilterOperation::CreateBlurFilter(4.0f));
  5419. SetFilter(layer_.get(), blur_filter);
  5420. CheckCanUseLCDText(LCDTextDisallowedReason::kPixelOrColorEffect, "filter");
  5421. GetEffectNode(layer_.get())->has_potential_filter_animation = true;
  5422. SetFilter(layer_.get(), FilterOperations());
  5423. CheckCanUseLCDText(LCDTextDisallowedReason::kPixelOrColorEffect,
  5424. "filter animation");
  5425. GetEffectNode(layer_.get())->has_potential_filter_animation = false;
  5426. SetFilter(layer_.get(), FilterOperations());
  5427. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "no filter");
  5428. }
  5429. TEST_P(LCDTextTest, BackdropFilter) {
  5430. FilterOperations backdrop_filter;
  5431. backdrop_filter.Append(FilterOperation::CreateBlurFilter(4.0f));
  5432. SetBackdropFilter(descendant_.get(), backdrop_filter);
  5433. UpdateDrawProperties(host_impl()->active_tree());
  5434. CheckCanUseLCDText(LCDTextDisallowedReason::kPixelOrColorEffect,
  5435. "backdrop-filter", layer_);
  5436. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "backdrop-filter",
  5437. descendant_);
  5438. SetBackdropFilter(descendant_.get(), FilterOperations());
  5439. UpdateDrawProperties(host_impl()->active_tree());
  5440. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "no backdrop-filter",
  5441. layer_);
  5442. }
  5443. TEST_P(LCDTextTest, BackdropFilterAnimation) {
  5444. FilterOperations backdrop_filter;
  5445. backdrop_filter.Append(FilterOperation::CreateBlurFilter(4.0f));
  5446. SetBackdropFilter(descendant_.get(), backdrop_filter);
  5447. UpdateDrawProperties(host_impl()->active_tree());
  5448. CheckCanUseLCDText(LCDTextDisallowedReason::kPixelOrColorEffect,
  5449. "backdrop-filter", layer_);
  5450. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "backdrop-filter",
  5451. descendant_);
  5452. GetEffectNode(descendant_.get())->has_potential_backdrop_filter_animation =
  5453. true;
  5454. SetBackdropFilter(descendant_.get(), FilterOperations());
  5455. UpdateDrawProperties(host_impl()->active_tree());
  5456. CheckCanUseLCDText(LCDTextDisallowedReason::kPixelOrColorEffect,
  5457. "backdrop-filter animation", layer_);
  5458. GetEffectNode(descendant_.get())->has_potential_backdrop_filter_animation =
  5459. false;
  5460. UpdateDrawProperties(host_impl()->active_tree());
  5461. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "no backdrop-filter",
  5462. layer_);
  5463. }
  5464. TEST_P(LCDTextTest, ContentsOpaqueForText) {
  5465. layer_->SetContentsOpaque(false);
  5466. layer_->SetBackgroundColor(SkColors::kGreen);
  5467. layer_->SetContentsOpaqueForText(true);
  5468. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "contents opaque for text",
  5469. layer_);
  5470. layer_->SetContentsOpaqueForText(false);
  5471. CheckCanUseLCDText(LCDTextDisallowedReason::kContentsNotOpaque,
  5472. "contents not opaque for text", layer_);
  5473. }
  5474. TEST_P(LCDTextTest, TransformAnimation) {
  5475. GetTransformNode(layer_.get())->has_potential_animation = true;
  5476. SetLocalTransformChanged(layer_.get());
  5477. CheckCanUseLCDText(LCDTextDisallowedReason::kTransformAnimation,
  5478. "transform animation");
  5479. GetTransformNode(layer_.get())->has_potential_animation = false;
  5480. SetLocalTransformChanged(layer_.get());
  5481. CheckCanUseLCDText(LCDTextDisallowedReason::kNone, "no transform animation");
  5482. }
  5483. } // namespace
  5484. } // namespace cc