Transaction_test.cpp 236 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <algorithm>
  17. #include <chrono>
  18. #include <cinttypes>
  19. #include <functional>
  20. #include <limits>
  21. #include <ostream>
  22. #include <thread>
  23. #include <gtest/gtest.h>
  24. #include <android/native_window.h>
  25. #include <binder/ProcessState.h>
  26. #include <gui/BufferItemConsumer.h>
  27. #include <gui/IProducerListener.h>
  28. #include <gui/ISurfaceComposer.h>
  29. #include <gui/LayerState.h>
  30. #include <gui/Surface.h>
  31. #include <gui/SurfaceComposerClient.h>
  32. #include <hardware/hwcomposer_defs.h>
  33. #include <private/android_filesystem_config.h>
  34. #include <private/gui/ComposerService.h>
  35. #include <ui/ColorSpace.h>
  36. #include <ui/DisplayInfo.h>
  37. #include <ui/Rect.h>
  38. #include <utils/String8.h>
  39. #include <math.h>
  40. #include <math/vec3.h>
  41. #include <sys/types.h>
  42. #include <unistd.h>
  43. #include "BufferGenerator.h"
  44. namespace android {
  45. namespace {
  46. struct Color {
  47. uint8_t r;
  48. uint8_t g;
  49. uint8_t b;
  50. uint8_t a;
  51. static const Color RED;
  52. static const Color GREEN;
  53. static const Color BLUE;
  54. static const Color WHITE;
  55. static const Color BLACK;
  56. static const Color TRANSPARENT;
  57. };
  58. const Color Color::RED{255, 0, 0, 255};
  59. const Color Color::GREEN{0, 255, 0, 255};
  60. const Color Color::BLUE{0, 0, 255, 255};
  61. const Color Color::WHITE{255, 255, 255, 255};
  62. const Color Color::BLACK{0, 0, 0, 255};
  63. const Color Color::TRANSPARENT{0, 0, 0, 0};
  64. using android::hardware::graphics::common::V1_1::BufferUsage;
  65. using namespace std::chrono_literals;
  66. std::ostream& operator<<(std::ostream& os, const Color& color) {
  67. os << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", " << int(color.a);
  68. return os;
  69. }
  70. // Fill a region with the specified color.
  71. void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
  72. const Color& color) {
  73. Rect r(0, 0, buffer.width, buffer.height);
  74. if (!r.intersect(rect, &r)) {
  75. return;
  76. }
  77. int32_t width = r.right - r.left;
  78. int32_t height = r.bottom - r.top;
  79. for (int32_t row = 0; row < height; row++) {
  80. uint8_t* dst =
  81. static_cast<uint8_t*>(buffer.bits) + (buffer.stride * (r.top + row) + r.left) * 4;
  82. for (int32_t column = 0; column < width; column++) {
  83. dst[0] = color.r;
  84. dst[1] = color.g;
  85. dst[2] = color.b;
  86. dst[3] = color.a;
  87. dst += 4;
  88. }
  89. }
  90. }
  91. // Fill a region with the specified color.
  92. void fillGraphicBufferColor(const sp<GraphicBuffer>& buffer, const Rect& rect, const Color& color) {
  93. Rect r(0, 0, buffer->width, buffer->height);
  94. if (!r.intersect(rect, &r)) {
  95. return;
  96. }
  97. int32_t width = r.right - r.left;
  98. int32_t height = r.bottom - r.top;
  99. uint8_t* pixels;
  100. buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
  101. reinterpret_cast<void**>(&pixels));
  102. for (int32_t row = 0; row < height; row++) {
  103. uint8_t* dst = pixels + (buffer->getStride() * (r.top + row) + r.left) * 4;
  104. for (int32_t column = 0; column < width; column++) {
  105. dst[0] = color.r;
  106. dst[1] = color.g;
  107. dst[2] = color.b;
  108. dst[3] = color.a;
  109. dst += 4;
  110. }
  111. }
  112. buffer->unlock();
  113. }
  114. // Check if a region has the specified color.
  115. void expectBufferColor(const sp<GraphicBuffer>& outBuffer, uint8_t* pixels, const Rect& rect,
  116. const Color& color, uint8_t tolerance) {
  117. int32_t x = rect.left;
  118. int32_t y = rect.top;
  119. int32_t width = rect.right - rect.left;
  120. int32_t height = rect.bottom - rect.top;
  121. int32_t bufferWidth = int32_t(outBuffer->getWidth());
  122. int32_t bufferHeight = int32_t(outBuffer->getHeight());
  123. if (x + width > bufferWidth) {
  124. x = std::min(x, bufferWidth);
  125. width = bufferWidth - x;
  126. }
  127. if (y + height > bufferHeight) {
  128. y = std::min(y, bufferHeight);
  129. height = bufferHeight - y;
  130. }
  131. auto colorCompare = [tolerance](uint8_t a, uint8_t b) {
  132. uint8_t tmp = a >= b ? a - b : b - a;
  133. return tmp <= tolerance;
  134. };
  135. for (int32_t j = 0; j < height; j++) {
  136. const uint8_t* src = pixels + (outBuffer->getStride() * (y + j) + x) * 4;
  137. for (int32_t i = 0; i < width; i++) {
  138. const uint8_t expected[4] = {color.r, color.g, color.b, color.a};
  139. EXPECT_TRUE(std::equal(src, src + 4, expected, colorCompare))
  140. << "pixel @ (" << x + i << ", " << y + j << "): "
  141. << "expected (" << color << "), "
  142. << "got (" << Color{src[0], src[1], src[2], src[3]} << ")";
  143. src += 4;
  144. }
  145. }
  146. }
  147. } // anonymous namespace
  148. using Transaction = SurfaceComposerClient::Transaction;
  149. // Fill an RGBA_8888 formatted surface with a single color.
  150. static void fillSurfaceRGBA8(const sp<SurfaceControl>& sc, uint8_t r, uint8_t g, uint8_t b,
  151. bool unlock = true) {
  152. ANativeWindow_Buffer outBuffer;
  153. sp<Surface> s = sc->getSurface();
  154. ASSERT_TRUE(s != nullptr);
  155. ASSERT_EQ(NO_ERROR, s->lock(&outBuffer, nullptr));
  156. uint8_t* img = reinterpret_cast<uint8_t*>(outBuffer.bits);
  157. for (int y = 0; y < outBuffer.height; y++) {
  158. for (int x = 0; x < outBuffer.width; x++) {
  159. uint8_t* pixel = img + (4 * (y * outBuffer.stride + x));
  160. pixel[0] = r;
  161. pixel[1] = g;
  162. pixel[2] = b;
  163. pixel[3] = 255;
  164. }
  165. }
  166. if (unlock) {
  167. ASSERT_EQ(NO_ERROR, s->unlockAndPost());
  168. }
  169. }
  170. // A ScreenCapture is a screenshot from SurfaceFlinger that can be used to check
  171. // individual pixel values for testing purposes.
  172. class ScreenCapture : public RefBase {
  173. public:
  174. static void captureScreen(std::unique_ptr<ScreenCapture>* sc) {
  175. captureScreen(sc, SurfaceComposerClient::getInternalDisplayToken());
  176. }
  177. static void captureScreen(std::unique_ptr<ScreenCapture>* sc, sp<IBinder> displayToken) {
  178. const auto sf = ComposerService::getComposerService();
  179. SurfaceComposerClient::Transaction().apply(true);
  180. sp<GraphicBuffer> outBuffer;
  181. ASSERT_EQ(NO_ERROR, sf->captureScreen(displayToken, &outBuffer, Rect(), 0, 0, false));
  182. *sc = std::make_unique<ScreenCapture>(outBuffer);
  183. }
  184. static void captureLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
  185. Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
  186. sp<ISurfaceComposer> sf(ComposerService::getComposerService());
  187. SurfaceComposerClient::Transaction().apply(true);
  188. sp<GraphicBuffer> outBuffer;
  189. ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale));
  190. *sc = std::make_unique<ScreenCapture>(outBuffer);
  191. }
  192. static void captureChildLayers(std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
  193. Rect crop = Rect::EMPTY_RECT, float frameScale = 1.0) {
  194. sp<ISurfaceComposer> sf(ComposerService::getComposerService());
  195. SurfaceComposerClient::Transaction().apply(true);
  196. sp<GraphicBuffer> outBuffer;
  197. ASSERT_EQ(NO_ERROR, sf->captureLayers(parentHandle, &outBuffer, crop, frameScale, true));
  198. *sc = std::make_unique<ScreenCapture>(outBuffer);
  199. }
  200. static void captureChildLayersExcluding(
  201. std::unique_ptr<ScreenCapture>* sc, sp<IBinder>& parentHandle,
  202. std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>> excludeLayers) {
  203. sp<ISurfaceComposer> sf(ComposerService::getComposerService());
  204. SurfaceComposerClient::Transaction().apply(true);
  205. sp<GraphicBuffer> outBuffer;
  206. ASSERT_EQ(NO_ERROR,
  207. sf->captureLayers(parentHandle, &outBuffer, ui::Dataspace::V0_SRGB,
  208. ui::PixelFormat::RGBA_8888, Rect::EMPTY_RECT, excludeLayers,
  209. 1.0f, true));
  210. *sc = std::make_unique<ScreenCapture>(outBuffer);
  211. }
  212. void expectColor(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
  213. ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
  214. expectBufferColor(mOutBuffer, mPixels, rect, color, tolerance);
  215. }
  216. void expectBorder(const Rect& rect, const Color& color, uint8_t tolerance = 0) {
  217. ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
  218. const bool leftBorder = rect.left > 0;
  219. const bool topBorder = rect.top > 0;
  220. const bool rightBorder = rect.right < int32_t(mOutBuffer->getWidth());
  221. const bool bottomBorder = rect.bottom < int32_t(mOutBuffer->getHeight());
  222. if (topBorder) {
  223. Rect top(rect.left, rect.top - 1, rect.right, rect.top);
  224. if (leftBorder) {
  225. top.left -= 1;
  226. }
  227. if (rightBorder) {
  228. top.right += 1;
  229. }
  230. expectColor(top, color, tolerance);
  231. }
  232. if (leftBorder) {
  233. Rect left(rect.left - 1, rect.top, rect.left, rect.bottom);
  234. expectColor(left, color, tolerance);
  235. }
  236. if (rightBorder) {
  237. Rect right(rect.right, rect.top, rect.right + 1, rect.bottom);
  238. expectColor(right, color, tolerance);
  239. }
  240. if (bottomBorder) {
  241. Rect bottom(rect.left, rect.bottom, rect.right, rect.bottom + 1);
  242. if (leftBorder) {
  243. bottom.left -= 1;
  244. }
  245. if (rightBorder) {
  246. bottom.right += 1;
  247. }
  248. expectColor(bottom, color, tolerance);
  249. }
  250. }
  251. void expectQuadrant(const Rect& rect, const Color& topLeft, const Color& topRight,
  252. const Color& bottomLeft, const Color& bottomRight, bool filtered = false,
  253. uint8_t tolerance = 0) {
  254. ASSERT_TRUE((rect.right - rect.left) % 2 == 0 && (rect.bottom - rect.top) % 2 == 0);
  255. const int32_t centerX = rect.left + (rect.right - rect.left) / 2;
  256. const int32_t centerY = rect.top + (rect.bottom - rect.top) / 2;
  257. // avoid checking borders due to unspecified filtering behavior
  258. const int32_t offsetX = filtered ? 2 : 0;
  259. const int32_t offsetY = filtered ? 2 : 0;
  260. expectColor(Rect(rect.left, rect.top, centerX - offsetX, centerY - offsetY), topLeft,
  261. tolerance);
  262. expectColor(Rect(centerX + offsetX, rect.top, rect.right, centerY - offsetY), topRight,
  263. tolerance);
  264. expectColor(Rect(rect.left, centerY + offsetY, centerX - offsetX, rect.bottom), bottomLeft,
  265. tolerance);
  266. expectColor(Rect(centerX + offsetX, centerY + offsetY, rect.right, rect.bottom),
  267. bottomRight, tolerance);
  268. }
  269. void checkPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) {
  270. ASSERT_EQ(HAL_PIXEL_FORMAT_RGBA_8888, mOutBuffer->getPixelFormat());
  271. const uint8_t* pixel = mPixels + (4 * (y * mOutBuffer->getStride() + x));
  272. if (r != pixel[0] || g != pixel[1] || b != pixel[2]) {
  273. String8 err(String8::format("pixel @ (%3d, %3d): "
  274. "expected [%3d, %3d, %3d], got [%3d, %3d, %3d]",
  275. x, y, r, g, b, pixel[0], pixel[1], pixel[2]));
  276. EXPECT_EQ(String8(), err) << err.string();
  277. }
  278. }
  279. void expectFGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 195, 63, 63); }
  280. void expectBGColor(uint32_t x, uint32_t y) { checkPixel(x, y, 63, 63, 195); }
  281. void expectChildColor(uint32_t x, uint32_t y) { checkPixel(x, y, 200, 200, 200); }
  282. explicit ScreenCapture(const sp<GraphicBuffer>& outBuffer) : mOutBuffer(outBuffer) {
  283. mOutBuffer->lock(GRALLOC_USAGE_SW_READ_OFTEN, reinterpret_cast<void**>(&mPixels));
  284. }
  285. ~ScreenCapture() { mOutBuffer->unlock(); }
  286. private:
  287. sp<GraphicBuffer> mOutBuffer;
  288. uint8_t* mPixels = nullptr;
  289. };
  290. class LayerTransactionTest : public ::testing::Test {
  291. protected:
  292. void SetUp() override {
  293. mClient = new SurfaceComposerClient;
  294. ASSERT_EQ(NO_ERROR, mClient->initCheck()) << "failed to create SurfaceComposerClient";
  295. ASSERT_NO_FATAL_FAILURE(SetUpDisplay());
  296. sp<ISurfaceComposer> sf(ComposerService::getComposerService());
  297. ASSERT_NO_FATAL_FAILURE(sf->getColorManagement(&mColorManagementUsed));
  298. }
  299. virtual void TearDown() {
  300. mBlackBgSurface = 0;
  301. mClient->dispose();
  302. mClient = 0;
  303. }
  304. virtual sp<SurfaceControl> createLayer(const sp<SurfaceComposerClient>& client,
  305. const char* name, uint32_t width, uint32_t height,
  306. uint32_t flags = 0, SurfaceControl* parent = nullptr) {
  307. auto layer =
  308. createSurface(client, name, width, height, PIXEL_FORMAT_RGBA_8888, flags, parent);
  309. Transaction t;
  310. t.setLayerStack(layer, mDisplayLayerStack).setLayer(layer, mLayerZBase);
  311. status_t error = t.apply();
  312. if (error != NO_ERROR) {
  313. ADD_FAILURE() << "failed to initialize SurfaceControl";
  314. layer.clear();
  315. }
  316. return layer;
  317. }
  318. virtual sp<SurfaceControl> createSurface(const sp<SurfaceComposerClient>& client,
  319. const char* name, uint32_t width, uint32_t height,
  320. PixelFormat format, uint32_t flags,
  321. SurfaceControl* parent = nullptr) {
  322. auto layer = client->createSurface(String8(name), width, height, format, flags, parent);
  323. EXPECT_NE(nullptr, layer.get()) << "failed to create SurfaceControl";
  324. return layer;
  325. }
  326. virtual sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
  327. uint32_t flags = 0, SurfaceControl* parent = nullptr) {
  328. return createLayer(mClient, name, width, height, flags, parent);
  329. }
  330. sp<SurfaceControl> createColorLayer(const char* name, const Color& color,
  331. SurfaceControl* parent = nullptr) {
  332. auto colorLayer = createSurface(mClient, name, 0 /* buffer width */, 0 /* buffer height */,
  333. PIXEL_FORMAT_RGBA_8888,
  334. ISurfaceComposerClient::eFXSurfaceColor, parent);
  335. asTransaction([&](Transaction& t) {
  336. t.setColor(colorLayer, half3{color.r / 255.0f, color.g / 255.0f, color.b / 255.0f});
  337. t.setAlpha(colorLayer, color.a / 255.0f);
  338. });
  339. return colorLayer;
  340. }
  341. ANativeWindow_Buffer getBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
  342. // wait for previous transactions (such as setSize) to complete
  343. Transaction().apply(true);
  344. ANativeWindow_Buffer buffer = {};
  345. EXPECT_EQ(NO_ERROR, layer->getSurface()->lock(&buffer, nullptr));
  346. return buffer;
  347. }
  348. void postBufferQueueLayerBuffer(const sp<SurfaceControl>& layer) {
  349. ASSERT_EQ(NO_ERROR, layer->getSurface()->unlockAndPost());
  350. // wait for the newly posted buffer to be latched
  351. waitForLayerBuffers();
  352. }
  353. virtual void fillBufferQueueLayerColor(const sp<SurfaceControl>& layer, const Color& color,
  354. int32_t bufferWidth, int32_t bufferHeight) {
  355. ANativeWindow_Buffer buffer;
  356. ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
  357. fillANativeWindowBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
  358. postBufferQueueLayerBuffer(layer);
  359. }
  360. virtual void fillBufferStateLayerColor(const sp<SurfaceControl>& layer, const Color& color,
  361. int32_t bufferWidth, int32_t bufferHeight) {
  362. sp<GraphicBuffer> buffer =
  363. new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
  364. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  365. BufferUsage::COMPOSER_OVERLAY,
  366. "test");
  367. fillGraphicBufferColor(buffer, Rect(0, 0, bufferWidth, bufferHeight), color);
  368. Transaction().setBuffer(layer, buffer).apply();
  369. }
  370. void fillLayerColor(uint32_t mLayerType, const sp<SurfaceControl>& layer, const Color& color,
  371. int32_t bufferWidth, int32_t bufferHeight) {
  372. switch (mLayerType) {
  373. case ISurfaceComposerClient::eFXSurfaceBufferQueue:
  374. fillBufferQueueLayerColor(layer, color, bufferWidth, bufferHeight);
  375. break;
  376. case ISurfaceComposerClient::eFXSurfaceBufferState:
  377. fillBufferStateLayerColor(layer, color, bufferWidth, bufferHeight);
  378. break;
  379. default:
  380. ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
  381. }
  382. }
  383. void fillLayerQuadrant(uint32_t mLayerType, const sp<SurfaceControl>& layer,
  384. int32_t bufferWidth, int32_t bufferHeight, const Color& topLeft,
  385. const Color& topRight, const Color& bottomLeft,
  386. const Color& bottomRight) {
  387. switch (mLayerType) {
  388. case ISurfaceComposerClient::eFXSurfaceBufferQueue:
  389. fillBufferQueueLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
  390. bottomLeft, bottomRight);
  391. break;
  392. case ISurfaceComposerClient::eFXSurfaceBufferState:
  393. fillBufferStateLayerQuadrant(layer, bufferWidth, bufferHeight, topLeft, topRight,
  394. bottomLeft, bottomRight);
  395. break;
  396. default:
  397. ASSERT_TRUE(false) << "unsupported layer type: " << mLayerType;
  398. }
  399. }
  400. virtual void fillBufferQueueLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
  401. int32_t bufferHeight, const Color& topLeft,
  402. const Color& topRight, const Color& bottomLeft,
  403. const Color& bottomRight) {
  404. ANativeWindow_Buffer buffer;
  405. ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
  406. ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
  407. const int32_t halfW = bufferWidth / 2;
  408. const int32_t halfH = bufferHeight / 2;
  409. fillANativeWindowBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
  410. fillANativeWindowBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
  411. fillANativeWindowBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
  412. fillANativeWindowBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight),
  413. bottomRight);
  414. postBufferQueueLayerBuffer(layer);
  415. }
  416. virtual void fillBufferStateLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
  417. int32_t bufferHeight, const Color& topLeft,
  418. const Color& topRight, const Color& bottomLeft,
  419. const Color& bottomRight) {
  420. sp<GraphicBuffer> buffer =
  421. new GraphicBuffer(bufferWidth, bufferHeight, PIXEL_FORMAT_RGBA_8888, 1,
  422. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  423. BufferUsage::COMPOSER_OVERLAY,
  424. "test");
  425. ASSERT_TRUE(bufferWidth % 2 == 0 && bufferHeight % 2 == 0);
  426. const int32_t halfW = bufferWidth / 2;
  427. const int32_t halfH = bufferHeight / 2;
  428. fillGraphicBufferColor(buffer, Rect(0, 0, halfW, halfH), topLeft);
  429. fillGraphicBufferColor(buffer, Rect(halfW, 0, bufferWidth, halfH), topRight);
  430. fillGraphicBufferColor(buffer, Rect(0, halfH, halfW, bufferHeight), bottomLeft);
  431. fillGraphicBufferColor(buffer, Rect(halfW, halfH, bufferWidth, bufferHeight), bottomRight);
  432. Transaction().setBuffer(layer, buffer).setSize(layer, bufferWidth, bufferHeight).apply();
  433. }
  434. std::unique_ptr<ScreenCapture> screenshot() {
  435. std::unique_ptr<ScreenCapture> screenshot;
  436. ScreenCapture::captureScreen(&screenshot);
  437. return screenshot;
  438. }
  439. void asTransaction(const std::function<void(Transaction&)>& exec) {
  440. Transaction t;
  441. exec(t);
  442. t.apply(true);
  443. }
  444. static status_t getBuffer(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
  445. static BufferGenerator bufferGenerator;
  446. return bufferGenerator.get(outBuffer, outFence);
  447. }
  448. sp<SurfaceComposerClient> mClient;
  449. sp<IBinder> mDisplay;
  450. uint32_t mDisplayWidth;
  451. uint32_t mDisplayHeight;
  452. uint32_t mDisplayLayerStack;
  453. Rect mDisplayRect = Rect::INVALID_RECT;
  454. // leave room for ~256 layers
  455. const int32_t mLayerZBase = std::numeric_limits<int32_t>::max() - 256;
  456. sp<SurfaceControl> mBlackBgSurface;
  457. bool mColorManagementUsed;
  458. private:
  459. void SetUpDisplay() {
  460. mDisplay = mClient->getInternalDisplayToken();
  461. ASSERT_FALSE(mDisplay == nullptr) << "failed to get display";
  462. // get display width/height
  463. DisplayInfo info;
  464. ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplay, &info));
  465. mDisplayWidth = info.w;
  466. mDisplayHeight = info.h;
  467. mDisplayRect =
  468. Rect(static_cast<int32_t>(mDisplayWidth), static_cast<int32_t>(mDisplayHeight));
  469. // After a new buffer is queued, SurfaceFlinger is notified and will
  470. // latch the new buffer on next vsync. Let's heuristically wait for 3
  471. // vsyncs.
  472. mBufferPostDelay = int32_t(1e6 / info.fps) * 3;
  473. mDisplayLayerStack = 0;
  474. mBlackBgSurface =
  475. createSurface(mClient, "BaseSurface", 0 /* buffer width */, 0 /* buffer height */,
  476. PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
  477. // set layer stack (b/68888219)
  478. Transaction t;
  479. t.setDisplayLayerStack(mDisplay, mDisplayLayerStack);
  480. t.setCrop_legacy(mBlackBgSurface, Rect(0, 0, mDisplayWidth, mDisplayHeight));
  481. t.setLayerStack(mBlackBgSurface, mDisplayLayerStack);
  482. t.setColor(mBlackBgSurface, half3{0, 0, 0});
  483. t.setLayer(mBlackBgSurface, mLayerZBase);
  484. t.apply();
  485. }
  486. void waitForLayerBuffers() {
  487. // Request an empty transaction to get applied synchronously to ensure the buffer is
  488. // latched.
  489. Transaction().apply(true);
  490. usleep(mBufferPostDelay);
  491. }
  492. int32_t mBufferPostDelay;
  493. friend class LayerRenderPathTestHarness;
  494. };
  495. enum class RenderPath { SCREENSHOT, VIRTUAL_DISPLAY };
  496. class LayerRenderPathTestHarness {
  497. public:
  498. LayerRenderPathTestHarness(LayerTransactionTest* delegate, RenderPath renderPath)
  499. : mDelegate(delegate), mRenderPath(renderPath) {}
  500. std::unique_ptr<ScreenCapture> getScreenCapture() {
  501. switch (mRenderPath) {
  502. case RenderPath::SCREENSHOT:
  503. return mDelegate->screenshot();
  504. case RenderPath::VIRTUAL_DISPLAY:
  505. const auto mainDisplay = SurfaceComposerClient::getInternalDisplayToken();
  506. DisplayInfo mainDisplayInfo;
  507. SurfaceComposerClient::getDisplayInfo(mainDisplay, &mainDisplayInfo);
  508. sp<IBinder> vDisplay;
  509. sp<IGraphicBufferProducer> producer;
  510. sp<IGraphicBufferConsumer> consumer;
  511. sp<BufferItemConsumer> itemConsumer;
  512. BufferQueue::createBufferQueue(&producer, &consumer);
  513. consumer->setConsumerName(String8("Virtual disp consumer"));
  514. consumer->setDefaultBufferSize(mainDisplayInfo.w, mainDisplayInfo.h);
  515. itemConsumer = new BufferItemConsumer(consumer,
  516. // Sample usage bits from screenrecord
  517. GRALLOC_USAGE_HW_VIDEO_ENCODER |
  518. GRALLOC_USAGE_SW_READ_OFTEN);
  519. vDisplay = SurfaceComposerClient::createDisplay(String8("VirtualDisplay"),
  520. false /*secure*/);
  521. SurfaceComposerClient::Transaction t;
  522. t.setDisplaySurface(vDisplay, producer);
  523. t.setDisplayLayerStack(vDisplay, 0);
  524. t.setDisplayProjection(vDisplay, mainDisplayInfo.orientation,
  525. Rect(mainDisplayInfo.viewportW, mainDisplayInfo.viewportH),
  526. Rect(mainDisplayInfo.w, mainDisplayInfo.h));
  527. t.apply();
  528. SurfaceComposerClient::Transaction().apply(true);
  529. BufferItem item;
  530. itemConsumer->acquireBuffer(&item, 0, true);
  531. auto sc = std::make_unique<ScreenCapture>(item.mGraphicBuffer);
  532. itemConsumer->releaseBuffer(item);
  533. SurfaceComposerClient::destroyDisplay(vDisplay);
  534. return sc;
  535. }
  536. }
  537. protected:
  538. LayerTransactionTest* mDelegate;
  539. RenderPath mRenderPath;
  540. };
  541. class LayerTypeTransactionHarness : public LayerTransactionTest {
  542. public:
  543. LayerTypeTransactionHarness(uint32_t layerType) : mLayerType(layerType) {}
  544. sp<SurfaceControl> createLayer(const char* name, uint32_t width, uint32_t height,
  545. uint32_t flags = 0, SurfaceControl* parent = nullptr) {
  546. // if the flags already have a layer type specified, return an error
  547. if (flags & ISurfaceComposerClient::eFXSurfaceMask) {
  548. return nullptr;
  549. }
  550. return LayerTransactionTest::createLayer(name, width, height, flags | mLayerType, parent);
  551. }
  552. void fillLayerColor(const sp<SurfaceControl>& layer, const Color& color, int32_t bufferWidth,
  553. int32_t bufferHeight) {
  554. ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerColor(mLayerType, layer, color,
  555. bufferWidth, bufferHeight));
  556. }
  557. void fillLayerQuadrant(const sp<SurfaceControl>& layer, int32_t bufferWidth,
  558. int32_t bufferHeight, const Color& topLeft, const Color& topRight,
  559. const Color& bottomLeft, const Color& bottomRight) {
  560. ASSERT_NO_FATAL_FAILURE(LayerTransactionTest::fillLayerQuadrant(mLayerType, layer,
  561. bufferWidth, bufferHeight,
  562. topLeft, topRight,
  563. bottomLeft, bottomRight));
  564. }
  565. protected:
  566. uint32_t mLayerType;
  567. };
  568. class LayerTypeTransactionTest : public LayerTypeTransactionHarness,
  569. public ::testing::WithParamInterface<uint32_t> {
  570. public:
  571. LayerTypeTransactionTest() : LayerTypeTransactionHarness(GetParam()) {}
  572. };
  573. class LayerTypeAndRenderTypeTransactionTest
  574. : public LayerTypeTransactionHarness,
  575. public ::testing::WithParamInterface<std::tuple<uint32_t, RenderPath>> {
  576. public:
  577. LayerTypeAndRenderTypeTransactionTest()
  578. : LayerTypeTransactionHarness(std::get<0>(GetParam())),
  579. mRenderPathHarness(LayerRenderPathTestHarness(this, std::get<1>(GetParam()))) {}
  580. std::unique_ptr<ScreenCapture> getScreenCapture() {
  581. return mRenderPathHarness.getScreenCapture();
  582. }
  583. protected:
  584. LayerRenderPathTestHarness mRenderPathHarness;
  585. };
  586. // Environment for starting up binder threads. This is required for testing
  587. // virtual displays, as BufferQueue parameters may be queried over binder.
  588. class BinderEnvironment : public ::testing::Environment {
  589. public:
  590. void SetUp() override { ProcessState::self()->startThreadPool(); }
  591. };
  592. ::testing::Environment* const binderEnv =
  593. ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
  594. class LayerRenderTypeTransactionTest : public LayerTransactionTest,
  595. public ::testing::WithParamInterface<RenderPath> {
  596. public:
  597. LayerRenderTypeTransactionTest() : mHarness(LayerRenderPathTestHarness(this, GetParam())) {}
  598. std::unique_ptr<ScreenCapture> getScreenCapture() { return mHarness.getScreenCapture(); }
  599. void setRelativeZBasicHelper(uint32_t layerType);
  600. void setRelativeZGroupHelper(uint32_t layerType);
  601. void setAlphaBasicHelper(uint32_t layerType);
  602. void setBackgroundColorHelper(uint32_t layerType, bool priorColor, bool bufferFill, float alpha,
  603. Color finalColor);
  604. protected:
  605. LayerRenderPathTestHarness mHarness;
  606. };
  607. INSTANTIATE_TEST_CASE_P(
  608. LayerTypeAndRenderTypeTransactionTests, LayerTypeAndRenderTypeTransactionTest,
  609. ::testing::Combine(
  610. ::testing::Values(
  611. static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
  612. static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)),
  613. ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT)));
  614. INSTANTIATE_TEST_CASE_P(LayerRenderTypeTransactionTests, LayerRenderTypeTransactionTest,
  615. ::testing::Values(RenderPath::VIRTUAL_DISPLAY, RenderPath::SCREENSHOT));
  616. INSTANTIATE_TEST_CASE_P(
  617. LayerTypeTransactionTests, LayerTypeTransactionTest,
  618. ::testing::Values(static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferQueue),
  619. static_cast<uint32_t>(ISurfaceComposerClient::eFXSurfaceBufferState)));
  620. TEST_P(LayerRenderTypeTransactionTest, SetPositionBasic_BufferQueue) {
  621. sp<SurfaceControl> layer;
  622. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  623. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  624. {
  625. SCOPED_TRACE("default position");
  626. const Rect rect(0, 0, 32, 32);
  627. auto shot = getScreenCapture();
  628. shot->expectColor(rect, Color::RED);
  629. shot->expectBorder(rect, Color::BLACK);
  630. }
  631. Transaction().setPosition(layer, 5, 10).apply();
  632. {
  633. SCOPED_TRACE("new position");
  634. const Rect rect(5, 10, 37, 42);
  635. auto shot = getScreenCapture();
  636. shot->expectColor(rect, Color::RED);
  637. shot->expectBorder(rect, Color::BLACK);
  638. }
  639. }
  640. TEST_P(LayerRenderTypeTransactionTest, SetPositionRounding_BufferQueue) {
  641. sp<SurfaceControl> layer;
  642. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  643. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  644. // GLES requires only 4 bits of subpixel precision during rasterization
  645. // XXX GLES composition does not match HWC composition due to precision
  646. // loss (b/69315223)
  647. const float epsilon = 1.0f / 16.0f;
  648. Transaction().setPosition(layer, 0.5f - epsilon, 0.5f - epsilon).apply();
  649. {
  650. SCOPED_TRACE("rounding down");
  651. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  652. }
  653. Transaction().setPosition(layer, 0.5f + epsilon, 0.5f + epsilon).apply();
  654. {
  655. SCOPED_TRACE("rounding up");
  656. getScreenCapture()->expectColor(Rect(1, 1, 33, 33), Color::RED);
  657. }
  658. }
  659. TEST_P(LayerRenderTypeTransactionTest, SetPositionOutOfBounds_BufferQueue) {
  660. sp<SurfaceControl> layer;
  661. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  662. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  663. Transaction().setPosition(layer, -32, -32).apply();
  664. {
  665. SCOPED_TRACE("negative coordinates");
  666. getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
  667. }
  668. Transaction().setPosition(layer, mDisplayWidth, mDisplayHeight).apply();
  669. {
  670. SCOPED_TRACE("positive coordinates");
  671. getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
  672. }
  673. }
  674. TEST_P(LayerRenderTypeTransactionTest, SetPositionPartiallyOutOfBounds_BufferQueue) {
  675. sp<SurfaceControl> layer;
  676. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  677. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  678. // partially out of bounds
  679. Transaction().setPosition(layer, -30, -30).apply();
  680. {
  681. SCOPED_TRACE("negative coordinates");
  682. getScreenCapture()->expectColor(Rect(0, 0, 2, 2), Color::RED);
  683. }
  684. Transaction().setPosition(layer, mDisplayWidth - 2, mDisplayHeight - 2).apply();
  685. {
  686. SCOPED_TRACE("positive coordinates");
  687. getScreenCapture()->expectColor(Rect(mDisplayWidth - 2, mDisplayHeight - 2, mDisplayWidth,
  688. mDisplayHeight),
  689. Color::RED);
  690. }
  691. }
  692. TEST_P(LayerRenderTypeTransactionTest, SetPositionWithResize_BufferQueue) {
  693. sp<SurfaceControl> layer;
  694. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  695. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  696. // setPosition is applied immediately by default, with or without resize
  697. // pending
  698. Transaction().setPosition(layer, 5, 10).setSize(layer, 64, 64).apply();
  699. {
  700. SCOPED_TRACE("resize pending");
  701. auto shot = getScreenCapture();
  702. const Rect rect(5, 10, 37, 42);
  703. shot->expectColor(rect, Color::RED);
  704. shot->expectBorder(rect, Color::BLACK);
  705. }
  706. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
  707. {
  708. SCOPED_TRACE("resize applied");
  709. getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
  710. }
  711. }
  712. TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResize_BufferQueue) {
  713. sp<SurfaceControl> layer;
  714. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  715. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  716. // request setPosition to be applied with the next resize
  717. Transaction().setPosition(layer, 5, 10).setGeometryAppliesWithResize(layer).apply();
  718. {
  719. SCOPED_TRACE("new position pending");
  720. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  721. }
  722. Transaction().setPosition(layer, 15, 20).apply();
  723. {
  724. SCOPED_TRACE("pending new position modified");
  725. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  726. }
  727. Transaction().setSize(layer, 64, 64).apply();
  728. {
  729. SCOPED_TRACE("resize pending");
  730. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  731. }
  732. // finally resize and latch the buffer
  733. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
  734. {
  735. SCOPED_TRACE("new position applied");
  736. getScreenCapture()->expectColor(Rect(15, 20, 79, 84), Color::RED);
  737. }
  738. }
  739. TEST_P(LayerRenderTypeTransactionTest, SetPositionWithNextResizeScaleToWindow_BufferQueue) {
  740. sp<SurfaceControl> layer;
  741. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  742. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  743. // setPosition is not immediate even with SCALE_TO_WINDOW override
  744. Transaction()
  745. .setPosition(layer, 5, 10)
  746. .setSize(layer, 64, 64)
  747. .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
  748. .setGeometryAppliesWithResize(layer)
  749. .apply();
  750. {
  751. SCOPED_TRACE("new position pending");
  752. getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
  753. }
  754. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
  755. {
  756. SCOPED_TRACE("new position applied");
  757. getScreenCapture()->expectColor(Rect(5, 10, 69, 74), Color::RED);
  758. }
  759. }
  760. TEST_P(LayerRenderTypeTransactionTest, SetSizeBasic_BufferQueue) {
  761. sp<SurfaceControl> layer;
  762. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  763. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  764. Transaction().setSize(layer, 64, 64).apply();
  765. {
  766. SCOPED_TRACE("resize pending");
  767. auto shot = getScreenCapture();
  768. const Rect rect(0, 0, 32, 32);
  769. shot->expectColor(rect, Color::RED);
  770. shot->expectBorder(rect, Color::BLACK);
  771. }
  772. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
  773. {
  774. SCOPED_TRACE("resize applied");
  775. auto shot = getScreenCapture();
  776. const Rect rect(0, 0, 64, 64);
  777. shot->expectColor(rect, Color::RED);
  778. shot->expectBorder(rect, Color::BLACK);
  779. }
  780. }
  781. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetSizeInvalid) {
  782. // cannot test robustness against invalid sizes (zero or really huge)
  783. }
  784. TEST_P(LayerRenderTypeTransactionTest, SetSizeWithScaleToWindow_BufferQueue) {
  785. sp<SurfaceControl> layer;
  786. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  787. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  788. // setSize is immediate with SCALE_TO_WINDOW, unlike setPosition
  789. Transaction()
  790. .setSize(layer, 64, 64)
  791. .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
  792. .apply();
  793. getScreenCapture()->expectColor(Rect(0, 0, 64, 64), Color::RED);
  794. }
  795. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZBasic) {
  796. sp<SurfaceControl> layerR;
  797. sp<SurfaceControl> layerG;
  798. ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
  799. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
  800. ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
  801. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
  802. Transaction().setLayer(layerR, mLayerZBase + 1).apply();
  803. {
  804. SCOPED_TRACE("layerR");
  805. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  806. }
  807. Transaction().setLayer(layerG, mLayerZBase + 2).apply();
  808. {
  809. SCOPED_TRACE("layerG");
  810. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
  811. }
  812. }
  813. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetZNegative) {
  814. sp<SurfaceControl> parent =
  815. LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
  816. ISurfaceComposerClient::eFXSurfaceContainer);
  817. Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
  818. sp<SurfaceControl> layerR;
  819. sp<SurfaceControl> layerG;
  820. ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
  821. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
  822. ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
  823. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
  824. Transaction()
  825. .reparent(layerR, parent->getHandle())
  826. .reparent(layerG, parent->getHandle())
  827. .apply();
  828. Transaction().setLayer(layerR, -1).setLayer(layerG, -2).apply();
  829. {
  830. SCOPED_TRACE("layerR");
  831. auto shot = getScreenCapture();
  832. shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
  833. }
  834. Transaction().setLayer(layerR, -3).apply();
  835. {
  836. SCOPED_TRACE("layerG");
  837. auto shot = getScreenCapture();
  838. shot->expectColor(Rect(0, 0, 32, 32), Color::GREEN);
  839. }
  840. }
  841. void LayerRenderTypeTransactionTest::setRelativeZBasicHelper(uint32_t layerType) {
  842. sp<SurfaceControl> layerR;
  843. sp<SurfaceControl> layerG;
  844. ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32, layerType));
  845. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
  846. ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32, layerType));
  847. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
  848. switch (layerType) {
  849. case ISurfaceComposerClient::eFXSurfaceBufferQueue:
  850. Transaction()
  851. .setPosition(layerG, 16, 16)
  852. .setRelativeLayer(layerG, layerR->getHandle(), 1)
  853. .apply();
  854. break;
  855. case ISurfaceComposerClient::eFXSurfaceBufferState:
  856. Transaction()
  857. .setFrame(layerR, Rect(0, 0, 32, 32))
  858. .setFrame(layerG, Rect(16, 16, 48, 48))
  859. .setRelativeLayer(layerG, layerR->getHandle(), 1)
  860. .apply();
  861. break;
  862. default:
  863. ASSERT_FALSE(true) << "Unsupported layer type";
  864. }
  865. {
  866. SCOPED_TRACE("layerG above");
  867. auto shot = getScreenCapture();
  868. shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
  869. shot->expectColor(Rect(16, 16, 48, 48), Color::GREEN);
  870. }
  871. Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).apply();
  872. {
  873. SCOPED_TRACE("layerG below");
  874. auto shot = getScreenCapture();
  875. shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
  876. shot->expectColor(Rect(32, 32, 48, 48), Color::GREEN);
  877. }
  878. }
  879. TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferQueue) {
  880. ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
  881. }
  882. TEST_P(LayerRenderTypeTransactionTest, SetRelativeZBasic_BufferState) {
  883. ASSERT_NO_FATAL_FAILURE(setRelativeZBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
  884. }
  885. TEST_P(LayerTypeTransactionTest, SetRelativeZNegative) {
  886. sp<SurfaceControl> parent =
  887. LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
  888. ISurfaceComposerClient::eFXSurfaceContainer);
  889. Transaction().setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight)).apply();
  890. sp<SurfaceControl> layerR;
  891. sp<SurfaceControl> layerG;
  892. sp<SurfaceControl> layerB;
  893. ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
  894. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
  895. ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
  896. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
  897. ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test B", 32, 32));
  898. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerB, Color::BLUE, 32, 32));
  899. Transaction()
  900. .reparent(layerB, parent->getHandle())
  901. .apply();
  902. // layerR = mLayerZBase, layerG = layerR - 1, layerB = -2
  903. Transaction().setRelativeLayer(layerG, layerR->getHandle(), -1).setLayer(layerB, -2).apply();
  904. std::unique_ptr<ScreenCapture> screenshot;
  905. // only layerB is in this range
  906. sp<IBinder> parentHandle = parent->getHandle();
  907. ScreenCapture::captureLayers(&screenshot, parentHandle, Rect(0, 0, 32, 32));
  908. screenshot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
  909. }
  910. TEST_P(LayerTypeTransactionTest, SetLayerAndRelative) {
  911. sp<SurfaceControl> parent =
  912. LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
  913. ISurfaceComposerClient::eFXSurfaceColor);
  914. sp<SurfaceControl> childLayer;
  915. ASSERT_NO_FATAL_FAILURE(
  916. childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
  917. 0 /* buffer height */,
  918. ISurfaceComposerClient::eFXSurfaceColor,
  919. parent.get()));
  920. Transaction()
  921. .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
  922. .setColor(parent, half3{0.0f, 0.0f, 0.0f})
  923. .show(childLayer)
  924. .show(parent)
  925. .setCrop_legacy(parent, Rect(0, 0, mDisplayWidth, mDisplayHeight))
  926. .setCrop_legacy(childLayer, Rect(0, 0, 20, 30))
  927. .apply();
  928. Transaction()
  929. .setRelativeLayer(childLayer, parent->getHandle(), -1)
  930. .setLayer(childLayer, 1)
  931. .apply();
  932. {
  933. SCOPED_TRACE("setLayer above");
  934. // Set layer should get applied and place the child above.
  935. std::unique_ptr<ScreenCapture> screenshot;
  936. ScreenCapture::captureScreen(&screenshot);
  937. screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
  938. }
  939. Transaction()
  940. .setLayer(childLayer, 1)
  941. .setRelativeLayer(childLayer, parent->getHandle(), -1)
  942. .apply();
  943. {
  944. SCOPED_TRACE("setRelative below");
  945. // Set relative layer should get applied and place the child below.
  946. std::unique_ptr<ScreenCapture> screenshot;
  947. ScreenCapture::captureScreen(&screenshot);
  948. screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
  949. }
  950. }
  951. TEST_P(LayerTypeTransactionTest, HideRelativeParentHidesLayer) {
  952. sp<SurfaceControl> parent =
  953. LayerTransactionTest::createLayer("Parent", 0 /* buffer width */, 0 /* buffer height */,
  954. ISurfaceComposerClient::eFXSurfaceColor);
  955. sp<SurfaceControl> relativeParent =
  956. LayerTransactionTest::createLayer("RelativeParent", 0 /* buffer width */,
  957. 0 /* buffer height */, ISurfaceComposerClient::eFXSurfaceColor);
  958. sp<SurfaceControl> childLayer;
  959. ASSERT_NO_FATAL_FAILURE(
  960. childLayer = LayerTransactionTest::createLayer("childLayer", 0 /* buffer width */,
  961. 0 /* buffer height */,
  962. ISurfaceComposerClient::eFXSurfaceColor,
  963. parent.get()));
  964. Transaction()
  965. .setColor(childLayer, half3{1.0f, 0.0f, 0.0f})
  966. .setColor(parent, half3{0.0f, 0.0f, 0.0f})
  967. .setColor(relativeParent, half3{0.0f, 1.0f, 0.0f})
  968. .show(childLayer)
  969. .show(parent)
  970. .show(relativeParent)
  971. .setLayer(parent, mLayerZBase - 1)
  972. .setLayer(relativeParent, mLayerZBase)
  973. .apply();
  974. Transaction()
  975. .setRelativeLayer(childLayer, relativeParent->getHandle(), 1)
  976. .apply();
  977. {
  978. SCOPED_TRACE("setLayer above");
  979. // Set layer should get applied and place the child above.
  980. std::unique_ptr<ScreenCapture> screenshot;
  981. ScreenCapture::captureScreen(&screenshot);
  982. screenshot->expectColor(Rect(0, 0, 20, 30), Color::RED);
  983. }
  984. Transaction()
  985. .hide(relativeParent)
  986. .apply();
  987. {
  988. SCOPED_TRACE("hide relative parent");
  989. // The relative should no longer be visible.
  990. std::unique_ptr<ScreenCapture> screenshot;
  991. ScreenCapture::captureScreen(&screenshot);
  992. screenshot->expectColor(Rect(0, 0, 20, 30), Color::BLACK);
  993. }
  994. }
  995. void LayerRenderTypeTransactionTest::setRelativeZGroupHelper(uint32_t layerType) {
  996. sp<SurfaceControl> layerR;
  997. sp<SurfaceControl> layerG;
  998. sp<SurfaceControl> layerB;
  999. ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test", 32, 32, layerType));
  1000. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerR, Color::RED, 32, 32));
  1001. ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test", 32, 32, layerType));
  1002. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerG, Color::GREEN, 32, 32));
  1003. ASSERT_NO_FATAL_FAILURE(layerB = createLayer("test", 32, 32, layerType));
  1004. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layerB, Color::BLUE, 32, 32));
  1005. // layerR = 0, layerG = layerR + 3, layerB = 2
  1006. switch (layerType) {
  1007. case ISurfaceComposerClient::eFXSurfaceBufferQueue:
  1008. Transaction()
  1009. .setPosition(layerG, 8, 8)
  1010. .setRelativeLayer(layerG, layerR->getHandle(), 3)
  1011. .setPosition(layerB, 16, 16)
  1012. .setLayer(layerB, mLayerZBase + 2)
  1013. .apply();
  1014. break;
  1015. case ISurfaceComposerClient::eFXSurfaceBufferState:
  1016. Transaction()
  1017. .setFrame(layerR, Rect(0, 0, 32, 32))
  1018. .setFrame(layerG, Rect(8, 8, 40, 40))
  1019. .setRelativeLayer(layerG, layerR->getHandle(), 3)
  1020. .setFrame(layerB, Rect(16, 16, 48, 48))
  1021. .setLayer(layerB, mLayerZBase + 2)
  1022. .apply();
  1023. break;
  1024. default:
  1025. ASSERT_FALSE(true) << "Unsupported layer type";
  1026. }
  1027. {
  1028. SCOPED_TRACE("(layerR < layerG) < layerB");
  1029. auto shot = getScreenCapture();
  1030. shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
  1031. shot->expectColor(Rect(8, 8, 16, 16), Color::GREEN);
  1032. shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
  1033. }
  1034. // layerR = 4, layerG = layerR + 3, layerB = 2
  1035. Transaction().setLayer(layerR, mLayerZBase + 4).apply();
  1036. {
  1037. SCOPED_TRACE("layerB < (layerR < layerG)");
  1038. auto shot = getScreenCapture();
  1039. shot->expectColor(Rect(0, 0, 8, 8), Color::RED);
  1040. shot->expectColor(Rect(8, 8, 40, 40), Color::GREEN);
  1041. shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
  1042. }
  1043. // layerR = 4, layerG = layerR - 3, layerB = 2
  1044. Transaction().setRelativeLayer(layerG, layerR->getHandle(), -3).apply();
  1045. {
  1046. SCOPED_TRACE("layerB < (layerG < layerR)");
  1047. auto shot = getScreenCapture();
  1048. shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1049. shot->expectColor(Rect(32, 32, 40, 40), Color::GREEN);
  1050. shot->expectColor(Rect(40, 40, 48, 48), Color::BLUE);
  1051. }
  1052. // restore to absolute z
  1053. // layerR = 4, layerG = 0, layerB = 2
  1054. Transaction().setLayer(layerG, mLayerZBase).apply();
  1055. {
  1056. SCOPED_TRACE("layerG < layerB < layerR");
  1057. auto shot = getScreenCapture();
  1058. shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1059. shot->expectColor(Rect(32, 32, 48, 48), Color::BLUE);
  1060. }
  1061. // layerR should not affect layerG anymore
  1062. // layerR = 1, layerG = 0, layerB = 2
  1063. Transaction().setLayer(layerR, mLayerZBase + 1).apply();
  1064. {
  1065. SCOPED_TRACE("layerG < layerR < layerB");
  1066. auto shot = getScreenCapture();
  1067. shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
  1068. shot->expectColor(Rect(16, 16, 48, 48), Color::BLUE);
  1069. }
  1070. }
  1071. TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferQueue) {
  1072. ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
  1073. }
  1074. TEST_P(LayerRenderTypeTransactionTest, SetRelativeZGroup_BufferState) {
  1075. ASSERT_NO_FATAL_FAILURE(setRelativeZGroupHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
  1076. }
  1077. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetRelativeZBug64572777) {
  1078. sp<SurfaceControl> layerR;
  1079. sp<SurfaceControl> layerG;
  1080. ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
  1081. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, Color::RED, 32, 32));
  1082. ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
  1083. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
  1084. Transaction()
  1085. .setPosition(layerG, 16, 16)
  1086. .setRelativeLayer(layerG, layerR->getHandle(), 1)
  1087. .apply();
  1088. layerG.clear();
  1089. // layerG should have been removed
  1090. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1091. }
  1092. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsHidden) {
  1093. sp<SurfaceControl> layer;
  1094. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1095. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
  1096. Transaction().setFlags(layer, layer_state_t::eLayerHidden, layer_state_t::eLayerHidden).apply();
  1097. {
  1098. SCOPED_TRACE("layer hidden");
  1099. getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
  1100. }
  1101. Transaction().setFlags(layer, 0, layer_state_t::eLayerHidden).apply();
  1102. {
  1103. SCOPED_TRACE("layer shown");
  1104. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1105. }
  1106. }
  1107. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetFlagsOpaque) {
  1108. const Color translucentRed = {100, 0, 0, 100};
  1109. sp<SurfaceControl> layerR;
  1110. sp<SurfaceControl> layerG;
  1111. ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
  1112. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerR, translucentRed, 32, 32));
  1113. ASSERT_NO_FATAL_FAILURE(layerG = createLayer("test G", 32, 32));
  1114. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerG, Color::GREEN, 32, 32));
  1115. Transaction()
  1116. .setLayer(layerR, mLayerZBase + 1)
  1117. .setFlags(layerR, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
  1118. .apply();
  1119. {
  1120. SCOPED_TRACE("layerR opaque");
  1121. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, 0, 0, 255});
  1122. }
  1123. Transaction().setFlags(layerR, 0, layer_state_t::eLayerOpaque).apply();
  1124. {
  1125. SCOPED_TRACE("layerR translucent");
  1126. const uint8_t g = uint8_t(255 - translucentRed.a);
  1127. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {100, g, 0, 255});
  1128. }
  1129. }
  1130. TEST_P(LayerTypeTransactionTest, SetFlagsSecure) {
  1131. sp<SurfaceControl> layer;
  1132. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1133. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
  1134. sp<ISurfaceComposer> composer = ComposerService::getComposerService();
  1135. sp<GraphicBuffer> outBuffer;
  1136. Transaction()
  1137. .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
  1138. .apply(true);
  1139. ASSERT_EQ(PERMISSION_DENIED,
  1140. composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
  1141. Transaction().setFlags(layer, 0, layer_state_t::eLayerSecure).apply(true);
  1142. ASSERT_EQ(NO_ERROR,
  1143. composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
  1144. }
  1145. /** RAII Wrapper around get/seteuid */
  1146. class UIDFaker {
  1147. uid_t oldId;
  1148. public:
  1149. UIDFaker(uid_t uid) {
  1150. oldId = geteuid();
  1151. seteuid(uid);
  1152. }
  1153. ~UIDFaker() {
  1154. seteuid(oldId);
  1155. }
  1156. };
  1157. TEST_F(LayerTransactionTest, SetFlagsSecureEUidSystem) {
  1158. sp<SurfaceControl> layer;
  1159. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1160. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  1161. sp<ISurfaceComposer> composer = ComposerService::getComposerService();
  1162. sp<GraphicBuffer> outBuffer;
  1163. Transaction()
  1164. .setFlags(layer, layer_state_t::eLayerSecure, layer_state_t::eLayerSecure)
  1165. .apply(true);
  1166. ASSERT_EQ(PERMISSION_DENIED,
  1167. composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
  1168. UIDFaker f(AID_SYSTEM);
  1169. // By default the system can capture screenshots with secure layers but they
  1170. // will be blacked out
  1171. ASSERT_EQ(NO_ERROR,
  1172. composer->captureScreen(mDisplay, &outBuffer, Rect(), 0, 0, false));
  1173. {
  1174. SCOPED_TRACE("as system");
  1175. auto shot = screenshot();
  1176. shot->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
  1177. }
  1178. // Here we pass captureSecureLayers = true and since we are AID_SYSTEM we should be able
  1179. // to receive them...we are expected to take care with the results.
  1180. bool outCapturedSecureLayers;
  1181. ASSERT_EQ(NO_ERROR,
  1182. composer->captureScreen(mDisplay, &outBuffer, outCapturedSecureLayers,
  1183. ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, Rect(), 0,
  1184. 0, false, ISurfaceComposer::eRotateNone, true));
  1185. ASSERT_EQ(true, outCapturedSecureLayers);
  1186. ScreenCapture sc(outBuffer);
  1187. sc.expectColor(Rect(0, 0, 32, 32), Color::RED);
  1188. }
  1189. TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferQueue) {
  1190. const Rect top(0, 0, 32, 16);
  1191. const Rect bottom(0, 16, 32, 32);
  1192. sp<SurfaceControl> layer;
  1193. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1194. ANativeWindow_Buffer buffer;
  1195. ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
  1196. ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::TRANSPARENT));
  1197. ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::RED));
  1198. // setTransparentRegionHint always applies to the following buffer
  1199. Transaction().setTransparentRegionHint(layer, Region(top)).apply();
  1200. ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
  1201. {
  1202. SCOPED_TRACE("top transparent");
  1203. auto shot = getScreenCapture();
  1204. shot->expectColor(top, Color::BLACK);
  1205. shot->expectColor(bottom, Color::RED);
  1206. }
  1207. Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
  1208. {
  1209. SCOPED_TRACE("transparent region hint pending");
  1210. auto shot = getScreenCapture();
  1211. shot->expectColor(top, Color::BLACK);
  1212. shot->expectColor(bottom, Color::RED);
  1213. }
  1214. ASSERT_NO_FATAL_FAILURE(buffer = getBufferQueueLayerBuffer(layer));
  1215. ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, top, Color::RED));
  1216. ASSERT_NO_FATAL_FAILURE(fillANativeWindowBufferColor(buffer, bottom, Color::TRANSPARENT));
  1217. ASSERT_NO_FATAL_FAILURE(postBufferQueueLayerBuffer(layer));
  1218. {
  1219. SCOPED_TRACE("bottom transparent");
  1220. auto shot = getScreenCapture();
  1221. shot->expectColor(top, Color::RED);
  1222. shot->expectColor(bottom, Color::BLACK);
  1223. }
  1224. }
  1225. TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintBasic_BufferState) {
  1226. const Rect top(0, 0, 32, 16);
  1227. const Rect bottom(0, 16, 32, 32);
  1228. sp<SurfaceControl> layer;
  1229. ASSERT_NO_FATAL_FAILURE(
  1230. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  1231. sp<GraphicBuffer> buffer =
  1232. new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  1233. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  1234. BufferUsage::COMPOSER_OVERLAY,
  1235. "test");
  1236. ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::TRANSPARENT));
  1237. ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::RED));
  1238. Transaction()
  1239. .setTransparentRegionHint(layer, Region(top))
  1240. .setBuffer(layer, buffer)
  1241. .setFrame(layer, Rect(0, 0, 32, 32))
  1242. .apply();
  1243. {
  1244. SCOPED_TRACE("top transparent");
  1245. auto shot = getScreenCapture();
  1246. shot->expectColor(top, Color::BLACK);
  1247. shot->expectColor(bottom, Color::RED);
  1248. }
  1249. Transaction().setTransparentRegionHint(layer, Region(bottom)).apply();
  1250. {
  1251. SCOPED_TRACE("transparent region hint intermediate");
  1252. auto shot = getScreenCapture();
  1253. shot->expectColor(top, Color::BLACK);
  1254. shot->expectColor(bottom, Color::BLACK);
  1255. }
  1256. buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  1257. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  1258. BufferUsage::COMPOSER_OVERLAY,
  1259. "test");
  1260. ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, top, Color::RED));
  1261. ASSERT_NO_FATAL_FAILURE(fillGraphicBufferColor(buffer, bottom, Color::TRANSPARENT));
  1262. Transaction().setBuffer(layer, buffer).apply();
  1263. {
  1264. SCOPED_TRACE("bottom transparent");
  1265. auto shot = getScreenCapture();
  1266. shot->expectColor(top, Color::RED);
  1267. shot->expectColor(bottom, Color::BLACK);
  1268. }
  1269. }
  1270. TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferQueue) {
  1271. sp<SurfaceControl> layerTransparent;
  1272. sp<SurfaceControl> layerR;
  1273. ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
  1274. ASSERT_NO_FATAL_FAILURE(layerR = createLayer("test R", 32, 32));
  1275. // check that transparent region hint is bound by the layer size
  1276. Transaction()
  1277. .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
  1278. .setPosition(layerR, 16, 16)
  1279. .setLayer(layerR, mLayerZBase + 1)
  1280. .apply();
  1281. ASSERT_NO_FATAL_FAILURE(
  1282. fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
  1283. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layerR, Color::RED, 32, 32));
  1284. getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
  1285. }
  1286. TEST_P(LayerRenderTypeTransactionTest, SetTransparentRegionHintOutOfBounds_BufferState) {
  1287. sp<SurfaceControl> layerTransparent;
  1288. sp<SurfaceControl> layerR;
  1289. ASSERT_NO_FATAL_FAILURE(layerTransparent = createLayer("test transparent", 32, 32));
  1290. ASSERT_NO_FATAL_FAILURE(
  1291. layerR = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  1292. // check that transparent region hint is bound by the layer size
  1293. Transaction()
  1294. .setTransparentRegionHint(layerTransparent, Region(mDisplayRect))
  1295. .setFrame(layerR, Rect(16, 16, 48, 48))
  1296. .setLayer(layerR, mLayerZBase + 1)
  1297. .apply();
  1298. ASSERT_NO_FATAL_FAILURE(
  1299. fillBufferQueueLayerColor(layerTransparent, Color::TRANSPARENT, 32, 32));
  1300. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layerR, Color::RED, 32, 32));
  1301. getScreenCapture()->expectColor(Rect(16, 16, 48, 48), Color::RED);
  1302. }
  1303. void LayerRenderTypeTransactionTest::setAlphaBasicHelper(uint32_t layerType) {
  1304. sp<SurfaceControl> layer1;
  1305. sp<SurfaceControl> layer2;
  1306. ASSERT_NO_FATAL_FAILURE(layer1 = createLayer("test 1", 32, 32, layerType));
  1307. ASSERT_NO_FATAL_FAILURE(layer2 = createLayer("test 2", 32, 32, layerType));
  1308. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer1, {64, 0, 0, 255}, 32, 32));
  1309. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layerType, layer2, {0, 64, 0, 255}, 32, 32));
  1310. switch (layerType) {
  1311. case ISurfaceComposerClient::eFXSurfaceBufferQueue:
  1312. Transaction()
  1313. .setAlpha(layer1, 0.25f)
  1314. .setAlpha(layer2, 0.75f)
  1315. .setPosition(layer2, 16, 0)
  1316. .setLayer(layer2, mLayerZBase + 1)
  1317. .apply();
  1318. break;
  1319. case ISurfaceComposerClient::eFXSurfaceBufferState:
  1320. Transaction()
  1321. .setAlpha(layer1, 0.25f)
  1322. .setAlpha(layer2, 0.75f)
  1323. .setFrame(layer1, Rect(0, 0, 32, 32))
  1324. .setFrame(layer2, Rect(16, 0, 48, 32))
  1325. .setLayer(layer2, mLayerZBase + 1)
  1326. .apply();
  1327. break;
  1328. default:
  1329. ASSERT_FALSE(true) << "Unsupported layer type";
  1330. }
  1331. {
  1332. auto shot = getScreenCapture();
  1333. uint8_t r = 16; // 64 * 0.25f
  1334. uint8_t g = 48; // 64 * 0.75f
  1335. shot->expectColor(Rect(0, 0, 16, 32), {r, 0, 0, 255});
  1336. shot->expectColor(Rect(32, 0, 48, 32), {0, g, 0, 255});
  1337. r /= 4; // r * (1.0f - 0.75f)
  1338. shot->expectColor(Rect(16, 0, 32, 32), {r, g, 0, 255});
  1339. }
  1340. }
  1341. TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferQueue) {
  1342. ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue));
  1343. }
  1344. TEST_P(LayerRenderTypeTransactionTest, SetAlphaBasic_BufferState) {
  1345. ASSERT_NO_FATAL_FAILURE(setAlphaBasicHelper(ISurfaceComposerClient::eFXSurfaceBufferState));
  1346. }
  1347. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetAlphaClamped) {
  1348. const Color color = {64, 0, 0, 255};
  1349. sp<SurfaceControl> layer;
  1350. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1351. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, color, 32, 32));
  1352. Transaction().setAlpha(layer, 2.0f).apply();
  1353. {
  1354. SCOPED_TRACE("clamped to 1.0f");
  1355. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), color);
  1356. }
  1357. Transaction().setAlpha(layer, -1.0f).apply();
  1358. {
  1359. SCOPED_TRACE("clamped to 0.0f");
  1360. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
  1361. }
  1362. }
  1363. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadius) {
  1364. sp<SurfaceControl> layer;
  1365. const uint8_t size = 64;
  1366. const uint8_t testArea = 4;
  1367. const float cornerRadius = 20.0f;
  1368. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", size, size));
  1369. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, size, size));
  1370. Transaction()
  1371. .setCornerRadius(layer, cornerRadius)
  1372. .apply();
  1373. {
  1374. const uint8_t bottom = size - 1;
  1375. const uint8_t right = size - 1;
  1376. auto shot = getScreenCapture();
  1377. // Transparent corners
  1378. shot->expectColor(Rect(0, 0, testArea, testArea), Color::BLACK);
  1379. shot->expectColor(Rect(size - testArea, 0, right, testArea), Color::BLACK);
  1380. shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
  1381. shot->expectColor(Rect(size - testArea, bottom - testArea, right, bottom), Color::BLACK);
  1382. }
  1383. }
  1384. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetCornerRadiusChildCrop) {
  1385. sp<SurfaceControl> parent;
  1386. sp<SurfaceControl> child;
  1387. const uint8_t size = 64;
  1388. const uint8_t testArea = 4;
  1389. const float cornerRadius = 20.0f;
  1390. ASSERT_NO_FATAL_FAILURE(parent = createLayer("parent", size, size));
  1391. ASSERT_NO_FATAL_FAILURE(fillLayerColor(parent, Color::RED, size, size));
  1392. ASSERT_NO_FATAL_FAILURE(child = createLayer("child", size, size / 2));
  1393. ASSERT_NO_FATAL_FAILURE(fillLayerColor(child, Color::GREEN, size, size / 2));
  1394. Transaction()
  1395. .setCornerRadius(parent, cornerRadius)
  1396. .reparent(child, parent->getHandle())
  1397. .setPosition(child, 0, size / 2)
  1398. .apply();
  1399. {
  1400. const uint8_t bottom = size - 1;
  1401. const uint8_t right = size - 1;
  1402. auto shot = getScreenCapture();
  1403. // Top edge of child should not have rounded corners because it's translated in the parent
  1404. shot->expectColor(Rect(0, size / 2, right, static_cast<int>(bottom - cornerRadius)),
  1405. Color::GREEN);
  1406. // But bottom edges should have been clipped according to parent bounds
  1407. shot->expectColor(Rect(0, bottom - testArea, testArea, bottom), Color::BLACK);
  1408. shot->expectColor(Rect(right - testArea, bottom - testArea, right, bottom), Color::BLACK);
  1409. }
  1410. }
  1411. TEST_P(LayerRenderTypeTransactionTest, SetColorBasic) {
  1412. sp<SurfaceControl> bufferLayer;
  1413. sp<SurfaceControl> colorLayer;
  1414. ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
  1415. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
  1416. ASSERT_NO_FATAL_FAILURE(colorLayer =
  1417. createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
  1418. ISurfaceComposerClient::eFXSurfaceColor));
  1419. Transaction()
  1420. .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
  1421. .setLayer(colorLayer, mLayerZBase + 1)
  1422. .apply();
  1423. {
  1424. SCOPED_TRACE("default color");
  1425. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
  1426. }
  1427. const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
  1428. const Color expected = {15, 51, 85, 255};
  1429. // this is handwavy, but the precison loss scaled by 255 (8-bit per
  1430. // channel) should be less than one
  1431. const uint8_t tolerance = 1;
  1432. Transaction().setColor(colorLayer, color).apply();
  1433. {
  1434. SCOPED_TRACE("new color");
  1435. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expected, tolerance);
  1436. }
  1437. }
  1438. // RED: Color layer base color and BufferQueueLayer/BufferStateLayer fill
  1439. // BLUE: prior background color
  1440. // GREEN: final background color
  1441. // BLACK: no color or fill
  1442. void LayerRenderTypeTransactionTest::setBackgroundColorHelper(uint32_t layerType, bool priorColor,
  1443. bool bufferFill, float alpha,
  1444. Color finalColor) {
  1445. sp<SurfaceControl> layer;
  1446. int32_t width = 500;
  1447. int32_t height = 500;
  1448. Color fillColor = Color::RED;
  1449. Color priorBgColor = Color::BLUE;
  1450. Color expectedColor = Color::BLACK;
  1451. switch (layerType) {
  1452. case ISurfaceComposerClient::eFXSurfaceColor:
  1453. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 0, 0, layerType));
  1454. Transaction()
  1455. .setCrop_legacy(layer, Rect(0, 0, width, height))
  1456. .setColor(layer, half3(1.0f, 0, 0))
  1457. .apply();
  1458. expectedColor = fillColor;
  1459. break;
  1460. case ISurfaceComposerClient::eFXSurfaceBufferQueue:
  1461. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
  1462. if (bufferFill) {
  1463. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, fillColor, width, height));
  1464. expectedColor = fillColor;
  1465. }
  1466. Transaction().setCrop_legacy(layer, Rect(0, 0, width, height)).apply();
  1467. break;
  1468. case ISurfaceComposerClient::eFXSurfaceBufferState:
  1469. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height, layerType));
  1470. if (bufferFill) {
  1471. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, fillColor, width, height));
  1472. expectedColor = fillColor;
  1473. }
  1474. Transaction().setFrame(layer, Rect(0, 0, width, height)).apply();
  1475. break;
  1476. default:
  1477. GTEST_FAIL() << "Unknown layer type in setBackgroundColorHelper";
  1478. return;
  1479. }
  1480. if (priorColor && layerType != ISurfaceComposerClient::eFXSurfaceColor) {
  1481. Transaction()
  1482. .setBackgroundColor(layer, half3(0, 0, 1.0f), 1.0f, ui::Dataspace::UNKNOWN)
  1483. .apply();
  1484. if (!bufferFill) {
  1485. expectedColor = priorBgColor;
  1486. }
  1487. }
  1488. {
  1489. SCOPED_TRACE("default before setting background color layer");
  1490. screenshot()->expectColor(Rect(0, 0, width, height), expectedColor);
  1491. }
  1492. Transaction()
  1493. .setBackgroundColor(layer, half3(0, 1.0f, 0), alpha, ui::Dataspace::UNKNOWN)
  1494. .apply();
  1495. {
  1496. auto shot = screenshot();
  1497. shot->expectColor(Rect(0, 0, width, height), finalColor);
  1498. shot->expectBorder(Rect(0, 0, width, height), Color::BLACK);
  1499. }
  1500. }
  1501. TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_Color_NoEffect) {
  1502. bool priorColor = false;
  1503. bool bufferFill = false;
  1504. float alpha = 1.0f;
  1505. Color finalColor = Color::RED;
  1506. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceColor,
  1507. priorColor, bufferFill, alpha, finalColor));
  1508. }
  1509. TEST_P(LayerRenderTypeTransactionTest,
  1510. SetBackgroundColor_BufferQueue_BufferFill_NoPriorColor_Basic) {
  1511. bool priorColor = false;
  1512. bool bufferFill = true;
  1513. float alpha = 1.0f;
  1514. Color finalColor = Color::RED;
  1515. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
  1516. priorColor, bufferFill, alpha, finalColor));
  1517. }
  1518. TEST_P(LayerRenderTypeTransactionTest,
  1519. SetBackgroundColor_BufferQueue_NoBufferFill_NoPriorColor_Basic) {
  1520. bool priorColor = false;
  1521. bool bufferFill = false;
  1522. float alpha = 1.0f;
  1523. Color finalColor = Color::GREEN;
  1524. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
  1525. priorColor, bufferFill, alpha, finalColor));
  1526. }
  1527. TEST_P(LayerRenderTypeTransactionTest, SetBackgroundColor_BufferQueue_BufferFill_PriorColor_Basic) {
  1528. bool priorColor = true;
  1529. bool bufferFill = true;
  1530. float alpha = 1.0f;
  1531. Color finalColor = Color::RED;
  1532. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
  1533. priorColor, bufferFill, alpha, finalColor));
  1534. }
  1535. TEST_P(LayerRenderTypeTransactionTest,
  1536. SetBackgroundColor_BufferQueue_NoBufferFill_PriorColor_Basic) {
  1537. bool priorColor = true;
  1538. bool bufferFill = false;
  1539. float alpha = 1.0f;
  1540. Color finalColor = Color::GREEN;
  1541. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
  1542. priorColor, bufferFill, alpha, finalColor));
  1543. }
  1544. TEST_P(LayerRenderTypeTransactionTest,
  1545. SetBackgroundColor_BufferQueue_NoPriorColor_ZeroAlpha_NoEffect) {
  1546. bool priorColor = false;
  1547. bool bufferFill = false;
  1548. float alpha = 0;
  1549. Color finalColor = Color::BLACK;
  1550. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
  1551. priorColor, bufferFill, alpha, finalColor));
  1552. }
  1553. TEST_P(LayerRenderTypeTransactionTest,
  1554. SetBackgroundColor_BufferQueue_PriorColor_ZeroAlpha_DeleteBackground) {
  1555. bool priorColor = true;
  1556. bool bufferFill = false;
  1557. float alpha = 0;
  1558. Color finalColor = Color::BLACK;
  1559. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferQueue,
  1560. priorColor, bufferFill, alpha, finalColor));
  1561. }
  1562. TEST_P(LayerRenderTypeTransactionTest,
  1563. SetBackgroundColor_BufferState_BufferFill_NoPriorColor_Basic) {
  1564. bool priorColor = false;
  1565. bool bufferFill = true;
  1566. float alpha = 1.0f;
  1567. Color finalColor = Color::RED;
  1568. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
  1569. priorColor, bufferFill, alpha, finalColor));
  1570. }
  1571. TEST_P(LayerRenderTypeTransactionTest,
  1572. SetBackgroundColor_BufferState_NoBufferFill_NoPriorColor_Basic) {
  1573. bool priorColor = false;
  1574. bool bufferFill = false;
  1575. float alpha = 1.0f;
  1576. Color finalColor = Color::GREEN;
  1577. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
  1578. priorColor, bufferFill, alpha, finalColor));
  1579. }
  1580. TEST_P(LayerRenderTypeTransactionTest,
  1581. SetBackgroundColor_BufferState_NoBufferFill_PriorColor_Basic) {
  1582. bool priorColor = true;
  1583. bool bufferFill = false;
  1584. float alpha = 1.0f;
  1585. Color finalColor = Color::GREEN;
  1586. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
  1587. priorColor, bufferFill, alpha, finalColor));
  1588. }
  1589. TEST_P(LayerRenderTypeTransactionTest,
  1590. SetBackgroundColor_BufferState_NoPriorColor_ZeroAlpha_NoEffect) {
  1591. bool priorColor = false;
  1592. bool bufferFill = false;
  1593. float alpha = 0;
  1594. Color finalColor = Color::BLACK;
  1595. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
  1596. priorColor, bufferFill, alpha, finalColor));
  1597. }
  1598. TEST_P(LayerRenderTypeTransactionTest,
  1599. SetBackgroundColor_BufferState_PriorColor_ZeroAlpha_DeleteBackground) {
  1600. bool priorColor = true;
  1601. bool bufferFill = false;
  1602. float alpha = 0;
  1603. Color finalColor = Color::BLACK;
  1604. ASSERT_NO_FATAL_FAILURE(setBackgroundColorHelper(ISurfaceComposerClient::eFXSurfaceBufferState,
  1605. priorColor, bufferFill, alpha, finalColor));
  1606. }
  1607. TEST_P(LayerRenderTypeTransactionTest, SetColorClamped) {
  1608. sp<SurfaceControl> colorLayer;
  1609. ASSERT_NO_FATAL_FAILURE(colorLayer =
  1610. createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
  1611. ISurfaceComposerClient::eFXSurfaceColor));
  1612. Transaction()
  1613. .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
  1614. .setColor(colorLayer, half3(2.0f, -1.0f, 0.0f))
  1615. .apply();
  1616. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1617. }
  1618. TEST_P(LayerRenderTypeTransactionTest, SetColorWithAlpha) {
  1619. sp<SurfaceControl> bufferLayer;
  1620. sp<SurfaceControl> colorLayer;
  1621. ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
  1622. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
  1623. ASSERT_NO_FATAL_FAILURE(colorLayer =
  1624. createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
  1625. ISurfaceComposerClient::eFXSurfaceColor));
  1626. Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
  1627. const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
  1628. const float alpha = 0.25f;
  1629. const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
  1630. // this is handwavy, but the precison loss scaled by 255 (8-bit per
  1631. // channel) should be less than one
  1632. const uint8_t tolerance = 1;
  1633. Transaction()
  1634. .setColor(colorLayer, color)
  1635. .setAlpha(colorLayer, alpha)
  1636. .setLayer(colorLayer, mLayerZBase + 1)
  1637. .apply();
  1638. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
  1639. tolerance);
  1640. }
  1641. TEST_P(LayerRenderTypeTransactionTest, SetColorWithParentAlpha_Bug74220420) {
  1642. sp<SurfaceControl> bufferLayer;
  1643. sp<SurfaceControl> parentLayer;
  1644. sp<SurfaceControl> colorLayer;
  1645. ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test bg", 32, 32));
  1646. ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parentWithAlpha", 32, 32));
  1647. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::RED, 32, 32));
  1648. ASSERT_NO_FATAL_FAILURE(colorLayer = createLayer("childWithColor", 0 /* buffer width */,
  1649. 0 /* buffer height */,
  1650. ISurfaceComposerClient::eFXSurfaceColor));
  1651. Transaction().setCrop_legacy(colorLayer, Rect(0, 0, 32, 32)).apply();
  1652. const half3 color(15.0f / 255.0f, 51.0f / 255.0f, 85.0f / 255.0f);
  1653. const float alpha = 0.25f;
  1654. const ubyte3 expected((vec3(color) * alpha + vec3(1.0f, 0.0f, 0.0f) * (1.0f - alpha)) * 255.0f);
  1655. // this is handwavy, but the precision loss scaled by 255 (8-bit per
  1656. // channel) should be less than one
  1657. const uint8_t tolerance = 1;
  1658. Transaction()
  1659. .reparent(colorLayer, parentLayer->getHandle())
  1660. .setColor(colorLayer, color)
  1661. .setAlpha(parentLayer, alpha)
  1662. .setLayer(parentLayer, mLayerZBase + 1)
  1663. .apply();
  1664. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), {expected.r, expected.g, expected.b, 255},
  1665. tolerance);
  1666. }
  1667. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetColorWithBuffer) {
  1668. sp<SurfaceControl> bufferLayer;
  1669. ASSERT_NO_FATAL_FAILURE(bufferLayer = createLayer("test", 32, 32));
  1670. ASSERT_NO_FATAL_FAILURE(fillLayerColor(bufferLayer, Color::RED, 32, 32));
  1671. // color is ignored
  1672. Transaction().setColor(bufferLayer, half3(0.0f, 1.0f, 0.0f)).apply();
  1673. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1674. }
  1675. TEST_P(LayerTypeAndRenderTypeTransactionTest, SetLayerStackBasic) {
  1676. sp<SurfaceControl> layer;
  1677. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1678. ASSERT_NO_FATAL_FAILURE(fillLayerColor(layer, Color::RED, 32, 32));
  1679. Transaction().setLayerStack(layer, mDisplayLayerStack + 1).apply();
  1680. {
  1681. SCOPED_TRACE("non-existing layer stack");
  1682. getScreenCapture()->expectColor(mDisplayRect, Color::BLACK);
  1683. }
  1684. Transaction().setLayerStack(layer, mDisplayLayerStack).apply();
  1685. {
  1686. SCOPED_TRACE("original layer stack");
  1687. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1688. }
  1689. }
  1690. TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferQueue) {
  1691. sp<SurfaceControl> layer;
  1692. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1693. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
  1694. Color::BLUE, Color::WHITE));
  1695. Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 0, 0).apply();
  1696. {
  1697. SCOPED_TRACE("IDENTITY");
  1698. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
  1699. Color::BLUE, Color::WHITE);
  1700. }
  1701. Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).setPosition(layer, 32, 0).apply();
  1702. {
  1703. SCOPED_TRACE("FLIP_H");
  1704. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED,
  1705. Color::WHITE, Color::BLUE);
  1706. }
  1707. Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).setPosition(layer, 0, 32).apply();
  1708. {
  1709. SCOPED_TRACE("FLIP_V");
  1710. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE,
  1711. Color::RED, Color::GREEN);
  1712. }
  1713. Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).setPosition(layer, 32, 0).apply();
  1714. {
  1715. SCOPED_TRACE("ROT_90");
  1716. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED,
  1717. Color::WHITE, Color::GREEN);
  1718. }
  1719. Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setPosition(layer, 0, 0).apply();
  1720. {
  1721. SCOPED_TRACE("SCALE");
  1722. getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 64), Color::RED, Color::GREEN,
  1723. Color::BLUE, Color::WHITE, true /* filtered */);
  1724. }
  1725. }
  1726. TEST_P(LayerRenderTypeTransactionTest, SetMatrixBasic_BufferState) {
  1727. sp<SurfaceControl> layer;
  1728. ASSERT_NO_FATAL_FAILURE(
  1729. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  1730. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
  1731. Color::BLUE, Color::WHITE));
  1732. Transaction()
  1733. .setMatrix(layer, 1.0f, 0.0f, 0.0f, 1.0f)
  1734. .setFrame(layer, Rect(0, 0, 32, 32))
  1735. .apply();
  1736. {
  1737. SCOPED_TRACE("IDENTITY");
  1738. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
  1739. Color::BLUE, Color::WHITE);
  1740. }
  1741. Transaction().setMatrix(layer, -1.0f, 0.0f, 0.0f, 1.0f).apply();
  1742. {
  1743. SCOPED_TRACE("FLIP_H");
  1744. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
  1745. Color::BLUE, Color::WHITE);
  1746. }
  1747. Transaction().setMatrix(layer, 1.0f, 0.0f, 0.0f, -1.0f).apply();
  1748. {
  1749. SCOPED_TRACE("FLIP_V");
  1750. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
  1751. Color::BLUE, Color::WHITE);
  1752. }
  1753. Transaction().setMatrix(layer, 0.0f, 1.0f, -1.0f, 0.0f).apply();
  1754. {
  1755. SCOPED_TRACE("ROT_90");
  1756. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
  1757. Color::BLUE, Color::WHITE);
  1758. }
  1759. Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).apply();
  1760. {
  1761. SCOPED_TRACE("SCALE");
  1762. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::RED, Color::GREEN,
  1763. Color::BLUE, Color::WHITE);
  1764. }
  1765. }
  1766. TEST_P(LayerRenderTypeTransactionTest, SetMatrixRot45_BufferQueue) {
  1767. sp<SurfaceControl> layer;
  1768. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1769. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
  1770. Color::BLUE, Color::WHITE));
  1771. const float rot = M_SQRT1_2; // 45 degrees
  1772. const float trans = M_SQRT2 * 16.0f;
  1773. Transaction().setMatrix(layer, rot, rot, -rot, rot).setPosition(layer, trans, 0).apply();
  1774. auto shot = getScreenCapture();
  1775. // check a 8x8 region inside each color
  1776. auto get8x8Rect = [](int32_t centerX, int32_t centerY) {
  1777. const int32_t halfL = 4;
  1778. return Rect(centerX - halfL, centerY - halfL, centerX + halfL, centerY + halfL);
  1779. };
  1780. const int32_t unit = int32_t(trans / 2);
  1781. shot->expectColor(get8x8Rect(2 * unit, 1 * unit), Color::RED);
  1782. shot->expectColor(get8x8Rect(3 * unit, 2 * unit), Color::GREEN);
  1783. shot->expectColor(get8x8Rect(1 * unit, 2 * unit), Color::BLUE);
  1784. shot->expectColor(get8x8Rect(2 * unit, 3 * unit), Color::WHITE);
  1785. }
  1786. TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithResize_BufferQueue) {
  1787. sp<SurfaceControl> layer;
  1788. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1789. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  1790. // setMatrix is applied after any pending resize, unlike setPosition
  1791. Transaction().setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f).setSize(layer, 64, 64).apply();
  1792. {
  1793. SCOPED_TRACE("resize pending");
  1794. auto shot = getScreenCapture();
  1795. const Rect rect(0, 0, 32, 32);
  1796. shot->expectColor(rect, Color::RED);
  1797. shot->expectBorder(rect, Color::BLACK);
  1798. }
  1799. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 64, 64));
  1800. {
  1801. SCOPED_TRACE("resize applied");
  1802. const Rect rect(0, 0, 128, 128);
  1803. getScreenCapture()->expectColor(rect, Color::RED);
  1804. }
  1805. }
  1806. TEST_P(LayerRenderTypeTransactionTest, SetMatrixWithScaleToWindow_BufferQueue) {
  1807. sp<SurfaceControl> layer;
  1808. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1809. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  1810. // setMatrix is immediate with SCALE_TO_WINDOW, unlike setPosition
  1811. Transaction()
  1812. .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
  1813. .setSize(layer, 64, 64)
  1814. .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
  1815. .apply();
  1816. getScreenCapture()->expectColor(Rect(0, 0, 128, 128), Color::RED);
  1817. }
  1818. TEST_P(LayerRenderTypeTransactionTest, SetOverrideScalingModeBasic_BufferQueue) {
  1819. sp<SurfaceControl> layer;
  1820. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1821. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
  1822. Color::BLUE, Color::WHITE));
  1823. // XXX SCALE_CROP is not respected; calling setSize and
  1824. // setOverrideScalingMode in separate transactions does not work
  1825. // (b/69315456)
  1826. Transaction()
  1827. .setSize(layer, 64, 16)
  1828. .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
  1829. .apply();
  1830. {
  1831. SCOPED_TRACE("SCALE_TO_WINDOW");
  1832. getScreenCapture()->expectQuadrant(Rect(0, 0, 64, 16), Color::RED, Color::GREEN,
  1833. Color::BLUE, Color::WHITE, true /* filtered */);
  1834. }
  1835. }
  1836. TEST_P(LayerTypeTransactionTest, RefreshRateIsInitialized) {
  1837. sp<SurfaceControl> layer;
  1838. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1839. sp<IBinder> handle = layer->getHandle();
  1840. ASSERT_TRUE(handle != nullptr);
  1841. FrameStats frameStats;
  1842. mClient->getLayerFrameStats(handle, &frameStats);
  1843. ASSERT_GT(frameStats.refreshPeriodNano, static_cast<nsecs_t>(0));
  1844. }
  1845. TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferQueue) {
  1846. sp<SurfaceControl> layer;
  1847. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1848. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  1849. const Rect crop(8, 8, 24, 24);
  1850. Transaction().setCrop_legacy(layer, crop).apply();
  1851. auto shot = getScreenCapture();
  1852. shot->expectColor(crop, Color::RED);
  1853. shot->expectBorder(crop, Color::BLACK);
  1854. }
  1855. TEST_P(LayerRenderTypeTransactionTest, SetCropBasic_BufferState) {
  1856. sp<SurfaceControl> layer;
  1857. ASSERT_NO_FATAL_FAILURE(
  1858. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  1859. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
  1860. const Rect crop(8, 8, 24, 24);
  1861. Transaction().setCrop(layer, crop).apply();
  1862. auto shot = getScreenCapture();
  1863. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  1864. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  1865. }
  1866. TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferQueue) {
  1867. sp<SurfaceControl> layer;
  1868. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1869. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  1870. {
  1871. SCOPED_TRACE("empty rect");
  1872. Transaction().setCrop_legacy(layer, Rect(8, 8, 8, 8)).apply();
  1873. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1874. }
  1875. {
  1876. SCOPED_TRACE("negative rect");
  1877. Transaction().setCrop_legacy(layer, Rect(8, 8, 0, 0)).apply();
  1878. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1879. }
  1880. }
  1881. TEST_P(LayerRenderTypeTransactionTest, SetCropEmpty_BufferState) {
  1882. sp<SurfaceControl> layer;
  1883. ASSERT_NO_FATAL_FAILURE(
  1884. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  1885. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
  1886. {
  1887. SCOPED_TRACE("empty rect");
  1888. Transaction().setCrop(layer, Rect(8, 8, 8, 8)).apply();
  1889. getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  1890. }
  1891. {
  1892. SCOPED_TRACE("negative rect");
  1893. Transaction().setCrop(layer, Rect(8, 8, 0, 0)).apply();
  1894. getScreenCapture()->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  1895. }
  1896. }
  1897. TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferQueue) {
  1898. sp<SurfaceControl> layer;
  1899. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1900. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  1901. Transaction().setCrop_legacy(layer, Rect(-128, -64, 128, 64)).apply();
  1902. auto shot = getScreenCapture();
  1903. shot->expectColor(Rect(0, 0, 32, 32), Color::RED);
  1904. shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
  1905. }
  1906. TEST_P(LayerRenderTypeTransactionTest, SetCropOutOfBounds_BufferState) {
  1907. sp<SurfaceControl> layer;
  1908. ASSERT_NO_FATAL_FAILURE(
  1909. layer = createLayer("test", 32, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
  1910. sp<GraphicBuffer> buffer =
  1911. new GraphicBuffer(32, 64, PIXEL_FORMAT_RGBA_8888, 1,
  1912. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  1913. BufferUsage::COMPOSER_OVERLAY,
  1914. "test");
  1915. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 16), Color::BLUE);
  1916. fillGraphicBufferColor(buffer, Rect(0, 16, 32, 64), Color::RED);
  1917. Transaction().setFrame(layer, Rect(0, 0, 64, 64)).apply();
  1918. Transaction().setBuffer(layer, buffer).apply();
  1919. // Partially out of bounds in the negative (upper left) direction
  1920. Transaction().setCrop(layer, Rect(-128, -128, 32, 16)).apply();
  1921. {
  1922. SCOPED_TRACE("out of bounds, negative (upper left) direction");
  1923. auto shot = getScreenCapture();
  1924. shot->expectColor(Rect(0, 0, 64, 64), Color::BLUE);
  1925. shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
  1926. }
  1927. // Partially out of bounds in the positive (lower right) direction
  1928. Transaction().setCrop(layer, Rect(0, 16, 128, 128)).apply();
  1929. {
  1930. SCOPED_TRACE("out of bounds, positive (lower right) direction");
  1931. auto shot = getScreenCapture();
  1932. shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
  1933. shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
  1934. }
  1935. // Fully out of buffer space bounds
  1936. Transaction().setCrop(layer, Rect(-128, -128, -1, -1)).apply();
  1937. {
  1938. SCOPED_TRACE("Fully out of bounds");
  1939. auto shot = getScreenCapture();
  1940. shot->expectColor(Rect(0, 0, 64, 16), Color::BLUE);
  1941. shot->expectColor(Rect(0, 16, 64, 64), Color::RED);
  1942. shot->expectBorder(Rect(0, 0, 64, 64), Color::BLACK);
  1943. }
  1944. }
  1945. TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferQueue) {
  1946. sp<SurfaceControl> layer;
  1947. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1948. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  1949. const Point position(32, 32);
  1950. const Rect crop(8, 8, 24, 24);
  1951. Transaction().setPosition(layer, position.x, position.y).setCrop_legacy(layer, crop).apply();
  1952. auto shot = getScreenCapture();
  1953. shot->expectColor(crop + position, Color::RED);
  1954. shot->expectBorder(crop + position, Color::BLACK);
  1955. }
  1956. TEST_P(LayerRenderTypeTransactionTest, SetCropWithTranslation_BufferState) {
  1957. sp<SurfaceControl> layer;
  1958. ASSERT_NO_FATAL_FAILURE(
  1959. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  1960. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
  1961. const Rect frame(32, 32, 64, 64);
  1962. const Rect crop(8, 8, 24, 24);
  1963. Transaction().setFrame(layer, frame).setCrop(layer, crop).apply();
  1964. auto shot = getScreenCapture();
  1965. shot->expectColor(frame, Color::RED);
  1966. shot->expectBorder(frame, Color::BLACK);
  1967. }
  1968. TEST_P(LayerRenderTypeTransactionTest, SetCropWithScale_BufferQueue) {
  1969. sp<SurfaceControl> layer;
  1970. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1971. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  1972. // crop_legacy is affected by matrix
  1973. Transaction()
  1974. .setMatrix(layer, 2.0f, 0.0f, 0.0f, 2.0f)
  1975. .setCrop_legacy(layer, Rect(8, 8, 24, 24))
  1976. .apply();
  1977. auto shot = getScreenCapture();
  1978. shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
  1979. shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
  1980. }
  1981. TEST_P(LayerRenderTypeTransactionTest, SetCropWithResize_BufferQueue) {
  1982. sp<SurfaceControl> layer;
  1983. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  1984. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  1985. // setCrop_legacy is applied immediately by default, with or without resize pending
  1986. Transaction().setCrop_legacy(layer, Rect(8, 8, 24, 24)).setSize(layer, 16, 16).apply();
  1987. {
  1988. SCOPED_TRACE("resize pending");
  1989. auto shot = getScreenCapture();
  1990. shot->expectColor(Rect(8, 8, 24, 24), Color::RED);
  1991. shot->expectBorder(Rect(8, 8, 24, 24), Color::BLACK);
  1992. }
  1993. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
  1994. {
  1995. SCOPED_TRACE("resize applied");
  1996. auto shot = getScreenCapture();
  1997. shot->expectColor(Rect(8, 8, 16, 16), Color::RED);
  1998. shot->expectBorder(Rect(8, 8, 16, 16), Color::BLACK);
  1999. }
  2000. }
  2001. TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResize_BufferQueue) {
  2002. sp<SurfaceControl> layer;
  2003. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  2004. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  2005. // request setCrop_legacy to be applied with the next resize
  2006. Transaction()
  2007. .setCrop_legacy(layer, Rect(8, 8, 24, 24))
  2008. .setGeometryAppliesWithResize(layer)
  2009. .apply();
  2010. {
  2011. SCOPED_TRACE("waiting for next resize");
  2012. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  2013. }
  2014. Transaction().setCrop_legacy(layer, Rect(4, 4, 12, 12)).apply();
  2015. {
  2016. SCOPED_TRACE("pending crop modified");
  2017. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  2018. }
  2019. Transaction().setSize(layer, 16, 16).apply();
  2020. {
  2021. SCOPED_TRACE("resize pending");
  2022. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::RED);
  2023. }
  2024. // finally resize
  2025. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
  2026. {
  2027. SCOPED_TRACE("new crop applied");
  2028. auto shot = getScreenCapture();
  2029. shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
  2030. shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
  2031. }
  2032. }
  2033. TEST_P(LayerRenderTypeTransactionTest, SetCropWithNextResizeScaleToWindow_BufferQueue) {
  2034. sp<SurfaceControl> layer;
  2035. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  2036. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  2037. // setCrop_legacy is not immediate even with SCALE_TO_WINDOW override
  2038. Transaction()
  2039. .setCrop_legacy(layer, Rect(4, 4, 12, 12))
  2040. .setSize(layer, 16, 16)
  2041. .setOverrideScalingMode(layer, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)
  2042. .setGeometryAppliesWithResize(layer)
  2043. .apply();
  2044. {
  2045. SCOPED_TRACE("new crop pending");
  2046. auto shot = getScreenCapture();
  2047. shot->expectColor(Rect(0, 0, 16, 16), Color::RED);
  2048. shot->expectBorder(Rect(0, 0, 16, 16), Color::BLACK);
  2049. }
  2050. // XXX crop is never latched without other geometry change (b/69315677)
  2051. Transaction().setPosition(layer, 1, 0).setGeometryAppliesWithResize(layer).apply();
  2052. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 16, 16));
  2053. Transaction().setPosition(layer, 0, 0).apply();
  2054. {
  2055. SCOPED_TRACE("new crop applied");
  2056. auto shot = getScreenCapture();
  2057. shot->expectColor(Rect(4, 4, 12, 12), Color::RED);
  2058. shot->expectBorder(Rect(4, 4, 12, 12), Color::BLACK);
  2059. }
  2060. }
  2061. TEST_P(LayerRenderTypeTransactionTest, SetFrameBasic_BufferState) {
  2062. sp<SurfaceControl> layer;
  2063. ASSERT_NO_FATAL_FAILURE(
  2064. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2065. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
  2066. const Rect frame(8, 8, 24, 24);
  2067. Transaction().setFrame(layer, frame).apply();
  2068. auto shot = getScreenCapture();
  2069. shot->expectColor(frame, Color::RED);
  2070. shot->expectBorder(frame, Color::BLACK);
  2071. }
  2072. TEST_P(LayerRenderTypeTransactionTest, SetFrameEmpty_BufferState) {
  2073. sp<SurfaceControl> layer;
  2074. ASSERT_NO_FATAL_FAILURE(
  2075. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2076. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
  2077. {
  2078. SCOPED_TRACE("empty rect");
  2079. Transaction().setFrame(layer, Rect(8, 8, 8, 8)).apply();
  2080. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
  2081. }
  2082. {
  2083. SCOPED_TRACE("negative rect");
  2084. Transaction().setFrame(layer, Rect(8, 8, 0, 0)).apply();
  2085. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
  2086. }
  2087. }
  2088. TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultParentless_BufferState) {
  2089. sp<SurfaceControl> layer;
  2090. ASSERT_NO_FATAL_FAILURE(
  2091. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2092. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 10, 10));
  2093. // A parentless layer will default to a frame with the same size as the buffer
  2094. auto shot = getScreenCapture();
  2095. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2096. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2097. }
  2098. TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBSParent_BufferState) {
  2099. sp<SurfaceControl> parent, child;
  2100. ASSERT_NO_FATAL_FAILURE(
  2101. parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2102. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
  2103. Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
  2104. ASSERT_NO_FATAL_FAILURE(
  2105. child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2106. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
  2107. Transaction().reparent(child, parent->getHandle()).apply();
  2108. // A layer will default to the frame of its parent
  2109. auto shot = getScreenCapture();
  2110. shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
  2111. shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
  2112. }
  2113. TEST_P(LayerRenderTypeTransactionTest, SetFrameDefaultBQParent_BufferState) {
  2114. sp<SurfaceControl> parent, child;
  2115. ASSERT_NO_FATAL_FAILURE(parent = createLayer("test", 32, 32));
  2116. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(parent, Color::RED, 32, 32));
  2117. ASSERT_NO_FATAL_FAILURE(
  2118. child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2119. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
  2120. Transaction().reparent(child, parent->getHandle()).apply();
  2121. // A layer will default to the frame of its parent
  2122. auto shot = getScreenCapture();
  2123. shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
  2124. shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
  2125. }
  2126. TEST_P(LayerRenderTypeTransactionTest, SetFrameUpdate_BufferState) {
  2127. sp<SurfaceControl> layer;
  2128. ASSERT_NO_FATAL_FAILURE(
  2129. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2130. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
  2131. Transaction().setFrame(layer, Rect(0, 0, 32, 32)).apply();
  2132. std::this_thread::sleep_for(500ms);
  2133. Transaction().setFrame(layer, Rect(16, 16, 48, 48)).apply();
  2134. auto shot = getScreenCapture();
  2135. shot->expectColor(Rect(16, 16, 48, 48), Color::RED);
  2136. shot->expectBorder(Rect(16, 16, 48, 48), Color::BLACK);
  2137. }
  2138. TEST_P(LayerRenderTypeTransactionTest, SetFrameOutsideBounds_BufferState) {
  2139. sp<SurfaceControl> parent, child;
  2140. ASSERT_NO_FATAL_FAILURE(
  2141. parent = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2142. ASSERT_NO_FATAL_FAILURE(
  2143. child = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2144. Transaction().reparent(child, parent->getHandle()).apply();
  2145. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(parent, Color::RED, 32, 32));
  2146. Transaction().setFrame(parent, Rect(0, 0, 32, 32)).apply();
  2147. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(child, Color::BLUE, 10, 10));
  2148. Transaction().setFrame(child, Rect(0, 16, 32, 32)).apply();
  2149. auto shot = getScreenCapture();
  2150. shot->expectColor(Rect(0, 0, 32, 16), Color::RED);
  2151. shot->expectColor(Rect(0, 16, 32, 32), Color::BLUE);
  2152. shot->expectBorder(Rect(0, 0, 32, 32), Color::BLACK);
  2153. }
  2154. TEST_P(LayerRenderTypeTransactionTest, SetBufferBasic_BufferState) {
  2155. sp<SurfaceControl> layer;
  2156. ASSERT_NO_FATAL_FAILURE(
  2157. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2158. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
  2159. auto shot = getScreenCapture();
  2160. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2161. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2162. }
  2163. TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleBuffers_BufferState) {
  2164. sp<SurfaceControl> layer;
  2165. ASSERT_NO_FATAL_FAILURE(
  2166. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2167. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
  2168. {
  2169. SCOPED_TRACE("set buffer 1");
  2170. auto shot = getScreenCapture();
  2171. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2172. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2173. }
  2174. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::BLUE, 32, 32));
  2175. {
  2176. SCOPED_TRACE("set buffer 2");
  2177. auto shot = getScreenCapture();
  2178. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLUE);
  2179. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2180. }
  2181. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::RED, 32, 32));
  2182. {
  2183. SCOPED_TRACE("set buffer 3");
  2184. auto shot = getScreenCapture();
  2185. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2186. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2187. }
  2188. }
  2189. TEST_P(LayerRenderTypeTransactionTest, SetBufferMultipleLayers_BufferState) {
  2190. sp<SurfaceControl> layer1;
  2191. ASSERT_NO_FATAL_FAILURE(
  2192. layer1 = createLayer("test", 64, 64, ISurfaceComposerClient::eFXSurfaceBufferState));
  2193. sp<SurfaceControl> layer2;
  2194. ASSERT_NO_FATAL_FAILURE(
  2195. layer2 = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2196. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::RED, 64, 64));
  2197. Transaction().setFrame(layer1, Rect(0, 0, 64, 64)).apply();
  2198. {
  2199. SCOPED_TRACE("set layer 1 buffer red");
  2200. auto shot = getScreenCapture();
  2201. shot->expectColor(Rect(0, 0, 64, 64), Color::RED);
  2202. }
  2203. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::BLUE, 32, 32));
  2204. Transaction().setFrame(layer2, Rect(0, 0, 32, 32)).apply();
  2205. {
  2206. SCOPED_TRACE("set layer 2 buffer blue");
  2207. auto shot = getScreenCapture();
  2208. shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
  2209. shot->expectColor(Rect(0, 32, 64, 64), Color::RED);
  2210. shot->expectColor(Rect(0, 32, 32, 64), Color::RED);
  2211. }
  2212. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer1, Color::GREEN, 64, 64));
  2213. {
  2214. SCOPED_TRACE("set layer 1 buffer green");
  2215. auto shot = getScreenCapture();
  2216. shot->expectColor(Rect(0, 0, 32, 32), Color::BLUE);
  2217. shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
  2218. shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
  2219. }
  2220. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer2, Color::WHITE, 32, 32));
  2221. {
  2222. SCOPED_TRACE("set layer 2 buffer white");
  2223. auto shot = getScreenCapture();
  2224. shot->expectColor(Rect(0, 0, 32, 32), Color::WHITE);
  2225. shot->expectColor(Rect(0, 32, 64, 64), Color::GREEN);
  2226. shot->expectColor(Rect(0, 32, 32, 64), Color::GREEN);
  2227. }
  2228. }
  2229. TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_BufferState) {
  2230. sp<SurfaceControl> layer;
  2231. ASSERT_NO_FATAL_FAILURE(
  2232. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2233. std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
  2234. std::array<sp<GraphicBuffer>, 10> buffers;
  2235. size_t idx = 0;
  2236. for (auto& buffer : buffers) {
  2237. buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  2238. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  2239. BufferUsage::COMPOSER_OVERLAY,
  2240. "test");
  2241. Color color = colors[idx % colors.size()];
  2242. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
  2243. idx++;
  2244. }
  2245. // Set each buffer twice. The first time adds it to the cache, the second time tests that the
  2246. // cache is working.
  2247. idx = 0;
  2248. for (auto& buffer : buffers) {
  2249. for (int i = 0; i < 2; i++) {
  2250. Transaction().setBuffer(layer, buffer).apply();
  2251. Color color = colors[idx % colors.size()];
  2252. auto shot = screenshot();
  2253. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
  2254. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2255. }
  2256. idx++;
  2257. }
  2258. }
  2259. TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_LeastRecentlyUsed_BufferState) {
  2260. sp<SurfaceControl> layer;
  2261. ASSERT_NO_FATAL_FAILURE(
  2262. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2263. std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
  2264. std::array<sp<GraphicBuffer>, 70> buffers;
  2265. size_t idx = 0;
  2266. for (auto& buffer : buffers) {
  2267. buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  2268. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  2269. BufferUsage::COMPOSER_OVERLAY,
  2270. "test");
  2271. Color color = colors[idx % colors.size()];
  2272. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
  2273. idx++;
  2274. }
  2275. // Set each buffer twice. The first time adds it to the cache, the second time tests that the
  2276. // cache is working.
  2277. idx = 0;
  2278. for (auto& buffer : buffers) {
  2279. for (int i = 0; i < 2; i++) {
  2280. Transaction().setBuffer(layer, buffer).apply();
  2281. Color color = colors[idx % colors.size()];
  2282. auto shot = screenshot();
  2283. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
  2284. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2285. }
  2286. idx++;
  2287. }
  2288. }
  2289. TEST_P(LayerRenderTypeTransactionTest, SetBufferCaching_DestroyedBuffer_BufferState) {
  2290. sp<SurfaceControl> layer;
  2291. ASSERT_NO_FATAL_FAILURE(
  2292. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2293. std::array<Color, 4> colors = {Color::RED, Color::BLUE, Color::WHITE, Color::GREEN};
  2294. std::array<sp<GraphicBuffer>, 65> buffers;
  2295. size_t idx = 0;
  2296. for (auto& buffer : buffers) {
  2297. buffer = new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  2298. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  2299. BufferUsage::COMPOSER_OVERLAY,
  2300. "test");
  2301. Color color = colors[idx % colors.size()];
  2302. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), color);
  2303. idx++;
  2304. }
  2305. // Set each buffer twice. The first time adds it to the cache, the second time tests that the
  2306. // cache is working.
  2307. idx = 0;
  2308. for (auto& buffer : buffers) {
  2309. for (int i = 0; i < 2; i++) {
  2310. Transaction().setBuffer(layer, buffer).apply();
  2311. Color color = colors[idx % colors.size()];
  2312. auto shot = screenshot();
  2313. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), color);
  2314. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2315. }
  2316. if (idx == 0) {
  2317. buffers[0].clear();
  2318. }
  2319. idx++;
  2320. }
  2321. }
  2322. TEST_P(LayerRenderTypeTransactionTest, SetTransformRotate90_BufferState) {
  2323. sp<SurfaceControl> layer;
  2324. ASSERT_NO_FATAL_FAILURE(
  2325. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2326. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
  2327. Color::BLUE, Color::WHITE));
  2328. Transaction()
  2329. .setFrame(layer, Rect(0, 0, 32, 32))
  2330. .setTransform(layer, NATIVE_WINDOW_TRANSFORM_ROT_90)
  2331. .apply();
  2332. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::RED, Color::WHITE,
  2333. Color::GREEN, true /* filtered */);
  2334. }
  2335. TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipH_BufferState) {
  2336. sp<SurfaceControl> layer;
  2337. ASSERT_NO_FATAL_FAILURE(
  2338. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2339. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
  2340. Color::BLUE, Color::WHITE));
  2341. Transaction()
  2342. .setFrame(layer, Rect(0, 0, 32, 32))
  2343. .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_H)
  2344. .apply();
  2345. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::GREEN, Color::RED, Color::WHITE,
  2346. Color::BLUE, true /* filtered */);
  2347. }
  2348. TEST_P(LayerRenderTypeTransactionTest, SetTransformFlipV_BufferState) {
  2349. sp<SurfaceControl> layer;
  2350. ASSERT_NO_FATAL_FAILURE(
  2351. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2352. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerQuadrant(layer, 32, 32, Color::RED, Color::GREEN,
  2353. Color::BLUE, Color::WHITE));
  2354. Transaction()
  2355. .setFrame(layer, Rect(0, 0, 32, 32))
  2356. .setTransform(layer, NATIVE_WINDOW_TRANSFORM_FLIP_V)
  2357. .apply();
  2358. getScreenCapture()->expectQuadrant(Rect(0, 0, 32, 32), Color::BLUE, Color::WHITE, Color::RED,
  2359. Color::GREEN, true /* filtered */);
  2360. }
  2361. TEST_F(LayerTransactionTest, SetTransformToDisplayInverse_BufferState) {
  2362. sp<SurfaceControl> layer;
  2363. ASSERT_NO_FATAL_FAILURE(
  2364. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2365. Transaction().setTransformToDisplayInverse(layer, false).apply();
  2366. ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(layer, Color::GREEN, 32, 32));
  2367. Transaction().setTransformToDisplayInverse(layer, true).apply();
  2368. }
  2369. TEST_P(LayerRenderTypeTransactionTest, SetFenceBasic_BufferState) {
  2370. sp<SurfaceControl> layer;
  2371. Transaction transaction;
  2372. ASSERT_NO_FATAL_FAILURE(
  2373. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2374. sp<GraphicBuffer> buffer =
  2375. new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  2376. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  2377. BufferUsage::COMPOSER_OVERLAY,
  2378. "test");
  2379. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
  2380. sp<Fence> fence;
  2381. if (getBuffer(nullptr, &fence) != NO_ERROR) {
  2382. GTEST_SUCCEED() << "test not supported";
  2383. return;
  2384. }
  2385. Transaction().setBuffer(layer, buffer).setAcquireFence(layer, fence).apply();
  2386. status_t status = fence->wait(1000);
  2387. ASSERT_NE(static_cast<status_t>(Fence::Status::Unsignaled), status);
  2388. std::this_thread::sleep_for(200ms);
  2389. auto shot = getScreenCapture();
  2390. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2391. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2392. }
  2393. TEST_P(LayerRenderTypeTransactionTest, SetFenceNull_BufferState) {
  2394. sp<SurfaceControl> layer;
  2395. ASSERT_NO_FATAL_FAILURE(
  2396. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2397. sp<GraphicBuffer> buffer =
  2398. new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  2399. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  2400. BufferUsage::COMPOSER_OVERLAY,
  2401. "test");
  2402. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
  2403. sp<Fence> fence = Fence::NO_FENCE;
  2404. Transaction()
  2405. .setBuffer(layer, buffer)
  2406. .setAcquireFence(layer, fence)
  2407. .apply();
  2408. auto shot = getScreenCapture();
  2409. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2410. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2411. }
  2412. TEST_P(LayerRenderTypeTransactionTest, SetDataspaceBasic_BufferState) {
  2413. sp<SurfaceControl> layer;
  2414. ASSERT_NO_FATAL_FAILURE(
  2415. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2416. sp<GraphicBuffer> buffer =
  2417. new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  2418. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  2419. BufferUsage::COMPOSER_OVERLAY,
  2420. "test");
  2421. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
  2422. Transaction()
  2423. .setBuffer(layer, buffer)
  2424. .setDataspace(layer, ui::Dataspace::UNKNOWN)
  2425. .apply();
  2426. auto shot = getScreenCapture();
  2427. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2428. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2429. }
  2430. TEST_P(LayerRenderTypeTransactionTest, SetHdrMetadataBasic_BufferState) {
  2431. sp<SurfaceControl> layer;
  2432. ASSERT_NO_FATAL_FAILURE(
  2433. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2434. sp<GraphicBuffer> buffer =
  2435. new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  2436. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  2437. BufferUsage::COMPOSER_OVERLAY,
  2438. "test");
  2439. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
  2440. HdrMetadata hdrMetadata;
  2441. hdrMetadata.validTypes = 0;
  2442. Transaction()
  2443. .setBuffer(layer, buffer)
  2444. .setHdrMetadata(layer, hdrMetadata)
  2445. .apply();
  2446. auto shot = getScreenCapture();
  2447. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2448. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2449. }
  2450. TEST_P(LayerRenderTypeTransactionTest, SetSurfaceDamageRegionBasic_BufferState) {
  2451. sp<SurfaceControl> layer;
  2452. ASSERT_NO_FATAL_FAILURE(
  2453. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2454. sp<GraphicBuffer> buffer =
  2455. new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  2456. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  2457. BufferUsage::COMPOSER_OVERLAY,
  2458. "test");
  2459. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
  2460. Region region;
  2461. region.set(32, 32);
  2462. Transaction()
  2463. .setBuffer(layer, buffer)
  2464. .setSurfaceDamageRegion(layer, region)
  2465. .apply();
  2466. auto shot = getScreenCapture();
  2467. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2468. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2469. }
  2470. TEST_P(LayerRenderTypeTransactionTest, SetApiBasic_BufferState) {
  2471. sp<SurfaceControl> layer;
  2472. ASSERT_NO_FATAL_FAILURE(
  2473. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2474. sp<GraphicBuffer> buffer =
  2475. new GraphicBuffer(32, 32, PIXEL_FORMAT_RGBA_8888, 1,
  2476. BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
  2477. BufferUsage::COMPOSER_OVERLAY,
  2478. "test");
  2479. fillGraphicBufferColor(buffer, Rect(0, 0, 32, 32), Color::RED);
  2480. Transaction()
  2481. .setBuffer(layer, buffer)
  2482. .setApi(layer, NATIVE_WINDOW_API_CPU)
  2483. .apply();
  2484. auto shot = getScreenCapture();
  2485. shot->expectColor(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::RED);
  2486. shot->expectBorder(Rect(0, 0, mDisplayWidth, mDisplayHeight), Color::BLACK);
  2487. }
  2488. TEST_F(LayerTransactionTest, SetSidebandStreamNull_BufferState) {
  2489. sp<SurfaceControl> layer;
  2490. ASSERT_NO_FATAL_FAILURE(
  2491. layer = createLayer("test", 32, 32, ISurfaceComposerClient::eFXSurfaceBufferState));
  2492. // verify this doesn't cause a crash
  2493. Transaction().setSidebandStream(layer, nullptr).apply();
  2494. }
  2495. TEST_F(LayerTransactionTest, ReparentToSelf) {
  2496. sp<SurfaceControl> layer;
  2497. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", 32, 32));
  2498. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(layer, Color::RED, 32, 32));
  2499. Transaction().reparent(layer, layer->getHandle()).apply();
  2500. {
  2501. // We expect the transaction to be silently dropped, but for SurfaceFlinger
  2502. // to still be functioning.
  2503. SCOPED_TRACE("after reparent to self");
  2504. const Rect rect(0, 0, 32, 32);
  2505. auto shot = screenshot();
  2506. shot->expectColor(rect, Color::RED);
  2507. shot->expectBorder(rect, Color::BLACK);
  2508. }
  2509. }
  2510. class ColorTransformHelper {
  2511. public:
  2512. static void DegammaColorSingle(half& s) {
  2513. if (s <= 0.03928f)
  2514. s = s / 12.92f;
  2515. else
  2516. s = pow((s + 0.055f) / 1.055f, 2.4f);
  2517. }
  2518. static void DegammaColor(half3& color) {
  2519. DegammaColorSingle(color.r);
  2520. DegammaColorSingle(color.g);
  2521. DegammaColorSingle(color.b);
  2522. }
  2523. static void GammaColorSingle(half& s) {
  2524. if (s <= 0.0031308f) {
  2525. s = s * 12.92f;
  2526. } else {
  2527. s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
  2528. }
  2529. }
  2530. static void GammaColor(half3& color) {
  2531. GammaColorSingle(color.r);
  2532. GammaColorSingle(color.g);
  2533. GammaColorSingle(color.b);
  2534. }
  2535. static void applyMatrix(half3& color, const mat3& mat) {
  2536. half3 ret = half3(0);
  2537. for (int i = 0; i < 3; i++) {
  2538. for (int j = 0; j < 3; j++) {
  2539. ret[i] = ret[i] + color[j] * mat[j][i];
  2540. }
  2541. }
  2542. color = ret;
  2543. }
  2544. };
  2545. TEST_P(LayerRenderTypeTransactionTest, SetColorTransformBasic) {
  2546. sp<SurfaceControl> colorLayer;
  2547. ASSERT_NO_FATAL_FAILURE(colorLayer =
  2548. createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
  2549. ISurfaceComposerClient::eFXSurfaceColor));
  2550. Transaction()
  2551. .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
  2552. .setLayer(colorLayer, mLayerZBase + 1)
  2553. .apply();
  2554. {
  2555. SCOPED_TRACE("default color");
  2556. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
  2557. }
  2558. const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
  2559. half3 expected = color;
  2560. mat3 matrix;
  2561. matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
  2562. matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
  2563. matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
  2564. // degamma before applying the matrix
  2565. if (mColorManagementUsed) {
  2566. ColorTransformHelper::DegammaColor(expected);
  2567. }
  2568. ColorTransformHelper::applyMatrix(expected, matrix);
  2569. if (mColorManagementUsed) {
  2570. ColorTransformHelper::GammaColor(expected);
  2571. }
  2572. const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
  2573. uint8_t(expected.b * 255), 255};
  2574. // this is handwavy, but the precison loss scaled by 255 (8-bit per
  2575. // channel) should be less than one
  2576. const uint8_t tolerance = 1;
  2577. Transaction().setColor(colorLayer, color)
  2578. .setColorTransform(colorLayer, matrix, vec3()).apply();
  2579. {
  2580. SCOPED_TRACE("new color");
  2581. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
  2582. }
  2583. }
  2584. TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnParent) {
  2585. sp<SurfaceControl> parentLayer;
  2586. sp<SurfaceControl> colorLayer;
  2587. ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
  2588. 0 /* buffer height */,
  2589. ISurfaceComposerClient::eFXSurfaceContainer));
  2590. ASSERT_NO_FATAL_FAILURE(
  2591. colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
  2592. ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
  2593. Transaction()
  2594. .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
  2595. .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
  2596. .setLayer(parentLayer, mLayerZBase + 1)
  2597. .apply();
  2598. {
  2599. SCOPED_TRACE("default color");
  2600. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
  2601. }
  2602. const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
  2603. half3 expected = color;
  2604. mat3 matrix;
  2605. matrix[0][0] = 0.3; matrix[1][0] = 0.59; matrix[2][0] = 0.11;
  2606. matrix[0][1] = 0.3; matrix[1][1] = 0.59; matrix[2][1] = 0.11;
  2607. matrix[0][2] = 0.3; matrix[1][2] = 0.59; matrix[2][2] = 0.11;
  2608. // degamma before applying the matrix
  2609. if (mColorManagementUsed) {
  2610. ColorTransformHelper::DegammaColor(expected);
  2611. }
  2612. ColorTransformHelper::applyMatrix(expected, matrix);
  2613. if (mColorManagementUsed) {
  2614. ColorTransformHelper::GammaColor(expected);
  2615. }
  2616. const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
  2617. uint8_t(expected.b * 255), 255};
  2618. // this is handwavy, but the precison loss scaled by 255 (8-bit per
  2619. // channel) should be less than one
  2620. const uint8_t tolerance = 1;
  2621. Transaction()
  2622. .setColor(colorLayer, color)
  2623. .setColorTransform(parentLayer, matrix, vec3())
  2624. .apply();
  2625. {
  2626. SCOPED_TRACE("new color");
  2627. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
  2628. }
  2629. }
  2630. TEST_P(LayerRenderTypeTransactionTest, SetColorTransformOnChildAndParent) {
  2631. sp<SurfaceControl> parentLayer;
  2632. sp<SurfaceControl> colorLayer;
  2633. ASSERT_NO_FATAL_FAILURE(parentLayer = createLayer("parent", 0 /* buffer width */,
  2634. 0 /* buffer height */,
  2635. ISurfaceComposerClient::eFXSurfaceContainer));
  2636. ASSERT_NO_FATAL_FAILURE(
  2637. colorLayer = createLayer("test", 0 /* buffer width */, 0 /* buffer height */,
  2638. ISurfaceComposerClient::eFXSurfaceColor, parentLayer.get()));
  2639. Transaction()
  2640. .setCrop_legacy(parentLayer, Rect(0, 0, 100, 100))
  2641. .setCrop_legacy(colorLayer, Rect(0, 0, 32, 32))
  2642. .setLayer(parentLayer, mLayerZBase + 1)
  2643. .apply();
  2644. {
  2645. SCOPED_TRACE("default color");
  2646. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), Color::BLACK);
  2647. }
  2648. const half3 color(50.0f / 255.0f, 100.0f / 255.0f, 150.0f / 255.0f);
  2649. half3 expected = color;
  2650. mat3 matrixChild;
  2651. matrixChild[0][0] = 0.3; matrixChild[1][0] = 0.59; matrixChild[2][0] = 0.11;
  2652. matrixChild[0][1] = 0.3; matrixChild[1][1] = 0.59; matrixChild[2][1] = 0.11;
  2653. matrixChild[0][2] = 0.3; matrixChild[1][2] = 0.59; matrixChild[2][2] = 0.11;
  2654. mat3 matrixParent;
  2655. matrixParent[0][0] = 0.2; matrixParent[1][0] = 0.4; matrixParent[2][0] = 0.10;
  2656. matrixParent[0][1] = 0.2; matrixParent[1][1] = 0.4; matrixParent[2][1] = 0.10;
  2657. matrixParent[0][2] = 0.2; matrixParent[1][2] = 0.4; matrixParent[2][2] = 0.10;
  2658. // degamma before applying the matrix
  2659. if (mColorManagementUsed) {
  2660. ColorTransformHelper::DegammaColor(expected);
  2661. }
  2662. ColorTransformHelper::applyMatrix(expected, matrixChild);
  2663. ColorTransformHelper::applyMatrix(expected, matrixParent);
  2664. if (mColorManagementUsed) {
  2665. ColorTransformHelper::GammaColor(expected);
  2666. }
  2667. const Color expectedColor = {uint8_t(expected.r * 255), uint8_t(expected.g * 255),
  2668. uint8_t(expected.b * 255), 255};
  2669. // this is handwavy, but the precison loss scaled by 255 (8-bit per
  2670. // channel) should be less than one
  2671. const uint8_t tolerance = 1;
  2672. Transaction()
  2673. .setColor(colorLayer, color)
  2674. .setColorTransform(parentLayer, matrixParent, vec3())
  2675. .setColorTransform(colorLayer, matrixChild, vec3())
  2676. .apply();
  2677. {
  2678. SCOPED_TRACE("new color");
  2679. getScreenCapture()->expectColor(Rect(0, 0, 32, 32), expectedColor, tolerance);
  2680. }
  2681. }
  2682. struct CallbackData {
  2683. CallbackData() = default;
  2684. CallbackData(nsecs_t time, const sp<Fence>& fence,
  2685. const std::vector<SurfaceControlStats>& stats)
  2686. : latchTime(time), presentFence(fence), surfaceControlStats(stats) {}
  2687. nsecs_t latchTime;
  2688. sp<Fence> presentFence;
  2689. std::vector<SurfaceControlStats> surfaceControlStats;
  2690. };
  2691. class ExpectedResult {
  2692. public:
  2693. enum Transaction {
  2694. NOT_PRESENTED = 0,
  2695. PRESENTED,
  2696. };
  2697. enum Buffer {
  2698. NOT_ACQUIRED = 0,
  2699. ACQUIRED,
  2700. };
  2701. enum PreviousBuffer {
  2702. NOT_RELEASED = 0,
  2703. RELEASED,
  2704. UNKNOWN,
  2705. };
  2706. void reset() {
  2707. mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
  2708. mExpectedSurfaceResults.clear();
  2709. }
  2710. void addSurface(ExpectedResult::Transaction transactionResult, const sp<SurfaceControl>& layer,
  2711. ExpectedResult::Buffer bufferResult = ACQUIRED,
  2712. ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
  2713. mTransactionResult = transactionResult;
  2714. mExpectedSurfaceResults.emplace(std::piecewise_construct, std::forward_as_tuple(layer),
  2715. std::forward_as_tuple(bufferResult, previousBufferResult));
  2716. }
  2717. void addSurfaces(ExpectedResult::Transaction transactionResult,
  2718. const std::vector<sp<SurfaceControl>>& layers,
  2719. ExpectedResult::Buffer bufferResult = ACQUIRED,
  2720. ExpectedResult::PreviousBuffer previousBufferResult = NOT_RELEASED) {
  2721. for (const auto& layer : layers) {
  2722. addSurface(transactionResult, layer, bufferResult, previousBufferResult);
  2723. }
  2724. }
  2725. void addExpectedPresentTime(nsecs_t expectedPresentTime) {
  2726. mExpectedPresentTime = expectedPresentTime;
  2727. }
  2728. void verifyCallbackData(const CallbackData& callbackData) const {
  2729. const auto& [latchTime, presentFence, surfaceControlStats] = callbackData;
  2730. if (mTransactionResult == ExpectedResult::Transaction::PRESENTED) {
  2731. ASSERT_GE(latchTime, 0) << "bad latch time";
  2732. ASSERT_NE(presentFence, nullptr);
  2733. if (mExpectedPresentTime >= 0) {
  2734. ASSERT_EQ(presentFence->wait(3000), NO_ERROR);
  2735. ASSERT_GE(presentFence->getSignalTime(), mExpectedPresentTime - nsecs_t(5 * 1e6));
  2736. // if the panel is running at 30 hz, at the worst case, our expected time just
  2737. // misses vsync and we have to wait another 33.3ms
  2738. ASSERT_LE(presentFence->getSignalTime(),
  2739. mExpectedPresentTime + nsecs_t(66.666666 * 1e6));
  2740. }
  2741. } else {
  2742. ASSERT_EQ(presentFence, nullptr) << "transaction shouldn't have been presented";
  2743. ASSERT_EQ(latchTime, -1) << "unpresented transactions shouldn't be latched";
  2744. }
  2745. ASSERT_EQ(surfaceControlStats.size(), mExpectedSurfaceResults.size())
  2746. << "wrong number of surfaces";
  2747. for (const auto& stats : surfaceControlStats) {
  2748. ASSERT_NE(stats.surfaceControl, nullptr) << "returned null surface control";
  2749. const auto& expectedSurfaceResult = mExpectedSurfaceResults.find(stats.surfaceControl);
  2750. ASSERT_NE(expectedSurfaceResult, mExpectedSurfaceResults.end())
  2751. << "unexpected surface control";
  2752. expectedSurfaceResult->second.verifySurfaceControlStats(stats, latchTime);
  2753. }
  2754. }
  2755. private:
  2756. class ExpectedSurfaceResult {
  2757. public:
  2758. ExpectedSurfaceResult(ExpectedResult::Buffer bufferResult,
  2759. ExpectedResult::PreviousBuffer previousBufferResult)
  2760. : mBufferResult(bufferResult), mPreviousBufferResult(previousBufferResult) {}
  2761. void verifySurfaceControlStats(const SurfaceControlStats& surfaceControlStats,
  2762. nsecs_t latchTime) const {
  2763. const auto& [surfaceControl, acquireTime, previousReleaseFence] = surfaceControlStats;
  2764. ASSERT_EQ(acquireTime > 0, mBufferResult == ExpectedResult::Buffer::ACQUIRED)
  2765. << "bad acquire time";
  2766. ASSERT_LE(acquireTime, latchTime) << "acquire time should be <= latch time";
  2767. if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::RELEASED) {
  2768. ASSERT_NE(previousReleaseFence, nullptr)
  2769. << "failed to set release prev buffer fence";
  2770. } else if (mPreviousBufferResult == ExpectedResult::PreviousBuffer::NOT_RELEASED) {
  2771. ASSERT_EQ(previousReleaseFence, nullptr)
  2772. << "should not have set released prev buffer fence";
  2773. }
  2774. }
  2775. private:
  2776. ExpectedResult::Buffer mBufferResult;
  2777. ExpectedResult::PreviousBuffer mPreviousBufferResult;
  2778. };
  2779. struct SCHash {
  2780. std::size_t operator()(const sp<SurfaceControl>& sc) const {
  2781. return std::hash<IBinder*>{}(sc->getHandle().get());
  2782. }
  2783. };
  2784. ExpectedResult::Transaction mTransactionResult = ExpectedResult::Transaction::NOT_PRESENTED;
  2785. nsecs_t mExpectedPresentTime = -1;
  2786. std::unordered_map<sp<SurfaceControl>, ExpectedSurfaceResult, SCHash> mExpectedSurfaceResults;
  2787. };
  2788. class CallbackHelper {
  2789. public:
  2790. static void function(void* callbackContext, nsecs_t latchTime, const sp<Fence>& presentFence,
  2791. const std::vector<SurfaceControlStats>& stats) {
  2792. if (!callbackContext) {
  2793. ALOGE("failed to get callback context");
  2794. }
  2795. CallbackHelper* helper = static_cast<CallbackHelper*>(callbackContext);
  2796. std::lock_guard lock(helper->mMutex);
  2797. helper->mCallbackDataQueue.emplace(latchTime, presentFence, stats);
  2798. helper->mConditionVariable.notify_all();
  2799. }
  2800. void getCallbackData(CallbackData* outData) {
  2801. std::unique_lock lock(mMutex);
  2802. if (mCallbackDataQueue.empty()) {
  2803. ASSERT_NE(mConditionVariable.wait_for(lock, std::chrono::seconds(3)),
  2804. std::cv_status::timeout)
  2805. << "did not receive callback";
  2806. }
  2807. *outData = std::move(mCallbackDataQueue.front());
  2808. mCallbackDataQueue.pop();
  2809. }
  2810. void verifyFinalState() {
  2811. // Wait to see if there are extra callbacks
  2812. std::this_thread::sleep_for(500ms);
  2813. std::lock_guard lock(mMutex);
  2814. EXPECT_EQ(mCallbackDataQueue.size(), 0) << "extra callbacks received";
  2815. mCallbackDataQueue = {};
  2816. }
  2817. void* getContext() { return static_cast<void*>(this); }
  2818. std::mutex mMutex;
  2819. std::condition_variable mConditionVariable;
  2820. std::queue<CallbackData> mCallbackDataQueue;
  2821. };
  2822. class LayerCallbackTest : public LayerTransactionTest {
  2823. public:
  2824. virtual sp<SurfaceControl> createBufferStateLayer() {
  2825. return createLayer(mClient, "test", 0, 0, ISurfaceComposerClient::eFXSurfaceBufferState);
  2826. }
  2827. static int fillTransaction(Transaction& transaction, CallbackHelper* callbackHelper,
  2828. const sp<SurfaceControl>& layer = nullptr, bool setBuffer = true,
  2829. bool setBackgroundColor = false) {
  2830. if (layer) {
  2831. sp<GraphicBuffer> buffer;
  2832. sp<Fence> fence;
  2833. if (setBuffer) {
  2834. int err = getBuffer(&buffer, &fence);
  2835. if (err != NO_ERROR) {
  2836. return err;
  2837. }
  2838. transaction.setBuffer(layer, buffer);
  2839. transaction.setAcquireFence(layer, fence);
  2840. }
  2841. if (setBackgroundColor) {
  2842. transaction.setBackgroundColor(layer, /*color*/ half3(1.0f, 0, 0), /*alpha*/ 1.0f,
  2843. ui::Dataspace::UNKNOWN);
  2844. }
  2845. }
  2846. transaction.addTransactionCompletedCallback(callbackHelper->function,
  2847. callbackHelper->getContext());
  2848. return NO_ERROR;
  2849. }
  2850. static void waitForCallback(CallbackHelper& helper, const ExpectedResult& expectedResult,
  2851. bool finalState = false) {
  2852. CallbackData callbackData;
  2853. ASSERT_NO_FATAL_FAILURE(helper.getCallbackData(&callbackData));
  2854. EXPECT_NO_FATAL_FAILURE(expectedResult.verifyCallbackData(callbackData));
  2855. if (finalState) {
  2856. ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
  2857. }
  2858. }
  2859. static void waitForCallbacks(CallbackHelper& helper,
  2860. const std::vector<ExpectedResult>& expectedResults,
  2861. bool finalState = false) {
  2862. for (const auto& expectedResult : expectedResults) {
  2863. waitForCallback(helper, expectedResult);
  2864. }
  2865. if (finalState) {
  2866. ASSERT_NO_FATAL_FAILURE(helper.verifyFinalState());
  2867. }
  2868. }
  2869. };
  2870. TEST_F(LayerCallbackTest, BufferColor) {
  2871. sp<SurfaceControl> layer;
  2872. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  2873. Transaction transaction;
  2874. CallbackHelper callback;
  2875. int err = fillTransaction(transaction, &callback, layer, true, true);
  2876. if (err) {
  2877. GTEST_SUCCEED() << "test not supported";
  2878. return;
  2879. }
  2880. transaction.apply();
  2881. ExpectedResult expected;
  2882. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  2883. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  2884. }
  2885. TEST_F(LayerCallbackTest, NoBufferNoColor) {
  2886. sp<SurfaceControl> layer;
  2887. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  2888. Transaction transaction;
  2889. CallbackHelper callback;
  2890. int err = fillTransaction(transaction, &callback, layer, false, false);
  2891. if (err) {
  2892. GTEST_SUCCEED() << "test not supported";
  2893. return;
  2894. }
  2895. transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
  2896. ExpectedResult expected;
  2897. expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
  2898. ExpectedResult::Buffer::NOT_ACQUIRED);
  2899. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  2900. }
  2901. TEST_F(LayerCallbackTest, BufferNoColor) {
  2902. sp<SurfaceControl> layer;
  2903. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  2904. Transaction transaction;
  2905. CallbackHelper callback;
  2906. int err = fillTransaction(transaction, &callback, layer, true, false);
  2907. if (err) {
  2908. GTEST_SUCCEED() << "test not supported";
  2909. return;
  2910. }
  2911. transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
  2912. ExpectedResult expected;
  2913. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  2914. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  2915. }
  2916. TEST_F(LayerCallbackTest, NoBufferColor) {
  2917. sp<SurfaceControl> layer;
  2918. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  2919. Transaction transaction;
  2920. CallbackHelper callback;
  2921. int err = fillTransaction(transaction, &callback, layer, false, true);
  2922. if (err) {
  2923. GTEST_SUCCEED() << "test not supported";
  2924. return;
  2925. }
  2926. transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
  2927. ExpectedResult expected;
  2928. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
  2929. ExpectedResult::Buffer::NOT_ACQUIRED);
  2930. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  2931. }
  2932. TEST_F(LayerCallbackTest, NoStateChange) {
  2933. Transaction transaction;
  2934. CallbackHelper callback;
  2935. int err = fillTransaction(transaction, &callback);
  2936. if (err) {
  2937. GTEST_SUCCEED() << "test not supported";
  2938. return;
  2939. }
  2940. transaction.apply();
  2941. ExpectedResult expected;
  2942. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  2943. }
  2944. TEST_F(LayerCallbackTest, OffScreen) {
  2945. sp<SurfaceControl> layer;
  2946. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  2947. Transaction transaction;
  2948. CallbackHelper callback;
  2949. int err = fillTransaction(transaction, &callback, layer);
  2950. if (err) {
  2951. GTEST_SUCCEED() << "test not supported";
  2952. return;
  2953. }
  2954. transaction.setFrame(layer, Rect(-100, -100, 100, 100)).apply();
  2955. ExpectedResult expected;
  2956. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  2957. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  2958. }
  2959. TEST_F(LayerCallbackTest, MergeBufferNoColor) {
  2960. sp<SurfaceControl> layer1, layer2;
  2961. ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
  2962. ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
  2963. Transaction transaction1, transaction2;
  2964. CallbackHelper callback1, callback2;
  2965. int err = fillTransaction(transaction1, &callback1, layer1);
  2966. if (err) {
  2967. GTEST_SUCCEED() << "test not supported";
  2968. return;
  2969. }
  2970. err = fillTransaction(transaction2, &callback2, layer2);
  2971. if (err) {
  2972. GTEST_SUCCEED() << "test not supported";
  2973. return;
  2974. }
  2975. transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
  2976. transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
  2977. ExpectedResult expected;
  2978. expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
  2979. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
  2980. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
  2981. }
  2982. TEST_F(LayerCallbackTest, MergeNoBufferColor) {
  2983. sp<SurfaceControl> layer1, layer2;
  2984. ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
  2985. ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
  2986. Transaction transaction1, transaction2;
  2987. CallbackHelper callback1, callback2;
  2988. int err = fillTransaction(transaction1, &callback1, layer1, false, true);
  2989. if (err) {
  2990. GTEST_SUCCEED() << "test not supported";
  2991. return;
  2992. }
  2993. err = fillTransaction(transaction2, &callback2, layer2, false, true);
  2994. if (err) {
  2995. GTEST_SUCCEED() << "test not supported";
  2996. return;
  2997. }
  2998. transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
  2999. transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
  3000. ExpectedResult expected;
  3001. expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
  3002. ExpectedResult::Buffer::NOT_ACQUIRED);
  3003. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
  3004. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
  3005. }
  3006. TEST_F(LayerCallbackTest, MergeOneBufferOneColor) {
  3007. sp<SurfaceControl> layer1, layer2;
  3008. ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
  3009. ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
  3010. Transaction transaction1, transaction2;
  3011. CallbackHelper callback1, callback2;
  3012. int err = fillTransaction(transaction1, &callback1, layer1);
  3013. if (err) {
  3014. GTEST_SUCCEED() << "test not supported";
  3015. return;
  3016. }
  3017. err = fillTransaction(transaction2, &callback2, layer2, false, true);
  3018. if (err) {
  3019. GTEST_SUCCEED() << "test not supported";
  3020. return;
  3021. }
  3022. transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
  3023. transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
  3024. ExpectedResult expected;
  3025. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer1);
  3026. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer2,
  3027. ExpectedResult::Buffer::NOT_ACQUIRED);
  3028. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
  3029. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
  3030. }
  3031. TEST_F(LayerCallbackTest, Merge_SameCallback) {
  3032. sp<SurfaceControl> layer1, layer2;
  3033. ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
  3034. ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
  3035. Transaction transaction1, transaction2;
  3036. CallbackHelper callback;
  3037. int err = fillTransaction(transaction1, &callback, layer1);
  3038. if (err) {
  3039. GTEST_SUCCEED() << "test not supported";
  3040. return;
  3041. }
  3042. err = fillTransaction(transaction2, &callback, layer2);
  3043. if (err) {
  3044. GTEST_SUCCEED() << "test not supported";
  3045. return;
  3046. }
  3047. transaction2.merge(std::move(transaction1)).apply();
  3048. ExpectedResult expected;
  3049. expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
  3050. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
  3051. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  3052. }
  3053. TEST_F(LayerCallbackTest, Merge_SameLayer) {
  3054. sp<SurfaceControl> layer;
  3055. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3056. Transaction transaction1, transaction2;
  3057. CallbackHelper callback1, callback2;
  3058. int err = fillTransaction(transaction1, &callback1, layer);
  3059. if (err) {
  3060. GTEST_SUCCEED() << "test not supported";
  3061. return;
  3062. }
  3063. err = fillTransaction(transaction2, &callback2, layer);
  3064. if (err) {
  3065. GTEST_SUCCEED() << "test not supported";
  3066. return;
  3067. }
  3068. transaction2.merge(std::move(transaction1)).apply();
  3069. ExpectedResult expected;
  3070. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  3071. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
  3072. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
  3073. }
  3074. TEST_F(LayerCallbackTest, Merge_DifferentClients) {
  3075. sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
  3076. client2(new SurfaceComposerClient);
  3077. ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
  3078. ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
  3079. sp<SurfaceControl> layer1, layer2;
  3080. ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
  3081. ISurfaceComposerClient::eFXSurfaceBufferState));
  3082. ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
  3083. ISurfaceComposerClient::eFXSurfaceBufferState));
  3084. Transaction transaction1, transaction2;
  3085. CallbackHelper callback1, callback2;
  3086. int err = fillTransaction(transaction1, &callback1, layer1);
  3087. if (err) {
  3088. GTEST_SUCCEED() << "test not supported";
  3089. return;
  3090. }
  3091. err = fillTransaction(transaction2, &callback2, layer2);
  3092. if (err) {
  3093. GTEST_SUCCEED() << "test not supported";
  3094. return;
  3095. }
  3096. transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
  3097. transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
  3098. ExpectedResult expected;
  3099. expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
  3100. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
  3101. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
  3102. }
  3103. TEST_F(LayerCallbackTest, MultipleTransactions) {
  3104. sp<SurfaceControl> layer;
  3105. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3106. Transaction transaction;
  3107. CallbackHelper callback;
  3108. for (size_t i = 0; i < 10; i++) {
  3109. int err = fillTransaction(transaction, &callback, layer);
  3110. if (err) {
  3111. GTEST_SUCCEED() << "test not supported";
  3112. return;
  3113. }
  3114. transaction.apply();
  3115. ExpectedResult expected;
  3116. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
  3117. ExpectedResult::Buffer::ACQUIRED,
  3118. (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
  3119. : ExpectedResult::PreviousBuffer::RELEASED);
  3120. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
  3121. }
  3122. ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
  3123. }
  3124. TEST_F(LayerCallbackTest, MultipleTransactions_NoStateChange) {
  3125. sp<SurfaceControl> layer;
  3126. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3127. Transaction transaction;
  3128. CallbackHelper callback;
  3129. for (size_t i = 0; i < 10; i++) {
  3130. ExpectedResult expected;
  3131. if (i == 0) {
  3132. int err = fillTransaction(transaction, &callback, layer);
  3133. if (err) {
  3134. GTEST_SUCCEED() << "test not supported";
  3135. return;
  3136. }
  3137. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  3138. } else {
  3139. int err = fillTransaction(transaction, &callback);
  3140. if (err) {
  3141. GTEST_SUCCEED() << "test not supported";
  3142. return;
  3143. }
  3144. }
  3145. transaction.apply();
  3146. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected));
  3147. }
  3148. ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
  3149. }
  3150. TEST_F(LayerCallbackTest, MultipleTransactions_SameStateChange) {
  3151. sp<SurfaceControl> layer;
  3152. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3153. Transaction transaction;
  3154. CallbackHelper callback;
  3155. for (size_t i = 0; i < 10; i++) {
  3156. if (i == 0) {
  3157. int err = fillTransaction(transaction, &callback, layer);
  3158. if (err) {
  3159. GTEST_SUCCEED() << "test not supported";
  3160. return;
  3161. }
  3162. } else {
  3163. int err = fillTransaction(transaction, &callback);
  3164. if (err) {
  3165. GTEST_SUCCEED() << "test not supported";
  3166. return;
  3167. }
  3168. }
  3169. transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
  3170. ExpectedResult expected;
  3171. expected.addSurface((i == 0) ? ExpectedResult::Transaction::PRESENTED
  3172. : ExpectedResult::Transaction::NOT_PRESENTED,
  3173. layer,
  3174. (i == 0) ? ExpectedResult::Buffer::ACQUIRED
  3175. : ExpectedResult::Buffer::NOT_ACQUIRED);
  3176. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, i == 0));
  3177. }
  3178. ASSERT_NO_FATAL_FAILURE(callback.verifyFinalState());
  3179. }
  3180. TEST_F(LayerCallbackTest, MultipleTransactions_Merge) {
  3181. sp<SurfaceControl> layer1, layer2;
  3182. ASSERT_NO_FATAL_FAILURE(layer1 = createBufferStateLayer());
  3183. ASSERT_NO_FATAL_FAILURE(layer2 = createBufferStateLayer());
  3184. Transaction transaction1, transaction2;
  3185. CallbackHelper callback1, callback2;
  3186. for (size_t i = 0; i < 10; i++) {
  3187. int err = fillTransaction(transaction1, &callback1, layer1);
  3188. if (err) {
  3189. GTEST_SUCCEED() << "test not supported";
  3190. return;
  3191. }
  3192. err = fillTransaction(transaction2, &callback2, layer2);
  3193. if (err) {
  3194. GTEST_SUCCEED() << "test not supported";
  3195. return;
  3196. }
  3197. transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
  3198. transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
  3199. ExpectedResult expected;
  3200. expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
  3201. ExpectedResult::Buffer::ACQUIRED,
  3202. (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
  3203. : ExpectedResult::PreviousBuffer::RELEASED);
  3204. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
  3205. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
  3206. }
  3207. ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
  3208. ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
  3209. }
  3210. TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients) {
  3211. sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
  3212. client2(new SurfaceComposerClient);
  3213. ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
  3214. ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
  3215. sp<SurfaceControl> layer1, layer2;
  3216. ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
  3217. ISurfaceComposerClient::eFXSurfaceBufferState));
  3218. ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
  3219. ISurfaceComposerClient::eFXSurfaceBufferState));
  3220. Transaction transaction1, transaction2;
  3221. CallbackHelper callback1, callback2;
  3222. for (size_t i = 0; i < 10; i++) {
  3223. int err = fillTransaction(transaction1, &callback1, layer1);
  3224. if (err) {
  3225. GTEST_SUCCEED() << "test not supported";
  3226. return;
  3227. }
  3228. err = fillTransaction(transaction2, &callback2, layer2);
  3229. if (err) {
  3230. GTEST_SUCCEED() << "test not supported";
  3231. return;
  3232. }
  3233. transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
  3234. transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
  3235. ExpectedResult expected;
  3236. expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2},
  3237. ExpectedResult::Buffer::ACQUIRED,
  3238. (i == 0) ? ExpectedResult::PreviousBuffer::NOT_RELEASED
  3239. : ExpectedResult::PreviousBuffer::RELEASED);
  3240. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected));
  3241. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected));
  3242. }
  3243. ASSERT_NO_FATAL_FAILURE(callback1.verifyFinalState());
  3244. ASSERT_NO_FATAL_FAILURE(callback2.verifyFinalState());
  3245. }
  3246. TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_NoStateChange) {
  3247. sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
  3248. client2(new SurfaceComposerClient);
  3249. ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
  3250. ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
  3251. sp<SurfaceControl> layer1, layer2;
  3252. ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
  3253. ISurfaceComposerClient::eFXSurfaceBufferState));
  3254. ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
  3255. ISurfaceComposerClient::eFXSurfaceBufferState));
  3256. Transaction transaction1, transaction2;
  3257. CallbackHelper callback1, callback2;
  3258. // Normal call to set up test
  3259. int err = fillTransaction(transaction1, &callback1, layer1);
  3260. if (err) {
  3261. GTEST_SUCCEED() << "test not supported";
  3262. return;
  3263. }
  3264. err = fillTransaction(transaction2, &callback2, layer2);
  3265. if (err) {
  3266. GTEST_SUCCEED() << "test not supported";
  3267. return;
  3268. }
  3269. transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
  3270. transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
  3271. ExpectedResult expected;
  3272. expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
  3273. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
  3274. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
  3275. expected.reset();
  3276. // Test
  3277. err = fillTransaction(transaction1, &callback1);
  3278. if (err) {
  3279. GTEST_SUCCEED() << "test not supported";
  3280. return;
  3281. }
  3282. err = fillTransaction(transaction2, &callback2);
  3283. if (err) {
  3284. GTEST_SUCCEED() << "test not supported";
  3285. return;
  3286. }
  3287. transaction2.merge(std::move(transaction1)).apply();
  3288. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
  3289. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
  3290. }
  3291. TEST_F(LayerCallbackTest, MultipleTransactions_Merge_DifferentClients_SameStateChange) {
  3292. sp<SurfaceComposerClient> client1(new SurfaceComposerClient),
  3293. client2(new SurfaceComposerClient);
  3294. ASSERT_EQ(NO_ERROR, client1->initCheck()) << "failed to create SurfaceComposerClient";
  3295. ASSERT_EQ(NO_ERROR, client2->initCheck()) << "failed to create SurfaceComposerClient";
  3296. sp<SurfaceControl> layer1, layer2;
  3297. ASSERT_NO_FATAL_FAILURE(layer1 = createLayer(client1, "test", 0, 0,
  3298. ISurfaceComposerClient::eFXSurfaceBufferState));
  3299. ASSERT_NO_FATAL_FAILURE(layer2 = createLayer(client2, "test", 0, 0,
  3300. ISurfaceComposerClient::eFXSurfaceBufferState));
  3301. Transaction transaction1, transaction2;
  3302. CallbackHelper callback1, callback2;
  3303. // Normal call to set up test
  3304. int err = fillTransaction(transaction1, &callback1, layer1);
  3305. if (err) {
  3306. GTEST_SUCCEED() << "test not supported";
  3307. return;
  3308. }
  3309. err = fillTransaction(transaction2, &callback2, layer2);
  3310. if (err) {
  3311. GTEST_SUCCEED() << "test not supported";
  3312. return;
  3313. }
  3314. transaction1.setFrame(layer1, Rect(0, 0, 32, 32));
  3315. transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
  3316. ExpectedResult expected;
  3317. expected.addSurfaces(ExpectedResult::Transaction::PRESENTED, {layer1, layer2});
  3318. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
  3319. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
  3320. expected.reset();
  3321. // Test
  3322. err = fillTransaction(transaction1, &callback1);
  3323. if (err) {
  3324. GTEST_SUCCEED() << "test not supported";
  3325. return;
  3326. }
  3327. err = fillTransaction(transaction2, &callback2);
  3328. if (err) {
  3329. GTEST_SUCCEED() << "test not supported";
  3330. return;
  3331. }
  3332. transaction2.setFrame(layer2, Rect(32, 32, 64, 64)).merge(std::move(transaction1)).apply();
  3333. expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer2,
  3334. ExpectedResult::Buffer::NOT_ACQUIRED);
  3335. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected, true));
  3336. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected, true));
  3337. }
  3338. TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame) {
  3339. sp<SurfaceControl> layer;
  3340. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3341. Transaction transaction;
  3342. CallbackHelper callback;
  3343. std::vector<ExpectedResult> expectedResults(50);
  3344. for (auto& expected : expectedResults) {
  3345. expected.reset();
  3346. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
  3347. ExpectedResult::Buffer::ACQUIRED,
  3348. ExpectedResult::PreviousBuffer::UNKNOWN);
  3349. int err = fillTransaction(transaction, &callback, layer);
  3350. if (err) {
  3351. GTEST_SUCCEED() << "test not supported";
  3352. return;
  3353. }
  3354. transaction.apply();
  3355. }
  3356. EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
  3357. }
  3358. TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_NoStateChange) {
  3359. sp<SurfaceControl> layer;
  3360. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3361. // Normal call to set up test
  3362. Transaction transaction;
  3363. CallbackHelper callback;
  3364. int err = fillTransaction(transaction, &callback, layer);
  3365. if (err) {
  3366. GTEST_SUCCEED() << "test not supported";
  3367. return;
  3368. }
  3369. transaction.apply();
  3370. ExpectedResult expected;
  3371. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  3372. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  3373. // Test
  3374. std::vector<ExpectedResult> expectedResults(50);
  3375. for (auto& expected : expectedResults) {
  3376. expected.reset();
  3377. err = fillTransaction(transaction, &callback);
  3378. if (err) {
  3379. GTEST_SUCCEED() << "test not supported";
  3380. return;
  3381. }
  3382. transaction.apply();
  3383. }
  3384. EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
  3385. }
  3386. TEST_F(LayerCallbackTest, MultipleTransactions_SingleFrame_SameStateChange) {
  3387. sp<SurfaceControl> layer;
  3388. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3389. // Normal call to set up test
  3390. Transaction transaction;
  3391. CallbackHelper callback;
  3392. int err = fillTransaction(transaction, &callback, layer);
  3393. if (err) {
  3394. GTEST_SUCCEED() << "test not supported";
  3395. return;
  3396. }
  3397. transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
  3398. ExpectedResult expectedResult;
  3399. expectedResult.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  3400. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expectedResult, true));
  3401. // Test
  3402. std::vector<ExpectedResult> expectedResults(50);
  3403. for (auto& expected : expectedResults) {
  3404. expected.reset();
  3405. expected.addSurface(ExpectedResult::Transaction::NOT_PRESENTED, layer,
  3406. ExpectedResult::Buffer::NOT_ACQUIRED);
  3407. err = fillTransaction(transaction, &callback);
  3408. if (err) {
  3409. GTEST_SUCCEED() << "test not supported";
  3410. return;
  3411. }
  3412. transaction.setFrame(layer, Rect(0, 0, 32, 32)).apply();
  3413. }
  3414. EXPECT_NO_FATAL_FAILURE(waitForCallbacks(callback, expectedResults, true));
  3415. }
  3416. TEST_F(LayerCallbackTest, DesiredPresentTime) {
  3417. sp<SurfaceControl> layer;
  3418. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3419. Transaction transaction;
  3420. CallbackHelper callback;
  3421. int err = fillTransaction(transaction, &callback, layer);
  3422. if (err) {
  3423. GTEST_SUCCEED() << "test not supported";
  3424. return;
  3425. }
  3426. // Try to present 100ms in the future
  3427. nsecs_t time = systemTime() + (100 * 1e6);
  3428. transaction.setDesiredPresentTime(time);
  3429. transaction.apply();
  3430. ExpectedResult expected;
  3431. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  3432. expected.addExpectedPresentTime(time);
  3433. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  3434. }
  3435. TEST_F(LayerCallbackTest, DesiredPresentTime_Multiple) {
  3436. sp<SurfaceControl> layer;
  3437. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3438. Transaction transaction;
  3439. CallbackHelper callback1;
  3440. int err = fillTransaction(transaction, &callback1, layer);
  3441. if (err) {
  3442. GTEST_SUCCEED() << "test not supported";
  3443. return;
  3444. }
  3445. // Try to present 100ms in the future
  3446. nsecs_t time = systemTime() + (100 * 1e6);
  3447. transaction.setDesiredPresentTime(time);
  3448. transaction.apply();
  3449. ExpectedResult expected1;
  3450. expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  3451. expected1.addExpectedPresentTime(time);
  3452. CallbackHelper callback2;
  3453. err = fillTransaction(transaction, &callback2, layer);
  3454. if (err) {
  3455. GTEST_SUCCEED() << "test not supported";
  3456. return;
  3457. }
  3458. // Try to present 33ms after the first frame
  3459. time += (33.3 * 1e6);
  3460. transaction.setDesiredPresentTime(time);
  3461. transaction.apply();
  3462. ExpectedResult expected2;
  3463. expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
  3464. ExpectedResult::Buffer::ACQUIRED,
  3465. ExpectedResult::PreviousBuffer::RELEASED);
  3466. expected2.addExpectedPresentTime(time);
  3467. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
  3468. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
  3469. }
  3470. TEST_F(LayerCallbackTest, DesiredPresentTime_OutOfOrder) {
  3471. sp<SurfaceControl> layer;
  3472. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3473. Transaction transaction;
  3474. CallbackHelper callback1;
  3475. int err = fillTransaction(transaction, &callback1, layer);
  3476. if (err) {
  3477. GTEST_SUCCEED() << "test not supported";
  3478. return;
  3479. }
  3480. // Try to present 100ms in the future
  3481. nsecs_t time = systemTime() + (100 * 1e6);
  3482. transaction.setDesiredPresentTime(time);
  3483. transaction.apply();
  3484. ExpectedResult expected1;
  3485. expected1.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  3486. expected1.addExpectedPresentTime(time);
  3487. CallbackHelper callback2;
  3488. err = fillTransaction(transaction, &callback2, layer);
  3489. if (err) {
  3490. GTEST_SUCCEED() << "test not supported";
  3491. return;
  3492. }
  3493. // Try to present 33ms before the previous frame
  3494. time -= (33.3 * 1e6);
  3495. transaction.setDesiredPresentTime(time);
  3496. transaction.apply();
  3497. ExpectedResult expected2;
  3498. expected2.addSurface(ExpectedResult::Transaction::PRESENTED, layer,
  3499. ExpectedResult::Buffer::ACQUIRED,
  3500. ExpectedResult::PreviousBuffer::RELEASED);
  3501. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback1, expected1, true));
  3502. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback2, expected2, true));
  3503. }
  3504. TEST_F(LayerCallbackTest, DesiredPresentTime_Past) {
  3505. sp<SurfaceControl> layer;
  3506. ASSERT_NO_FATAL_FAILURE(layer = createBufferStateLayer());
  3507. Transaction transaction;
  3508. CallbackHelper callback;
  3509. int err = fillTransaction(transaction, &callback, layer);
  3510. if (err) {
  3511. GTEST_SUCCEED() << "test not supported";
  3512. return;
  3513. }
  3514. // Try to present 100ms in the past
  3515. nsecs_t time = systemTime() - (100 * 1e6);
  3516. transaction.setDesiredPresentTime(time);
  3517. transaction.apply();
  3518. ExpectedResult expected;
  3519. expected.addSurface(ExpectedResult::Transaction::PRESENTED, layer);
  3520. expected.addExpectedPresentTime(systemTime());
  3521. EXPECT_NO_FATAL_FAILURE(waitForCallback(callback, expected, true));
  3522. }
  3523. class LayerUpdateTest : public LayerTransactionTest {
  3524. protected:
  3525. virtual void SetUp() {
  3526. LayerTransactionTest::SetUp();
  3527. ASSERT_EQ(NO_ERROR, mClient->initCheck());
  3528. const auto display = SurfaceComposerClient::getInternalDisplayToken();
  3529. ASSERT_FALSE(display == nullptr);
  3530. DisplayInfo info;
  3531. ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
  3532. ssize_t displayWidth = info.w;
  3533. ssize_t displayHeight = info.h;
  3534. // Background surface
  3535. mBGSurfaceControl = createLayer(String8("BG Test Surface"), displayWidth,
  3536. displayHeight, 0);
  3537. ASSERT_TRUE(mBGSurfaceControl != nullptr);
  3538. ASSERT_TRUE(mBGSurfaceControl->isValid());
  3539. fillSurfaceRGBA8(mBGSurfaceControl, 63, 63, 195);
  3540. // Foreground surface
  3541. mFGSurfaceControl = createLayer(String8("FG Test Surface"), 64, 64, 0);
  3542. ASSERT_TRUE(mFGSurfaceControl != nullptr);
  3543. ASSERT_TRUE(mFGSurfaceControl->isValid());
  3544. fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
  3545. // Synchronization surface
  3546. mSyncSurfaceControl = createLayer(String8("Sync Test Surface"), 1, 1, 0);
  3547. ASSERT_TRUE(mSyncSurfaceControl != nullptr);
  3548. ASSERT_TRUE(mSyncSurfaceControl->isValid());
  3549. fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
  3550. asTransaction([&](Transaction& t) {
  3551. t.setDisplayLayerStack(display, 0);
  3552. t.setLayer(mBGSurfaceControl, INT32_MAX - 2).show(mBGSurfaceControl);
  3553. t.setLayer(mFGSurfaceControl, INT32_MAX - 1)
  3554. .setPosition(mFGSurfaceControl, 64, 64)
  3555. .show(mFGSurfaceControl);
  3556. t.setLayer(mSyncSurfaceControl, INT32_MAX - 1)
  3557. .setPosition(mSyncSurfaceControl, displayWidth - 2, displayHeight - 2)
  3558. .show(mSyncSurfaceControl);
  3559. });
  3560. }
  3561. virtual void TearDown() {
  3562. LayerTransactionTest::TearDown();
  3563. mBGSurfaceControl = 0;
  3564. mFGSurfaceControl = 0;
  3565. mSyncSurfaceControl = 0;
  3566. }
  3567. void waitForPostedBuffers() {
  3568. // Since the sync surface is in synchronous mode (i.e. double buffered)
  3569. // posting three buffers to it should ensure that at least two
  3570. // SurfaceFlinger::handlePageFlip calls have been made, which should
  3571. // guaranteed that a buffer posted to another Surface has been retired.
  3572. fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
  3573. fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
  3574. fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
  3575. }
  3576. sp<SurfaceControl> mBGSurfaceControl;
  3577. sp<SurfaceControl> mFGSurfaceControl;
  3578. // This surface is used to ensure that the buffers posted to
  3579. // mFGSurfaceControl have been picked up by SurfaceFlinger.
  3580. sp<SurfaceControl> mSyncSurfaceControl;
  3581. };
  3582. TEST_F(LayerUpdateTest, RelativesAreNotDetached) {
  3583. std::unique_ptr<ScreenCapture> sc;
  3584. sp<SurfaceControl> relative = createLayer(String8("relativeTestSurface"), 10, 10, 0);
  3585. fillSurfaceRGBA8(relative, 10, 10, 10);
  3586. waitForPostedBuffers();
  3587. Transaction{}
  3588. .setRelativeLayer(relative, mFGSurfaceControl->getHandle(), 1)
  3589. .setPosition(relative, 64, 64)
  3590. .apply();
  3591. {
  3592. // The relative should be on top of the FG control.
  3593. ScreenCapture::captureScreen(&sc);
  3594. sc->checkPixel(64, 64, 10, 10, 10);
  3595. }
  3596. Transaction{}.detachChildren(mFGSurfaceControl).apply();
  3597. {
  3598. // Nothing should change at this point.
  3599. ScreenCapture::captureScreen(&sc);
  3600. sc->checkPixel(64, 64, 10, 10, 10);
  3601. }
  3602. Transaction{}.hide(relative).apply();
  3603. {
  3604. // Ensure that the relative was actually hidden, rather than
  3605. // being left in the detached but visible state.
  3606. ScreenCapture::captureScreen(&sc);
  3607. sc->expectFGColor(64, 64);
  3608. }
  3609. }
  3610. class GeometryLatchingTest : public LayerUpdateTest {
  3611. protected:
  3612. void EXPECT_INITIAL_STATE(const char* trace) {
  3613. SCOPED_TRACE(trace);
  3614. ScreenCapture::captureScreen(&sc);
  3615. // We find the leading edge of the FG surface.
  3616. sc->expectFGColor(127, 127);
  3617. sc->expectBGColor(128, 128);
  3618. }
  3619. void lockAndFillFGBuffer() { fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63, false); }
  3620. void unlockFGBuffer() {
  3621. sp<Surface> s = mFGSurfaceControl->getSurface();
  3622. ASSERT_EQ(NO_ERROR, s->unlockAndPost());
  3623. waitForPostedBuffers();
  3624. }
  3625. void completeFGResize() {
  3626. fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
  3627. waitForPostedBuffers();
  3628. }
  3629. void restoreInitialState() {
  3630. asTransaction([&](Transaction& t) {
  3631. t.setSize(mFGSurfaceControl, 64, 64);
  3632. t.setPosition(mFGSurfaceControl, 64, 64);
  3633. t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 64, 64));
  3634. });
  3635. EXPECT_INITIAL_STATE("After restoring initial state");
  3636. }
  3637. std::unique_ptr<ScreenCapture> sc;
  3638. };
  3639. class CropLatchingTest : public GeometryLatchingTest {
  3640. protected:
  3641. void EXPECT_CROPPED_STATE(const char* trace) {
  3642. SCOPED_TRACE(trace);
  3643. ScreenCapture::captureScreen(&sc);
  3644. // The edge should be moved back one pixel by our crop.
  3645. sc->expectFGColor(126, 126);
  3646. sc->expectBGColor(127, 127);
  3647. sc->expectBGColor(128, 128);
  3648. }
  3649. void EXPECT_RESIZE_STATE(const char* trace) {
  3650. SCOPED_TRACE(trace);
  3651. ScreenCapture::captureScreen(&sc);
  3652. // The FG is now resized too 128,128 at 64,64
  3653. sc->expectFGColor(64, 64);
  3654. sc->expectFGColor(191, 191);
  3655. sc->expectBGColor(192, 192);
  3656. }
  3657. };
  3658. TEST_F(LayerUpdateTest, DeferredTransactionTest) {
  3659. std::unique_ptr<ScreenCapture> sc;
  3660. {
  3661. SCOPED_TRACE("before anything");
  3662. ScreenCapture::captureScreen(&sc);
  3663. sc->expectBGColor(32, 32);
  3664. sc->expectFGColor(96, 96);
  3665. sc->expectBGColor(160, 160);
  3666. }
  3667. // set up two deferred transactions on different frames
  3668. asTransaction([&](Transaction& t) {
  3669. t.setAlpha(mFGSurfaceControl, 0.75);
  3670. t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
  3671. mSyncSurfaceControl->getSurface()->getNextFrameNumber());
  3672. });
  3673. asTransaction([&](Transaction& t) {
  3674. t.setPosition(mFGSurfaceControl, 128, 128);
  3675. t.deferTransactionUntil_legacy(mFGSurfaceControl, mSyncSurfaceControl->getHandle(),
  3676. mSyncSurfaceControl->getSurface()->getNextFrameNumber() + 1);
  3677. });
  3678. {
  3679. SCOPED_TRACE("before any trigger");
  3680. ScreenCapture::captureScreen(&sc);
  3681. sc->expectBGColor(32, 32);
  3682. sc->expectFGColor(96, 96);
  3683. sc->expectBGColor(160, 160);
  3684. }
  3685. // should trigger the first deferred transaction, but not the second one
  3686. fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
  3687. {
  3688. SCOPED_TRACE("after first trigger");
  3689. ScreenCapture::captureScreen(&sc);
  3690. sc->expectBGColor(32, 32);
  3691. sc->checkPixel(96, 96, 162, 63, 96);
  3692. sc->expectBGColor(160, 160);
  3693. }
  3694. // should show up immediately since it's not deferred
  3695. asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 1.0); });
  3696. // trigger the second deferred transaction
  3697. fillSurfaceRGBA8(mSyncSurfaceControl, 31, 31, 31);
  3698. {
  3699. SCOPED_TRACE("after second trigger");
  3700. ScreenCapture::captureScreen(&sc);
  3701. sc->expectBGColor(32, 32);
  3702. sc->expectBGColor(96, 96);
  3703. sc->expectFGColor(160, 160);
  3704. }
  3705. }
  3706. TEST_F(LayerUpdateTest, LayerWithNoBuffersResizesImmediately) {
  3707. std::unique_ptr<ScreenCapture> sc;
  3708. sp<SurfaceControl> childNoBuffer =
  3709. createSurface(mClient, "Bufferless child", 0 /* buffer width */, 0 /* buffer height */,
  3710. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  3711. sp<SurfaceControl> childBuffer = createSurface(mClient, "Buffered child", 20, 20,
  3712. PIXEL_FORMAT_RGBA_8888, 0, childNoBuffer.get());
  3713. fillSurfaceRGBA8(childBuffer, 200, 200, 200);
  3714. SurfaceComposerClient::Transaction{}
  3715. .setCrop_legacy(childNoBuffer, Rect(0, 0, 10, 10))
  3716. .show(childNoBuffer)
  3717. .show(childBuffer)
  3718. .apply(true);
  3719. {
  3720. ScreenCapture::captureScreen(&sc);
  3721. sc->expectChildColor(73, 73);
  3722. sc->expectFGColor(74, 74);
  3723. }
  3724. SurfaceComposerClient::Transaction{}
  3725. .setCrop_legacy(childNoBuffer, Rect(0, 0, 20, 20))
  3726. .apply(true);
  3727. {
  3728. ScreenCapture::captureScreen(&sc);
  3729. sc->expectChildColor(73, 73);
  3730. sc->expectChildColor(74, 74);
  3731. }
  3732. }
  3733. TEST_F(LayerUpdateTest, MergingTransactions) {
  3734. std::unique_ptr<ScreenCapture> sc;
  3735. {
  3736. SCOPED_TRACE("before move");
  3737. ScreenCapture::captureScreen(&sc);
  3738. sc->expectBGColor(0, 12);
  3739. sc->expectFGColor(75, 75);
  3740. sc->expectBGColor(145, 145);
  3741. }
  3742. Transaction t1, t2;
  3743. t1.setPosition(mFGSurfaceControl, 128, 128);
  3744. t2.setPosition(mFGSurfaceControl, 0, 0);
  3745. // We expect that the position update from t2 now
  3746. // overwrites the position update from t1.
  3747. t1.merge(std::move(t2));
  3748. t1.apply();
  3749. {
  3750. ScreenCapture::captureScreen(&sc);
  3751. sc->expectFGColor(1, 1);
  3752. }
  3753. }
  3754. class ChildLayerTest : public LayerUpdateTest {
  3755. protected:
  3756. void SetUp() override {
  3757. LayerUpdateTest::SetUp();
  3758. mChild = createSurface(mClient, "Child surface", 10, 15, PIXEL_FORMAT_RGBA_8888, 0,
  3759. mFGSurfaceControl.get());
  3760. fillSurfaceRGBA8(mChild, 200, 200, 200);
  3761. {
  3762. SCOPED_TRACE("before anything");
  3763. mCapture = screenshot();
  3764. mCapture->expectChildColor(64, 64);
  3765. }
  3766. }
  3767. void TearDown() override {
  3768. LayerUpdateTest::TearDown();
  3769. mChild = 0;
  3770. }
  3771. sp<SurfaceControl> mChild;
  3772. std::unique_ptr<ScreenCapture> mCapture;
  3773. };
  3774. TEST_F(ChildLayerTest, ChildLayerPositioning) {
  3775. asTransaction([&](Transaction& t) {
  3776. t.show(mChild);
  3777. t.setPosition(mChild, 10, 10);
  3778. t.setPosition(mFGSurfaceControl, 64, 64);
  3779. });
  3780. {
  3781. mCapture = screenshot();
  3782. // Top left of foreground must now be visible
  3783. mCapture->expectFGColor(64, 64);
  3784. // But 10 pixels in we should see the child surface
  3785. mCapture->expectChildColor(74, 74);
  3786. // And 10 more pixels we should be back to the foreground surface
  3787. mCapture->expectFGColor(84, 84);
  3788. }
  3789. asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
  3790. {
  3791. mCapture = screenshot();
  3792. // Top left of foreground should now be at 0, 0
  3793. mCapture->expectFGColor(0, 0);
  3794. // But 10 pixels in we should see the child surface
  3795. mCapture->expectChildColor(10, 10);
  3796. // And 10 more pixels we should be back to the foreground surface
  3797. mCapture->expectFGColor(20, 20);
  3798. }
  3799. }
  3800. TEST_F(ChildLayerTest, ChildLayerCropping) {
  3801. asTransaction([&](Transaction& t) {
  3802. t.show(mChild);
  3803. t.setPosition(mChild, 0, 0);
  3804. t.setPosition(mFGSurfaceControl, 0, 0);
  3805. t.setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 5, 5));
  3806. });
  3807. {
  3808. mCapture = screenshot();
  3809. mCapture->expectChildColor(0, 0);
  3810. mCapture->expectChildColor(4, 4);
  3811. mCapture->expectBGColor(5, 5);
  3812. }
  3813. }
  3814. TEST_F(ChildLayerTest, ChildLayerConstraints) {
  3815. asTransaction([&](Transaction& t) {
  3816. t.show(mChild);
  3817. t.setPosition(mFGSurfaceControl, 0, 0);
  3818. t.setPosition(mChild, 63, 63);
  3819. });
  3820. {
  3821. mCapture = screenshot();
  3822. mCapture->expectFGColor(0, 0);
  3823. // Last pixel in foreground should now be the child.
  3824. mCapture->expectChildColor(63, 63);
  3825. // But the child should be constrained and the next pixel
  3826. // must be the background
  3827. mCapture->expectBGColor(64, 64);
  3828. }
  3829. }
  3830. TEST_F(ChildLayerTest, ChildLayerScaling) {
  3831. asTransaction([&](Transaction& t) { t.setPosition(mFGSurfaceControl, 0, 0); });
  3832. // Find the boundary between the parent and child
  3833. {
  3834. mCapture = screenshot();
  3835. mCapture->expectChildColor(9, 9);
  3836. mCapture->expectFGColor(10, 10);
  3837. }
  3838. asTransaction([&](Transaction& t) { t.setMatrix(mFGSurfaceControl, 2.0, 0, 0, 2.0); });
  3839. // The boundary should be twice as far from the origin now.
  3840. // The pixels from the last test should all be child now
  3841. {
  3842. mCapture = screenshot();
  3843. mCapture->expectChildColor(9, 9);
  3844. mCapture->expectChildColor(10, 10);
  3845. mCapture->expectChildColor(19, 19);
  3846. mCapture->expectFGColor(20, 20);
  3847. }
  3848. }
  3849. // A child with a scale transform should be cropped by its parent bounds.
  3850. TEST_F(ChildLayerTest, ChildLayerScalingCroppedByParent) {
  3851. asTransaction([&](Transaction& t) {
  3852. t.setPosition(mFGSurfaceControl, 0, 0);
  3853. t.setPosition(mChild, 0, 0);
  3854. });
  3855. // Find the boundary between the parent and child.
  3856. {
  3857. mCapture = screenshot();
  3858. mCapture->expectChildColor(0, 0);
  3859. mCapture->expectChildColor(9, 9);
  3860. mCapture->expectFGColor(10, 10);
  3861. }
  3862. asTransaction([&](Transaction& t) { t.setMatrix(mChild, 10.0, 0, 0, 10.0); });
  3863. // The child should fill its parent bounds and be cropped by it.
  3864. {
  3865. mCapture = screenshot();
  3866. mCapture->expectChildColor(0, 0);
  3867. mCapture->expectChildColor(63, 63);
  3868. mCapture->expectBGColor(64, 64);
  3869. }
  3870. }
  3871. TEST_F(ChildLayerTest, ChildLayerAlpha) {
  3872. fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 254);
  3873. fillSurfaceRGBA8(mFGSurfaceControl, 254, 0, 0);
  3874. fillSurfaceRGBA8(mChild, 0, 254, 0);
  3875. waitForPostedBuffers();
  3876. asTransaction([&](Transaction& t) {
  3877. t.show(mChild);
  3878. t.setPosition(mChild, 0, 0);
  3879. t.setPosition(mFGSurfaceControl, 0, 0);
  3880. });
  3881. {
  3882. mCapture = screenshot();
  3883. // Unblended child color
  3884. mCapture->checkPixel(0, 0, 0, 254, 0);
  3885. }
  3886. asTransaction([&](Transaction& t) { t.setAlpha(mChild, 0.5); });
  3887. {
  3888. mCapture = screenshot();
  3889. // Child and BG blended.
  3890. mCapture->checkPixel(0, 0, 127, 127, 0);
  3891. }
  3892. asTransaction([&](Transaction& t) { t.setAlpha(mFGSurfaceControl, 0.5); });
  3893. {
  3894. mCapture = screenshot();
  3895. // Child and BG blended.
  3896. mCapture->checkPixel(0, 0, 95, 64, 95);
  3897. }
  3898. }
  3899. TEST_F(ChildLayerTest, ReparentChildren) {
  3900. asTransaction([&](Transaction& t) {
  3901. t.show(mChild);
  3902. t.setPosition(mChild, 10, 10);
  3903. t.setPosition(mFGSurfaceControl, 64, 64);
  3904. });
  3905. {
  3906. mCapture = screenshot();
  3907. // Top left of foreground must now be visible
  3908. mCapture->expectFGColor(64, 64);
  3909. // But 10 pixels in we should see the child surface
  3910. mCapture->expectChildColor(74, 74);
  3911. // And 10 more pixels we should be back to the foreground surface
  3912. mCapture->expectFGColor(84, 84);
  3913. }
  3914. asTransaction([&](Transaction& t) {
  3915. t.reparentChildren(mFGSurfaceControl, mBGSurfaceControl->getHandle());
  3916. });
  3917. {
  3918. mCapture = screenshot();
  3919. mCapture->expectFGColor(64, 64);
  3920. // In reparenting we should have exposed the entire foreground surface.
  3921. mCapture->expectFGColor(74, 74);
  3922. // And the child layer should now begin at 10, 10 (since the BG
  3923. // layer is at (0, 0)).
  3924. mCapture->expectBGColor(9, 9);
  3925. mCapture->expectChildColor(10, 10);
  3926. }
  3927. }
  3928. TEST_F(ChildLayerTest, ChildrenSurviveParentDestruction) {
  3929. sp<SurfaceControl> mGrandChild =
  3930. createSurface(mClient, "Grand Child", 10, 10, PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
  3931. fillSurfaceRGBA8(mGrandChild, 111, 111, 111);
  3932. {
  3933. SCOPED_TRACE("Grandchild visible");
  3934. ScreenCapture::captureScreen(&mCapture);
  3935. mCapture->checkPixel(64, 64, 111, 111, 111);
  3936. }
  3937. mChild.clear();
  3938. {
  3939. SCOPED_TRACE("After destroying child");
  3940. ScreenCapture::captureScreen(&mCapture);
  3941. mCapture->expectFGColor(64, 64);
  3942. }
  3943. asTransaction([&](Transaction& t) {
  3944. t.reparent(mGrandChild, mFGSurfaceControl->getHandle());
  3945. });
  3946. {
  3947. SCOPED_TRACE("After reparenting grandchild");
  3948. ScreenCapture::captureScreen(&mCapture);
  3949. mCapture->checkPixel(64, 64, 111, 111, 111);
  3950. }
  3951. }
  3952. TEST_F(ChildLayerTest, DetachChildrenSameClient) {
  3953. asTransaction([&](Transaction& t) {
  3954. t.show(mChild);
  3955. t.setPosition(mChild, 10, 10);
  3956. t.setPosition(mFGSurfaceControl, 64, 64);
  3957. });
  3958. {
  3959. mCapture = screenshot();
  3960. // Top left of foreground must now be visible
  3961. mCapture->expectFGColor(64, 64);
  3962. // But 10 pixels in we should see the child surface
  3963. mCapture->expectChildColor(74, 74);
  3964. // And 10 more pixels we should be back to the foreground surface
  3965. mCapture->expectFGColor(84, 84);
  3966. }
  3967. asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
  3968. asTransaction([&](Transaction& t) { t.hide(mChild); });
  3969. // Since the child has the same client as the parent, it will not get
  3970. // detached and will be hidden.
  3971. {
  3972. mCapture = screenshot();
  3973. mCapture->expectFGColor(64, 64);
  3974. mCapture->expectFGColor(74, 74);
  3975. mCapture->expectFGColor(84, 84);
  3976. }
  3977. }
  3978. TEST_F(ChildLayerTest, DetachChildrenDifferentClient) {
  3979. sp<SurfaceComposerClient> mNewComposerClient = new SurfaceComposerClient;
  3980. sp<SurfaceControl> mChildNewClient =
  3981. createSurface(mNewComposerClient, "New Child Test Surface", 10, 10,
  3982. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  3983. ASSERT_TRUE(mChildNewClient->isValid());
  3984. fillSurfaceRGBA8(mChildNewClient, 200, 200, 200);
  3985. asTransaction([&](Transaction& t) {
  3986. t.hide(mChild);
  3987. t.show(mChildNewClient);
  3988. t.setPosition(mChildNewClient, 10, 10);
  3989. t.setPosition(mFGSurfaceControl, 64, 64);
  3990. });
  3991. {
  3992. mCapture = screenshot();
  3993. // Top left of foreground must now be visible
  3994. mCapture->expectFGColor(64, 64);
  3995. // But 10 pixels in we should see the child surface
  3996. mCapture->expectChildColor(74, 74);
  3997. // And 10 more pixels we should be back to the foreground surface
  3998. mCapture->expectFGColor(84, 84);
  3999. }
  4000. asTransaction([&](Transaction& t) { t.detachChildren(mFGSurfaceControl); });
  4001. asTransaction([&](Transaction& t) { t.hide(mChildNewClient); });
  4002. // Nothing should have changed.
  4003. {
  4004. mCapture = screenshot();
  4005. mCapture->expectFGColor(64, 64);
  4006. mCapture->expectChildColor(74, 74);
  4007. mCapture->expectFGColor(84, 84);
  4008. }
  4009. }
  4010. TEST_F(ChildLayerTest, DetachChildrenThenAttach) {
  4011. sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
  4012. sp<SurfaceControl> childNewClient =
  4013. newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
  4014. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4015. ASSERT_TRUE(childNewClient != nullptr);
  4016. ASSERT_TRUE(childNewClient->isValid());
  4017. fillSurfaceRGBA8(childNewClient, 200, 200, 200);
  4018. Transaction()
  4019. .hide(mChild)
  4020. .show(childNewClient)
  4021. .setPosition(childNewClient, 10, 10)
  4022. .setPosition(mFGSurfaceControl, 64, 64)
  4023. .apply();
  4024. {
  4025. mCapture = screenshot();
  4026. // Top left of foreground must now be visible
  4027. mCapture->expectFGColor(64, 64);
  4028. // But 10 pixels in we should see the child surface
  4029. mCapture->expectChildColor(74, 74);
  4030. // And 10 more pixels we should be back to the foreground surface
  4031. mCapture->expectFGColor(84, 84);
  4032. }
  4033. Transaction().detachChildren(mFGSurfaceControl).apply();
  4034. Transaction().hide(childNewClient).apply();
  4035. // Nothing should have changed.
  4036. {
  4037. mCapture = screenshot();
  4038. mCapture->expectFGColor(64, 64);
  4039. mCapture->expectChildColor(74, 74);
  4040. mCapture->expectFGColor(84, 84);
  4041. }
  4042. sp<SurfaceControl> newParentSurface = createLayer(String8("New Parent Surface"), 32, 32, 0);
  4043. fillLayerColor(ISurfaceComposerClient::eFXSurfaceBufferQueue, newParentSurface, Color::RED, 32,
  4044. 32);
  4045. Transaction()
  4046. .setLayer(newParentSurface, INT32_MAX - 1)
  4047. .show(newParentSurface)
  4048. .setPosition(newParentSurface, 20, 20)
  4049. .reparent(childNewClient, newParentSurface->getHandle())
  4050. .apply();
  4051. {
  4052. mCapture = screenshot();
  4053. // Child is now hidden.
  4054. mCapture->expectColor(Rect(20, 20, 52, 52), Color::RED);
  4055. }
  4056. }
  4057. TEST_F(ChildLayerTest, DetachChildrenWithDeferredTransaction) {
  4058. sp<SurfaceComposerClient> newComposerClient = new SurfaceComposerClient;
  4059. sp<SurfaceControl> childNewClient =
  4060. newComposerClient->createSurface(String8("New Child Test Surface"), 10, 10,
  4061. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4062. ASSERT_TRUE(childNewClient != nullptr);
  4063. ASSERT_TRUE(childNewClient->isValid());
  4064. fillSurfaceRGBA8(childNewClient, 200, 200, 200);
  4065. Transaction()
  4066. .hide(mChild)
  4067. .show(childNewClient)
  4068. .setPosition(childNewClient, 10, 10)
  4069. .setPosition(mFGSurfaceControl, 64, 64)
  4070. .apply();
  4071. {
  4072. mCapture = screenshot();
  4073. Rect rect = Rect(74, 74, 84, 84);
  4074. mCapture->expectBorder(rect, Color{195, 63, 63, 255});
  4075. mCapture->expectColor(rect, Color{200, 200, 200, 255});
  4076. }
  4077. Transaction()
  4078. .deferTransactionUntil_legacy(childNewClient, mFGSurfaceControl->getHandle(),
  4079. mFGSurfaceControl->getSurface()->getNextFrameNumber())
  4080. .apply();
  4081. Transaction().detachChildren(mFGSurfaceControl).apply();
  4082. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(mFGSurfaceControl, Color::RED, 32, 32));
  4083. // BufferLayer can still dequeue buffers even though there's a detached layer with a
  4084. // deferred transaction.
  4085. {
  4086. SCOPED_TRACE("new buffer");
  4087. mCapture = screenshot();
  4088. Rect rect = Rect(74, 74, 84, 84);
  4089. mCapture->expectBorder(rect, Color::RED);
  4090. mCapture->expectColor(rect, Color{200, 200, 200, 255});
  4091. }
  4092. }
  4093. TEST_F(ChildLayerTest, ChildrenInheritNonTransformScalingFromParent) {
  4094. asTransaction([&](Transaction& t) {
  4095. t.show(mChild);
  4096. t.setPosition(mChild, 0, 0);
  4097. t.setPosition(mFGSurfaceControl, 0, 0);
  4098. });
  4099. {
  4100. mCapture = screenshot();
  4101. // We've positioned the child in the top left.
  4102. mCapture->expectChildColor(0, 0);
  4103. // But it's only 10x15.
  4104. mCapture->expectFGColor(10, 15);
  4105. }
  4106. asTransaction([&](Transaction& t) {
  4107. t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
  4108. // We cause scaling by 2.
  4109. t.setSize(mFGSurfaceControl, 128, 128);
  4110. });
  4111. {
  4112. mCapture = screenshot();
  4113. // We've positioned the child in the top left.
  4114. mCapture->expectChildColor(0, 0);
  4115. mCapture->expectChildColor(10, 10);
  4116. mCapture->expectChildColor(19, 29);
  4117. // And now it should be scaled all the way to 20x30
  4118. mCapture->expectFGColor(20, 30);
  4119. }
  4120. }
  4121. // Regression test for b/37673612
  4122. TEST_F(ChildLayerTest, ChildrenWithParentBufferTransform) {
  4123. asTransaction([&](Transaction& t) {
  4124. t.show(mChild);
  4125. t.setPosition(mChild, 0, 0);
  4126. t.setPosition(mFGSurfaceControl, 0, 0);
  4127. });
  4128. {
  4129. mCapture = screenshot();
  4130. // We've positioned the child in the top left.
  4131. mCapture->expectChildColor(0, 0);
  4132. mCapture->expectChildColor(9, 14);
  4133. // But it's only 10x15.
  4134. mCapture->expectFGColor(10, 15);
  4135. }
  4136. // We set things up as in b/37673612 so that there is a mismatch between the buffer size and
  4137. // the WM specified state size.
  4138. asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
  4139. sp<Surface> s = mFGSurfaceControl->getSurface();
  4140. auto anw = static_cast<ANativeWindow*>(s.get());
  4141. native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
  4142. native_window_set_buffers_dimensions(anw, 64, 128);
  4143. fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
  4144. waitForPostedBuffers();
  4145. {
  4146. // The child should still be in the same place and not have any strange scaling as in
  4147. // b/37673612.
  4148. mCapture = screenshot();
  4149. mCapture->expectChildColor(0, 0);
  4150. mCapture->expectFGColor(10, 10);
  4151. }
  4152. }
  4153. // A child with a buffer transform from its parents should be cropped by its parent bounds.
  4154. TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferTransform) {
  4155. asTransaction([&](Transaction& t) {
  4156. t.show(mChild);
  4157. t.setPosition(mChild, 0, 0);
  4158. t.setPosition(mFGSurfaceControl, 0, 0);
  4159. t.setSize(mChild, 100, 100);
  4160. });
  4161. fillSurfaceRGBA8(mChild, 200, 200, 200);
  4162. {
  4163. mCapture = screenshot();
  4164. mCapture->expectChildColor(0, 0);
  4165. mCapture->expectChildColor(63, 63);
  4166. mCapture->expectBGColor(64, 64);
  4167. }
  4168. asTransaction([&](Transaction& t) { t.setSize(mFGSurfaceControl, 128, 64); });
  4169. sp<Surface> s = mFGSurfaceControl->getSurface();
  4170. auto anw = static_cast<ANativeWindow*>(s.get());
  4171. // Apply a 90 transform on the buffer.
  4172. native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
  4173. native_window_set_buffers_dimensions(anw, 64, 128);
  4174. fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
  4175. waitForPostedBuffers();
  4176. // The child should be cropped by the new parent bounds.
  4177. {
  4178. mCapture = screenshot();
  4179. mCapture->expectChildColor(0, 0);
  4180. mCapture->expectChildColor(99, 63);
  4181. mCapture->expectFGColor(100, 63);
  4182. mCapture->expectBGColor(128, 64);
  4183. }
  4184. }
  4185. // A child with a scale transform from its parents should be cropped by its parent bounds.
  4186. TEST_F(ChildLayerTest, ChildCroppedByParentWithBufferScale) {
  4187. asTransaction([&](Transaction& t) {
  4188. t.show(mChild);
  4189. t.setPosition(mChild, 0, 0);
  4190. t.setPosition(mFGSurfaceControl, 0, 0);
  4191. t.setSize(mChild, 200, 200);
  4192. });
  4193. fillSurfaceRGBA8(mChild, 200, 200, 200);
  4194. {
  4195. mCapture = screenshot();
  4196. mCapture->expectChildColor(0, 0);
  4197. mCapture->expectChildColor(63, 63);
  4198. mCapture->expectBGColor(64, 64);
  4199. }
  4200. asTransaction([&](Transaction& t) {
  4201. t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
  4202. // Set a scaling by 2.
  4203. t.setSize(mFGSurfaceControl, 128, 128);
  4204. });
  4205. // Child should inherit its parents scale but should be cropped by its parent bounds.
  4206. {
  4207. mCapture = screenshot();
  4208. mCapture->expectChildColor(0, 0);
  4209. mCapture->expectChildColor(127, 127);
  4210. mCapture->expectBGColor(128, 128);
  4211. }
  4212. }
  4213. // Regression test for b/127368943
  4214. // Child should ignore the buffer transform but apply parent scale transform.
  4215. TEST_F(ChildLayerTest, ChildrenWithParentBufferTransformAndScale) {
  4216. asTransaction([&](Transaction& t) {
  4217. t.show(mChild);
  4218. t.setPosition(mChild, 0, 0);
  4219. t.setPosition(mFGSurfaceControl, 0, 0);
  4220. });
  4221. {
  4222. mCapture = screenshot();
  4223. mCapture->expectChildColor(0, 0);
  4224. mCapture->expectChildColor(9, 14);
  4225. mCapture->expectFGColor(10, 15);
  4226. }
  4227. // Change the size of the foreground to 128 * 64 so we can test rotation as well.
  4228. asTransaction([&](Transaction& t) {
  4229. t.setOverrideScalingMode(mFGSurfaceControl, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
  4230. t.setSize(mFGSurfaceControl, 128, 64);
  4231. });
  4232. sp<Surface> s = mFGSurfaceControl->getSurface();
  4233. auto anw = static_cast<ANativeWindow*>(s.get());
  4234. // Apply a 90 transform on the buffer and submit a buffer half the expected size so that we
  4235. // have an effective scale of 2.0 applied to the buffer along with a rotation transform.
  4236. native_window_set_buffers_transform(anw, NATIVE_WINDOW_TRANSFORM_ROT_90);
  4237. native_window_set_buffers_dimensions(anw, 32, 64);
  4238. fillSurfaceRGBA8(mFGSurfaceControl, 195, 63, 63);
  4239. waitForPostedBuffers();
  4240. // The child should ignore the buffer transform but apply the 2.0 scale from parent.
  4241. {
  4242. mCapture = screenshot();
  4243. mCapture->expectChildColor(0, 0);
  4244. mCapture->expectChildColor(19, 29);
  4245. mCapture->expectFGColor(20, 30);
  4246. }
  4247. }
  4248. TEST_F(ChildLayerTest, Bug36858924) {
  4249. // Destroy the child layer
  4250. mChild.clear();
  4251. // Now recreate it as hidden
  4252. mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888,
  4253. ISurfaceComposerClient::eHidden, mFGSurfaceControl.get());
  4254. // Show the child layer in a deferred transaction
  4255. asTransaction([&](Transaction& t) {
  4256. t.deferTransactionUntil_legacy(mChild, mFGSurfaceControl->getHandle(),
  4257. mFGSurfaceControl->getSurface()->getNextFrameNumber());
  4258. t.show(mChild);
  4259. });
  4260. // Render the foreground surface a few times
  4261. //
  4262. // Prior to the bugfix for b/36858924, this would usually hang while trying to fill the third
  4263. // frame because SurfaceFlinger would never process the deferred transaction and would therefore
  4264. // never acquire/release the first buffer
  4265. ALOGI("Filling 1");
  4266. fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
  4267. ALOGI("Filling 2");
  4268. fillSurfaceRGBA8(mFGSurfaceControl, 0, 0, 255);
  4269. ALOGI("Filling 3");
  4270. fillSurfaceRGBA8(mFGSurfaceControl, 255, 0, 0);
  4271. ALOGI("Filling 4");
  4272. fillSurfaceRGBA8(mFGSurfaceControl, 0, 255, 0);
  4273. }
  4274. TEST_F(ChildLayerTest, Reparent) {
  4275. asTransaction([&](Transaction& t) {
  4276. t.show(mChild);
  4277. t.setPosition(mChild, 10, 10);
  4278. t.setPosition(mFGSurfaceControl, 64, 64);
  4279. });
  4280. {
  4281. mCapture = screenshot();
  4282. // Top left of foreground must now be visible
  4283. mCapture->expectFGColor(64, 64);
  4284. // But 10 pixels in we should see the child surface
  4285. mCapture->expectChildColor(74, 74);
  4286. // And 10 more pixels we should be back to the foreground surface
  4287. mCapture->expectFGColor(84, 84);
  4288. }
  4289. asTransaction([&](Transaction& t) { t.reparent(mChild, mBGSurfaceControl->getHandle()); });
  4290. {
  4291. mCapture = screenshot();
  4292. mCapture->expectFGColor(64, 64);
  4293. // In reparenting we should have exposed the entire foreground surface.
  4294. mCapture->expectFGColor(74, 74);
  4295. // And the child layer should now begin at 10, 10 (since the BG
  4296. // layer is at (0, 0)).
  4297. mCapture->expectBGColor(9, 9);
  4298. mCapture->expectChildColor(10, 10);
  4299. }
  4300. }
  4301. TEST_F(ChildLayerTest, ReparentToNoParent) {
  4302. asTransaction([&](Transaction& t) {
  4303. t.show(mChild);
  4304. t.setPosition(mChild, 10, 10);
  4305. t.setPosition(mFGSurfaceControl, 64, 64);
  4306. });
  4307. {
  4308. mCapture = screenshot();
  4309. // Top left of foreground must now be visible
  4310. mCapture->expectFGColor(64, 64);
  4311. // But 10 pixels in we should see the child surface
  4312. mCapture->expectChildColor(74, 74);
  4313. // And 10 more pixels we should be back to the foreground surface
  4314. mCapture->expectFGColor(84, 84);
  4315. }
  4316. asTransaction([&](Transaction& t) { t.reparent(mChild, nullptr); });
  4317. {
  4318. mCapture = screenshot();
  4319. // The surface should now be offscreen.
  4320. mCapture->expectFGColor(64, 64);
  4321. mCapture->expectFGColor(74, 74);
  4322. mCapture->expectFGColor(84, 84);
  4323. }
  4324. }
  4325. TEST_F(ChildLayerTest, ReparentFromNoParent) {
  4326. sp<SurfaceControl> newSurface = createLayer(String8("New Surface"), 10, 10, 0);
  4327. ASSERT_TRUE(newSurface != nullptr);
  4328. ASSERT_TRUE(newSurface->isValid());
  4329. fillSurfaceRGBA8(newSurface, 63, 195, 63);
  4330. asTransaction([&](Transaction& t) {
  4331. t.hide(mChild);
  4332. t.show(newSurface);
  4333. t.setPosition(newSurface, 10, 10);
  4334. t.setLayer(newSurface, INT32_MAX - 2);
  4335. t.setPosition(mFGSurfaceControl, 64, 64);
  4336. });
  4337. {
  4338. mCapture = screenshot();
  4339. // Top left of foreground must now be visible
  4340. mCapture->expectFGColor(64, 64);
  4341. // At 10, 10 we should see the new surface
  4342. mCapture->checkPixel(10, 10, 63, 195, 63);
  4343. }
  4344. asTransaction([&](Transaction& t) { t.reparent(newSurface, mFGSurfaceControl->getHandle()); });
  4345. {
  4346. mCapture = screenshot();
  4347. // newSurface will now be a child of mFGSurface so it will be 10, 10 offset from
  4348. // mFGSurface, putting it at 74, 74.
  4349. mCapture->expectFGColor(64, 64);
  4350. mCapture->checkPixel(74, 74, 63, 195, 63);
  4351. mCapture->expectFGColor(84, 84);
  4352. }
  4353. }
  4354. TEST_F(ChildLayerTest, NestedChildren) {
  4355. sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 10, 10,
  4356. PIXEL_FORMAT_RGBA_8888, 0, mChild.get());
  4357. fillSurfaceRGBA8(grandchild, 50, 50, 50);
  4358. {
  4359. mCapture = screenshot();
  4360. // Expect the grandchild to begin at 64, 64 because it's a child of mChild layer
  4361. // which begins at 64, 64
  4362. mCapture->checkPixel(64, 64, 50, 50, 50);
  4363. }
  4364. }
  4365. TEST_F(ChildLayerTest, ChildLayerRelativeLayer) {
  4366. sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 128, 128, 0);
  4367. fillSurfaceRGBA8(relative, 255, 255, 255);
  4368. Transaction t;
  4369. t.setLayer(relative, INT32_MAX)
  4370. .setRelativeLayer(mChild, relative->getHandle(), 1)
  4371. .setPosition(mFGSurfaceControl, 0, 0)
  4372. .apply(true);
  4373. // We expect that the child should have been elevated above our
  4374. // INT_MAX layer even though it's not a child of it.
  4375. {
  4376. mCapture = screenshot();
  4377. mCapture->expectChildColor(0, 0);
  4378. mCapture->expectChildColor(9, 9);
  4379. mCapture->checkPixel(10, 10, 255, 255, 255);
  4380. }
  4381. }
  4382. class BoundlessLayerTest : public LayerUpdateTest {
  4383. protected:
  4384. std::unique_ptr<ScreenCapture> mCapture;
  4385. };
  4386. // Verify setting a size on a buffer layer has no effect.
  4387. TEST_F(BoundlessLayerTest, BufferLayerIgnoresSize) {
  4388. sp<SurfaceControl> bufferLayer =
  4389. createSurface(mClient, "BufferLayer", 45, 45, PIXEL_FORMAT_RGBA_8888, 0,
  4390. mFGSurfaceControl.get());
  4391. ASSERT_TRUE(bufferLayer->isValid());
  4392. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(bufferLayer, Color::BLACK, 30, 30));
  4393. asTransaction([&](Transaction& t) { t.show(bufferLayer); });
  4394. {
  4395. mCapture = screenshot();
  4396. // Top left of background must now be visible
  4397. mCapture->expectBGColor(0, 0);
  4398. // Foreground Surface bounds must be color layer
  4399. mCapture->expectColor(Rect(64, 64, 94, 94), Color::BLACK);
  4400. // Buffer layer should not extend past buffer bounds
  4401. mCapture->expectFGColor(95, 95);
  4402. }
  4403. }
  4404. // Verify a boundless color layer will fill its parent bounds. The parent has a buffer size
  4405. // which will crop the color layer.
  4406. TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentBufferBounds) {
  4407. sp<SurfaceControl> colorLayer =
  4408. createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
  4409. ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
  4410. ASSERT_TRUE(colorLayer->isValid());
  4411. asTransaction([&](Transaction& t) {
  4412. t.setColor(colorLayer, half3{0, 0, 0});
  4413. t.show(colorLayer);
  4414. });
  4415. {
  4416. mCapture = screenshot();
  4417. // Top left of background must now be visible
  4418. mCapture->expectBGColor(0, 0);
  4419. // Foreground Surface bounds must be color layer
  4420. mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
  4421. // Color layer should not extend past foreground bounds
  4422. mCapture->expectBGColor(129, 129);
  4423. }
  4424. }
  4425. // Verify a boundless color layer will fill its parent bounds. The parent has no buffer but has
  4426. // a crop which will be used to crop the color layer.
  4427. TEST_F(BoundlessLayerTest, BoundlessColorLayerFillsParentCropBounds) {
  4428. sp<SurfaceControl> cropLayer = createSurface(mClient, "CropLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
  4429. 0 /* flags */, mFGSurfaceControl.get());
  4430. ASSERT_TRUE(cropLayer->isValid());
  4431. sp<SurfaceControl> colorLayer =
  4432. createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
  4433. ISurfaceComposerClient::eFXSurfaceColor, cropLayer.get());
  4434. ASSERT_TRUE(colorLayer->isValid());
  4435. asTransaction([&](Transaction& t) {
  4436. t.setCrop_legacy(cropLayer, Rect(5, 5, 10, 10));
  4437. t.setColor(colorLayer, half3{0, 0, 0});
  4438. t.show(cropLayer);
  4439. t.show(colorLayer);
  4440. });
  4441. {
  4442. mCapture = screenshot();
  4443. // Top left of background must now be visible
  4444. mCapture->expectBGColor(0, 0);
  4445. // Top left of foreground must now be visible
  4446. mCapture->expectFGColor(64, 64);
  4447. // 5 pixels from the foreground we should see the child surface
  4448. mCapture->expectColor(Rect(69, 69, 74, 74), Color::BLACK);
  4449. // 10 pixels from the foreground we should be back to the foreground surface
  4450. mCapture->expectFGColor(74, 74);
  4451. }
  4452. }
  4453. // Verify for boundless layer with no children, their transforms have no effect.
  4454. TEST_F(BoundlessLayerTest, BoundlessColorLayerTransformHasNoEffect) {
  4455. sp<SurfaceControl> colorLayer =
  4456. createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
  4457. ISurfaceComposerClient::eFXSurfaceColor, mFGSurfaceControl.get());
  4458. ASSERT_TRUE(colorLayer->isValid());
  4459. asTransaction([&](Transaction& t) {
  4460. t.setPosition(colorLayer, 320, 320);
  4461. t.setMatrix(colorLayer, 2, 0, 0, 2);
  4462. t.setColor(colorLayer, half3{0, 0, 0});
  4463. t.show(colorLayer);
  4464. });
  4465. {
  4466. mCapture = screenshot();
  4467. // Top left of background must now be visible
  4468. mCapture->expectBGColor(0, 0);
  4469. // Foreground Surface bounds must be color layer
  4470. mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
  4471. // Color layer should not extend past foreground bounds
  4472. mCapture->expectBGColor(129, 129);
  4473. }
  4474. }
  4475. // Verify for boundless layer with children, their transforms have an effect.
  4476. TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerCanSetTransform) {
  4477. sp<SurfaceControl> boundlessLayerRightShift =
  4478. createSurface(mClient, "BoundlessLayerRightShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
  4479. 0 /* flags */, mFGSurfaceControl.get());
  4480. ASSERT_TRUE(boundlessLayerRightShift->isValid());
  4481. sp<SurfaceControl> boundlessLayerDownShift =
  4482. createSurface(mClient, "BoundlessLayerLeftShift", 0, 0, PIXEL_FORMAT_RGBA_8888,
  4483. 0 /* flags */, boundlessLayerRightShift.get());
  4484. ASSERT_TRUE(boundlessLayerDownShift->isValid());
  4485. sp<SurfaceControl> colorLayer =
  4486. createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
  4487. ISurfaceComposerClient::eFXSurfaceColor, boundlessLayerDownShift.get());
  4488. ASSERT_TRUE(colorLayer->isValid());
  4489. asTransaction([&](Transaction& t) {
  4490. t.setPosition(boundlessLayerRightShift, 32, 0);
  4491. t.show(boundlessLayerRightShift);
  4492. t.setPosition(boundlessLayerDownShift, 0, 32);
  4493. t.show(boundlessLayerDownShift);
  4494. t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
  4495. t.setColor(colorLayer, half3{0, 0, 0});
  4496. t.show(colorLayer);
  4497. });
  4498. {
  4499. mCapture = screenshot();
  4500. // Top left of background must now be visible
  4501. mCapture->expectBGColor(0, 0);
  4502. // Top left of foreground must now be visible
  4503. mCapture->expectFGColor(64, 64);
  4504. // Foreground Surface bounds must be color layer
  4505. mCapture->expectColor(Rect(96, 96, 128, 128), Color::BLACK);
  4506. // Color layer should not extend past foreground bounds
  4507. mCapture->expectBGColor(129, 129);
  4508. }
  4509. }
  4510. // Verify child layers do not get clipped if they temporarily move into the negative
  4511. // coordinate space as the result of an intermediate transformation.
  4512. TEST_F(BoundlessLayerTest, IntermediateBoundlessLayerDoNotCrop) {
  4513. sp<SurfaceControl> boundlessLayer =
  4514. mClient->createSurface(String8("BoundlessLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
  4515. 0 /* flags */, mFGSurfaceControl.get());
  4516. ASSERT_TRUE(boundlessLayer != nullptr);
  4517. ASSERT_TRUE(boundlessLayer->isValid());
  4518. sp<SurfaceControl> colorLayer =
  4519. mClient->createSurface(String8("ColorLayer"), 0, 0, PIXEL_FORMAT_RGBA_8888,
  4520. ISurfaceComposerClient::eFXSurfaceColor, boundlessLayer.get());
  4521. ASSERT_TRUE(colorLayer != nullptr);
  4522. ASSERT_TRUE(colorLayer->isValid());
  4523. asTransaction([&](Transaction& t) {
  4524. // shift child layer off bounds. If this layer was not boundless, we will
  4525. // expect the child layer to be cropped.
  4526. t.setPosition(boundlessLayer, 32, 32);
  4527. t.show(boundlessLayer);
  4528. t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
  4529. // undo shift by parent
  4530. t.setPosition(colorLayer, -32, -32);
  4531. t.setColor(colorLayer, half3{0, 0, 0});
  4532. t.show(colorLayer);
  4533. });
  4534. {
  4535. mCapture = screenshot();
  4536. // Top left of background must now be visible
  4537. mCapture->expectBGColor(0, 0);
  4538. // Foreground Surface bounds must be color layer
  4539. mCapture->expectColor(Rect(64, 64, 128, 128), Color::BLACK);
  4540. // Color layer should not extend past foreground bounds
  4541. mCapture->expectBGColor(129, 129);
  4542. }
  4543. }
  4544. // Verify for boundless root layers with children, their transforms have an effect.
  4545. TEST_F(BoundlessLayerTest, RootBoundlessLayerCanSetTransform) {
  4546. sp<SurfaceControl> rootBoundlessLayer = createSurface(mClient, "RootBoundlessLayer", 0, 0,
  4547. PIXEL_FORMAT_RGBA_8888, 0 /* flags */);
  4548. ASSERT_TRUE(rootBoundlessLayer->isValid());
  4549. sp<SurfaceControl> colorLayer =
  4550. createSurface(mClient, "ColorLayer", 0, 0, PIXEL_FORMAT_RGBA_8888,
  4551. ISurfaceComposerClient::eFXSurfaceColor, rootBoundlessLayer.get());
  4552. ASSERT_TRUE(colorLayer->isValid());
  4553. asTransaction([&](Transaction& t) {
  4554. t.setLayer(rootBoundlessLayer, INT32_MAX - 1);
  4555. t.setPosition(rootBoundlessLayer, 32, 32);
  4556. t.show(rootBoundlessLayer);
  4557. t.setCrop_legacy(colorLayer, Rect(0, 0, 64, 64));
  4558. t.setColor(colorLayer, half3{0, 0, 0});
  4559. t.show(colorLayer);
  4560. t.hide(mFGSurfaceControl);
  4561. });
  4562. {
  4563. mCapture = screenshot();
  4564. // Top left of background must now be visible
  4565. mCapture->expectBGColor(0, 0);
  4566. // Top left of foreground must now be visible
  4567. mCapture->expectBGColor(31, 31);
  4568. // Foreground Surface bounds must be color layer
  4569. mCapture->expectColor(Rect(32, 32, 96, 96), Color::BLACK);
  4570. // Color layer should not extend past foreground bounds
  4571. mCapture->expectBGColor(97, 97);
  4572. }
  4573. }
  4574. class ScreenCaptureTest : public LayerUpdateTest {
  4575. protected:
  4576. std::unique_ptr<ScreenCapture> mCapture;
  4577. };
  4578. TEST_F(ScreenCaptureTest, CaptureSingleLayer) {
  4579. auto bgHandle = mBGSurfaceControl->getHandle();
  4580. ScreenCapture::captureLayers(&mCapture, bgHandle);
  4581. mCapture->expectBGColor(0, 0);
  4582. // Doesn't capture FG layer which is at 64, 64
  4583. mCapture->expectBGColor(64, 64);
  4584. }
  4585. TEST_F(ScreenCaptureTest, CaptureLayerWithChild) {
  4586. auto fgHandle = mFGSurfaceControl->getHandle();
  4587. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4588. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4589. fillSurfaceRGBA8(child, 200, 200, 200);
  4590. SurfaceComposerClient::Transaction().show(child).apply(true);
  4591. // Captures mFGSurfaceControl layer and its child.
  4592. ScreenCapture::captureLayers(&mCapture, fgHandle);
  4593. mCapture->expectFGColor(10, 10);
  4594. mCapture->expectChildColor(0, 0);
  4595. }
  4596. TEST_F(ScreenCaptureTest, CaptureLayerChildOnly) {
  4597. auto fgHandle = mFGSurfaceControl->getHandle();
  4598. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4599. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4600. fillSurfaceRGBA8(child, 200, 200, 200);
  4601. SurfaceComposerClient::Transaction().show(child).apply(true);
  4602. // Captures mFGSurfaceControl's child
  4603. ScreenCapture::captureChildLayers(&mCapture, fgHandle);
  4604. mCapture->checkPixel(10, 10, 0, 0, 0);
  4605. mCapture->expectChildColor(0, 0);
  4606. }
  4607. TEST_F(ScreenCaptureTest, CaptureLayerExclude) {
  4608. auto fgHandle = mFGSurfaceControl->getHandle();
  4609. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4610. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4611. fillSurfaceRGBA8(child, 200, 200, 200);
  4612. sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
  4613. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4614. fillSurfaceRGBA8(child2, 200, 0, 200);
  4615. SurfaceComposerClient::Transaction()
  4616. .show(child)
  4617. .show(child2)
  4618. .setLayer(child, 1)
  4619. .setLayer(child2, 2)
  4620. .apply(true);
  4621. // Child2 would be visible but its excluded, so we should see child1 color instead.
  4622. ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
  4623. mCapture->checkPixel(10, 10, 0, 0, 0);
  4624. mCapture->checkPixel(0, 0, 200, 200, 200);
  4625. }
  4626. // Like the last test but verifies that children are also exclude.
  4627. TEST_F(ScreenCaptureTest, CaptureLayerExcludeTree) {
  4628. auto fgHandle = mFGSurfaceControl->getHandle();
  4629. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4630. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4631. fillSurfaceRGBA8(child, 200, 200, 200);
  4632. sp<SurfaceControl> child2 = createSurface(mClient, "Child surface", 10, 10,
  4633. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4634. fillSurfaceRGBA8(child2, 200, 0, 200);
  4635. sp<SurfaceControl> child3 = createSurface(mClient, "Child surface", 10, 10,
  4636. PIXEL_FORMAT_RGBA_8888, 0, child2.get());
  4637. fillSurfaceRGBA8(child2, 200, 0, 200);
  4638. SurfaceComposerClient::Transaction()
  4639. .show(child)
  4640. .show(child2)
  4641. .show(child3)
  4642. .setLayer(child, 1)
  4643. .setLayer(child2, 2)
  4644. .apply(true);
  4645. // Child2 would be visible but its excluded, so we should see child1 color instead.
  4646. ScreenCapture::captureChildLayersExcluding(&mCapture, fgHandle, {child2->getHandle()});
  4647. mCapture->checkPixel(10, 10, 0, 0, 0);
  4648. mCapture->checkPixel(0, 0, 200, 200, 200);
  4649. }
  4650. TEST_F(ScreenCaptureTest, CaptureTransparent) {
  4651. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4652. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4653. fillSurfaceRGBA8(child, 200, 200, 200);
  4654. SurfaceComposerClient::Transaction().show(child).apply(true);
  4655. auto childHandle = child->getHandle();
  4656. // Captures child
  4657. ScreenCapture::captureLayers(&mCapture, childHandle, {0, 0, 10, 20});
  4658. mCapture->expectColor(Rect(0, 0, 9, 9), {200, 200, 200, 255});
  4659. // Area outside of child's bounds is transparent.
  4660. mCapture->expectColor(Rect(0, 10, 9, 19), {0, 0, 0, 0});
  4661. }
  4662. TEST_F(ScreenCaptureTest, DontCaptureRelativeOutsideTree) {
  4663. auto fgHandle = mFGSurfaceControl->getHandle();
  4664. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4665. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4666. ASSERT_NE(nullptr, child.get()) << "failed to create surface";
  4667. sp<SurfaceControl> relative = createLayer(String8("Relative surface"), 10, 10, 0);
  4668. fillSurfaceRGBA8(child, 200, 200, 200);
  4669. fillSurfaceRGBA8(relative, 100, 100, 100);
  4670. SurfaceComposerClient::Transaction()
  4671. .show(child)
  4672. // Set relative layer above fg layer so should be shown above when computing all layers.
  4673. .setRelativeLayer(relative, fgHandle, 1)
  4674. .show(relative)
  4675. .apply(true);
  4676. // Captures mFGSurfaceControl layer and its child. Relative layer shouldn't be captured.
  4677. ScreenCapture::captureLayers(&mCapture, fgHandle);
  4678. mCapture->expectFGColor(10, 10);
  4679. mCapture->expectChildColor(0, 0);
  4680. }
  4681. TEST_F(ScreenCaptureTest, CaptureRelativeInTree) {
  4682. auto fgHandle = mFGSurfaceControl->getHandle();
  4683. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4684. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4685. sp<SurfaceControl> relative = createSurface(mClient, "Relative surface", 10, 10,
  4686. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4687. fillSurfaceRGBA8(child, 200, 200, 200);
  4688. fillSurfaceRGBA8(relative, 100, 100, 100);
  4689. SurfaceComposerClient::Transaction()
  4690. .show(child)
  4691. // Set relative layer below fg layer but relative to child layer so it should be shown
  4692. // above child layer.
  4693. .setLayer(relative, -1)
  4694. .setRelativeLayer(relative, child->getHandle(), 1)
  4695. .show(relative)
  4696. .apply(true);
  4697. // Captures mFGSurfaceControl layer and its children. Relative layer is a child of fg so its
  4698. // relative value should be taken into account, placing it above child layer.
  4699. ScreenCapture::captureLayers(&mCapture, fgHandle);
  4700. mCapture->expectFGColor(10, 10);
  4701. // Relative layer is showing on top of child layer
  4702. mCapture->expectColor(Rect(0, 0, 9, 9), {100, 100, 100, 255});
  4703. }
  4704. // In the following tests we verify successful skipping of a parent layer,
  4705. // so we use the same verification logic and only change how we mutate
  4706. // the parent layer to verify that various properties are ignored.
  4707. class ScreenCaptureChildOnlyTest : public LayerUpdateTest {
  4708. public:
  4709. void SetUp() override {
  4710. LayerUpdateTest::SetUp();
  4711. mChild = createSurface(mClient, "Child surface", 10, 10, PIXEL_FORMAT_RGBA_8888, 0,
  4712. mFGSurfaceControl.get());
  4713. fillSurfaceRGBA8(mChild, 200, 200, 200);
  4714. SurfaceComposerClient::Transaction().show(mChild).apply(true);
  4715. }
  4716. void verify(std::function<void()> verifyStartingState) {
  4717. // Verify starting state before a screenshot is taken.
  4718. verifyStartingState();
  4719. // Verify child layer does not inherit any of the properties of its
  4720. // parent when its screenshot is captured.
  4721. auto fgHandle = mFGSurfaceControl->getHandle();
  4722. ScreenCapture::captureChildLayers(&mCapture, fgHandle);
  4723. mCapture->checkPixel(10, 10, 0, 0, 0);
  4724. mCapture->expectChildColor(0, 0);
  4725. // Verify all assumptions are still true after the screenshot is taken.
  4726. verifyStartingState();
  4727. }
  4728. std::unique_ptr<ScreenCapture> mCapture;
  4729. sp<SurfaceControl> mChild;
  4730. };
  4731. // Regression test b/76099859
  4732. TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentVisibility) {
  4733. SurfaceComposerClient::Transaction().hide(mFGSurfaceControl).apply(true);
  4734. // Even though the parent is hidden we should still capture the child.
  4735. // Before and after reparenting, verify child is properly hidden
  4736. // when rendering full-screen.
  4737. verify([&] { screenshot()->expectBGColor(64, 64); });
  4738. }
  4739. TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresParentCrop) {
  4740. SurfaceComposerClient::Transaction()
  4741. .setCrop_legacy(mFGSurfaceControl, Rect(0, 0, 1, 1))
  4742. .apply(true);
  4743. // Even though the parent is cropped out we should still capture the child.
  4744. // Before and after reparenting, verify child is cropped by parent.
  4745. verify([&] { screenshot()->expectBGColor(65, 65); });
  4746. }
  4747. // Regression test b/124372894
  4748. TEST_F(ScreenCaptureChildOnlyTest, CaptureLayerIgnoresTransform) {
  4749. SurfaceComposerClient::Transaction().setMatrix(mFGSurfaceControl, 2, 0, 0, 2).apply(true);
  4750. // We should not inherit the parent scaling.
  4751. // Before and after reparenting, verify child is properly scaled.
  4752. verify([&] { screenshot()->expectChildColor(80, 80); });
  4753. }
  4754. TEST_F(ScreenCaptureTest, CaptureLayerWithGrandchild) {
  4755. auto fgHandle = mFGSurfaceControl->getHandle();
  4756. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4757. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4758. fillSurfaceRGBA8(child, 200, 200, 200);
  4759. sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
  4760. PIXEL_FORMAT_RGBA_8888, 0, child.get());
  4761. fillSurfaceRGBA8(grandchild, 50, 50, 50);
  4762. SurfaceComposerClient::Transaction()
  4763. .show(child)
  4764. .setPosition(grandchild, 5, 5)
  4765. .show(grandchild)
  4766. .apply(true);
  4767. // Captures mFGSurfaceControl, its child, and the grandchild.
  4768. ScreenCapture::captureLayers(&mCapture, fgHandle);
  4769. mCapture->expectFGColor(10, 10);
  4770. mCapture->expectChildColor(0, 0);
  4771. mCapture->checkPixel(5, 5, 50, 50, 50);
  4772. }
  4773. TEST_F(ScreenCaptureTest, CaptureChildOnly) {
  4774. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4775. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4776. fillSurfaceRGBA8(child, 200, 200, 200);
  4777. auto childHandle = child->getHandle();
  4778. SurfaceComposerClient::Transaction().setPosition(child, 5, 5).show(child).apply(true);
  4779. // Captures only the child layer, and not the parent.
  4780. ScreenCapture::captureLayers(&mCapture, childHandle);
  4781. mCapture->expectChildColor(0, 0);
  4782. mCapture->expectChildColor(9, 9);
  4783. }
  4784. TEST_F(ScreenCaptureTest, CaptureGrandchildOnly) {
  4785. sp<SurfaceControl> child = createSurface(mClient, "Child surface", 10, 10,
  4786. PIXEL_FORMAT_RGBA_8888, 0, mFGSurfaceControl.get());
  4787. fillSurfaceRGBA8(child, 200, 200, 200);
  4788. auto childHandle = child->getHandle();
  4789. sp<SurfaceControl> grandchild = createSurface(mClient, "Grandchild surface", 5, 5,
  4790. PIXEL_FORMAT_RGBA_8888, 0, child.get());
  4791. fillSurfaceRGBA8(grandchild, 50, 50, 50);
  4792. SurfaceComposerClient::Transaction()
  4793. .show(child)
  4794. .setPosition(grandchild, 5, 5)
  4795. .show(grandchild)
  4796. .apply(true);
  4797. auto grandchildHandle = grandchild->getHandle();
  4798. // Captures only the grandchild.
  4799. ScreenCapture::captureLayers(&mCapture, grandchildHandle);
  4800. mCapture->checkPixel(0, 0, 50, 50, 50);
  4801. mCapture->checkPixel(4, 4, 50, 50, 50);
  4802. }
  4803. TEST_F(ScreenCaptureTest, CaptureCrop) {
  4804. sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
  4805. sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
  4806. PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
  4807. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
  4808. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
  4809. SurfaceComposerClient::Transaction()
  4810. .setLayer(redLayer, INT32_MAX - 1)
  4811. .show(redLayer)
  4812. .show(blueLayer)
  4813. .apply(true);
  4814. auto redLayerHandle = redLayer->getHandle();
  4815. // Capturing full screen should have both red and blue are visible.
  4816. ScreenCapture::captureLayers(&mCapture, redLayerHandle);
  4817. mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
  4818. // red area below the blue area
  4819. mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
  4820. // red area to the right of the blue area
  4821. mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
  4822. const Rect crop = Rect(0, 0, 30, 30);
  4823. ScreenCapture::captureLayers(&mCapture, redLayerHandle, crop);
  4824. // Capturing the cropped screen, cropping out the shown red area, should leave only the blue
  4825. // area visible.
  4826. mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
  4827. mCapture->checkPixel(30, 30, 0, 0, 0);
  4828. }
  4829. TEST_F(ScreenCaptureTest, CaptureSize) {
  4830. sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
  4831. sp<SurfaceControl> blueLayer = createSurface(mClient, "Blue surface", 30, 30,
  4832. PIXEL_FORMAT_RGBA_8888, 0, redLayer.get());
  4833. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
  4834. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(blueLayer, Color::BLUE, 30, 30));
  4835. SurfaceComposerClient::Transaction()
  4836. .setLayer(redLayer, INT32_MAX - 1)
  4837. .show(redLayer)
  4838. .show(blueLayer)
  4839. .apply(true);
  4840. auto redLayerHandle = redLayer->getHandle();
  4841. // Capturing full screen should have both red and blue are visible.
  4842. ScreenCapture::captureLayers(&mCapture, redLayerHandle);
  4843. mCapture->expectColor(Rect(0, 0, 29, 29), Color::BLUE);
  4844. // red area below the blue area
  4845. mCapture->expectColor(Rect(0, 30, 59, 59), Color::RED);
  4846. // red area to the right of the blue area
  4847. mCapture->expectColor(Rect(30, 0, 59, 59), Color::RED);
  4848. ScreenCapture::captureLayers(&mCapture, redLayerHandle, Rect::EMPTY_RECT, 0.5);
  4849. // Capturing the downsized area (30x30) should leave both red and blue but in a smaller area.
  4850. mCapture->expectColor(Rect(0, 0, 14, 14), Color::BLUE);
  4851. // red area below the blue area
  4852. mCapture->expectColor(Rect(0, 15, 29, 29), Color::RED);
  4853. // red area to the right of the blue area
  4854. mCapture->expectColor(Rect(15, 0, 29, 29), Color::RED);
  4855. mCapture->checkPixel(30, 30, 0, 0, 0);
  4856. }
  4857. TEST_F(ScreenCaptureTest, CaptureInvalidLayer) {
  4858. sp<SurfaceControl> redLayer = createLayer(String8("Red surface"), 60, 60, 0);
  4859. ASSERT_NO_FATAL_FAILURE(fillBufferQueueLayerColor(redLayer, Color::RED, 60, 60));
  4860. auto redLayerHandle = redLayer->getHandle();
  4861. redLayer.clear();
  4862. SurfaceComposerClient::Transaction().apply(true);
  4863. sp<GraphicBuffer> outBuffer;
  4864. // Layer was deleted so captureLayers should fail with NAME_NOT_FOUND
  4865. sp<ISurfaceComposer> sf(ComposerService::getComposerService());
  4866. ASSERT_EQ(NAME_NOT_FOUND, sf->captureLayers(redLayerHandle, &outBuffer, Rect::EMPTY_RECT, 1.0));
  4867. }
  4868. class DereferenceSurfaceControlTest : public LayerTransactionTest {
  4869. protected:
  4870. void SetUp() override {
  4871. LayerTransactionTest::SetUp();
  4872. bgLayer = createLayer("BG layer", 20, 20);
  4873. fillBufferQueueLayerColor(bgLayer, Color::RED, 20, 20);
  4874. fgLayer = createLayer("FG layer", 20, 20);
  4875. fillBufferQueueLayerColor(fgLayer, Color::BLUE, 20, 20);
  4876. Transaction().setLayer(fgLayer, mLayerZBase + 1).apply();
  4877. {
  4878. SCOPED_TRACE("before anything");
  4879. auto shot = screenshot();
  4880. shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
  4881. }
  4882. }
  4883. void TearDown() override {
  4884. LayerTransactionTest::TearDown();
  4885. bgLayer = 0;
  4886. fgLayer = 0;
  4887. }
  4888. sp<SurfaceControl> bgLayer;
  4889. sp<SurfaceControl> fgLayer;
  4890. };
  4891. TEST_F(DereferenceSurfaceControlTest, LayerNotInTransaction) {
  4892. fgLayer = nullptr;
  4893. {
  4894. SCOPED_TRACE("after setting null");
  4895. auto shot = screenshot();
  4896. shot->expectColor(Rect(0, 0, 20, 20), Color::RED);
  4897. }
  4898. }
  4899. TEST_F(DereferenceSurfaceControlTest, LayerInTransaction) {
  4900. auto transaction = Transaction().show(fgLayer);
  4901. fgLayer = nullptr;
  4902. {
  4903. SCOPED_TRACE("after setting null");
  4904. auto shot = screenshot();
  4905. shot->expectColor(Rect(0, 0, 20, 20), Color::BLUE);
  4906. }
  4907. }
  4908. class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
  4909. protected:
  4910. virtual void SetUp() {
  4911. LayerTransactionTest::SetUp();
  4912. ASSERT_EQ(NO_ERROR, mClient->initCheck());
  4913. mMainDisplay = SurfaceComposerClient::getInternalDisplayToken();
  4914. SurfaceComposerClient::getDisplayInfo(mMainDisplay, &mMainDisplayInfo);
  4915. sp<IGraphicBufferConsumer> consumer;
  4916. BufferQueue::createBufferQueue(&mProducer, &consumer);
  4917. consumer->setConsumerName(String8("Virtual disp consumer"));
  4918. consumer->setDefaultBufferSize(mMainDisplayInfo.w, mMainDisplayInfo.h);
  4919. }
  4920. virtual void TearDown() {
  4921. SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
  4922. LayerTransactionTest::TearDown();
  4923. mColorLayer = 0;
  4924. }
  4925. void createDisplay(const Rect& layerStackRect, uint32_t layerStack) {
  4926. mVirtualDisplay =
  4927. SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/);
  4928. asTransaction([&](Transaction& t) {
  4929. t.setDisplaySurface(mVirtualDisplay, mProducer);
  4930. t.setDisplayLayerStack(mVirtualDisplay, layerStack);
  4931. t.setDisplayProjection(mVirtualDisplay, mMainDisplayInfo.orientation, layerStackRect,
  4932. Rect(mMainDisplayInfo.w, mMainDisplayInfo.h));
  4933. });
  4934. }
  4935. void createColorLayer(uint32_t layerStack) {
  4936. mColorLayer =
  4937. createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
  4938. PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceColor);
  4939. ASSERT_TRUE(mColorLayer != nullptr);
  4940. ASSERT_TRUE(mColorLayer->isValid());
  4941. asTransaction([&](Transaction& t) {
  4942. t.setLayerStack(mColorLayer, layerStack);
  4943. t.setCrop_legacy(mColorLayer, Rect(0, 0, 30, 40));
  4944. t.setLayer(mColorLayer, INT32_MAX - 2);
  4945. t.setColor(mColorLayer,
  4946. half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
  4947. mExpectedColor.b / 255.0f});
  4948. t.show(mColorLayer);
  4949. });
  4950. }
  4951. DisplayInfo mMainDisplayInfo;
  4952. sp<IBinder> mMainDisplay;
  4953. sp<IBinder> mVirtualDisplay;
  4954. sp<IGraphicBufferProducer> mProducer;
  4955. sp<SurfaceControl> mColorLayer;
  4956. Color mExpectedColor = {63, 63, 195, 255};
  4957. };
  4958. TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
  4959. createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 1 /* layerStack */);
  4960. createColorLayer(1 /* layerStack */);
  4961. asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
  4962. // Verify color layer does not render on main display.
  4963. std::unique_ptr<ScreenCapture> sc;
  4964. ScreenCapture::captureScreen(&sc, mMainDisplay);
  4965. sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
  4966. sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
  4967. // Verify color layer renders correctly on virtual display.
  4968. ScreenCapture::captureScreen(&sc, mVirtualDisplay);
  4969. sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
  4970. sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 0});
  4971. }
  4972. TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
  4973. // Create a display and set its layer stack to the main display's layer stack so
  4974. // the contents of the main display are mirrored on to the virtual display.
  4975. // Assumption here is that the new mirrored display has the same viewport as the
  4976. // primary display that it is mirroring.
  4977. createDisplay({mMainDisplayInfo.viewportW, mMainDisplayInfo.viewportH}, 0 /* layerStack */);
  4978. createColorLayer(0 /* layerStack */);
  4979. asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
  4980. // Verify color layer renders correctly on main display and it is mirrored on the
  4981. // virtual display.
  4982. std::unique_ptr<ScreenCapture> sc;
  4983. ScreenCapture::captureScreen(&sc, mMainDisplay);
  4984. sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
  4985. sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
  4986. ScreenCapture::captureScreen(&sc, mVirtualDisplay);
  4987. sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
  4988. sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
  4989. }
  4990. class DisplayActiveConfigTest : public ::testing::Test {
  4991. protected:
  4992. void SetUp() override {
  4993. mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
  4994. SurfaceComposerClient::getDisplayConfigs(mDisplayToken, &mDisplayconfigs);
  4995. EXPECT_GT(mDisplayconfigs.size(), 0);
  4996. // set display power to on to make sure config can be changed
  4997. SurfaceComposerClient::setDisplayPowerMode(mDisplayToken, HWC_POWER_MODE_NORMAL);
  4998. }
  4999. sp<IBinder> mDisplayToken;
  5000. Vector<DisplayInfo> mDisplayconfigs;
  5001. };
  5002. TEST_F(DisplayActiveConfigTest, allConfigsAllowed) {
  5003. std::vector<int32_t> allowedConfigs;
  5004. // Add all configs to the allowed configs
  5005. for (int i = 0; i < mDisplayconfigs.size(); i++) {
  5006. allowedConfigs.push_back(i);
  5007. }
  5008. status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
  5009. EXPECT_EQ(res, NO_ERROR);
  5010. std::vector<int32_t> outConfigs;
  5011. res = SurfaceComposerClient::getAllowedDisplayConfigs(mDisplayToken, &outConfigs);
  5012. EXPECT_EQ(res, NO_ERROR);
  5013. EXPECT_EQ(allowedConfigs, outConfigs);
  5014. }
  5015. TEST_F(DisplayActiveConfigTest, changeAllowedConfig) {
  5016. // we need at least 2 configs available for this test
  5017. if (mDisplayconfigs.size() <= 1) return;
  5018. int activeConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
  5019. // We want to set the allowed config to everything but the active config
  5020. std::vector<int32_t> allowedConfigs;
  5021. for (int i = 0; i < mDisplayconfigs.size(); i++) {
  5022. if (i != activeConfig) {
  5023. allowedConfigs.push_back(i);
  5024. }
  5025. }
  5026. status_t res = SurfaceComposerClient::setAllowedDisplayConfigs(mDisplayToken, allowedConfigs);
  5027. EXPECT_EQ(res, NO_ERROR);
  5028. // Allow some time for the config change
  5029. std::this_thread::sleep_for(200ms);
  5030. int newActiveConfig = SurfaceComposerClient::getActiveConfig(mDisplayToken);
  5031. EXPECT_NE(activeConfig, newActiveConfig);
  5032. // Make sure the new config is part of allowed config
  5033. EXPECT_TRUE(std::find(allowedConfigs.begin(), allowedConfigs.end(), newActiveConfig) !=
  5034. allowedConfigs.end());
  5035. }
  5036. class RelativeZTest : public LayerTransactionTest {
  5037. protected:
  5038. virtual void SetUp() {
  5039. LayerTransactionTest::SetUp();
  5040. ASSERT_EQ(NO_ERROR, mClient->initCheck());
  5041. const auto display = SurfaceComposerClient::getInternalDisplayToken();
  5042. ASSERT_FALSE(display == nullptr);
  5043. // Back layer
  5044. mBackgroundLayer = createColorLayer("Background layer", Color::RED);
  5045. // Front layer
  5046. mForegroundLayer = createColorLayer("Foreground layer", Color::GREEN);
  5047. asTransaction([&](Transaction& t) {
  5048. t.setDisplayLayerStack(display, 0);
  5049. t.setLayer(mBackgroundLayer, INT32_MAX - 2).show(mBackgroundLayer);
  5050. t.setLayer(mForegroundLayer, INT32_MAX - 1).show(mForegroundLayer);
  5051. });
  5052. }
  5053. virtual void TearDown() {
  5054. LayerTransactionTest::TearDown();
  5055. mBackgroundLayer = 0;
  5056. mForegroundLayer = 0;
  5057. }
  5058. sp<SurfaceControl> mBackgroundLayer;
  5059. sp<SurfaceControl> mForegroundLayer;
  5060. };
  5061. // When a layer is reparented offscreen, remove relative z order if the relative parent
  5062. // is still onscreen so that the layer is not drawn.
  5063. TEST_F(RelativeZTest, LayerRemoved) {
  5064. std::unique_ptr<ScreenCapture> sc;
  5065. // Background layer (RED)
  5066. // Child layer (WHITE) (relative to foregroud layer)
  5067. // Foregroud layer (GREEN)
  5068. sp<SurfaceControl> childLayer =
  5069. createColorLayer("Child layer", Color::BLUE, mBackgroundLayer.get());
  5070. Transaction{}
  5071. .setRelativeLayer(childLayer, mForegroundLayer->getHandle(), 1)
  5072. .show(childLayer)
  5073. .apply();
  5074. {
  5075. // The childLayer should be in front of the FG control.
  5076. ScreenCapture::captureScreen(&sc);
  5077. sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
  5078. }
  5079. // Background layer (RED)
  5080. // Foregroud layer (GREEN)
  5081. Transaction{}.reparent(childLayer, nullptr).apply();
  5082. // Background layer (RED)
  5083. // Child layer (WHITE)
  5084. // Foregroud layer (GREEN)
  5085. Transaction{}.reparent(childLayer, mBackgroundLayer->getHandle()).apply();
  5086. {
  5087. // The relative z info for child layer should be reset, leaving FG control on top.
  5088. ScreenCapture::captureScreen(&sc);
  5089. sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
  5090. }
  5091. }
  5092. // When a layer is reparented offscreen, preseve relative z order if the relative parent
  5093. // is also offscreen. Regression test b/132613412
  5094. TEST_F(RelativeZTest, LayerRemovedOffscreenRelativeParent) {
  5095. std::unique_ptr<ScreenCapture> sc;
  5096. // Background layer (RED)
  5097. // Foregroud layer (GREEN)
  5098. // child level 1 (WHITE)
  5099. // child level 2a (BLUE)
  5100. // child level 3 (GREEN) (relative to child level 2b)
  5101. // child level 2b (BLACK)
  5102. sp<SurfaceControl> childLevel1 =
  5103. createColorLayer("child level 1", Color::WHITE, mForegroundLayer.get());
  5104. sp<SurfaceControl> childLevel2a =
  5105. createColorLayer("child level 2a", Color::BLUE, childLevel1.get());
  5106. sp<SurfaceControl> childLevel2b =
  5107. createColorLayer("child level 2b", Color::BLACK, childLevel1.get());
  5108. sp<SurfaceControl> childLevel3 =
  5109. createColorLayer("child level 3", Color::GREEN, childLevel2a.get());
  5110. Transaction{}
  5111. .setRelativeLayer(childLevel3, childLevel2b->getHandle(), 1)
  5112. .show(childLevel2a)
  5113. .show(childLevel2b)
  5114. .show(childLevel3)
  5115. .apply();
  5116. {
  5117. // The childLevel3 should be in front of childLevel2b.
  5118. ScreenCapture::captureScreen(&sc);
  5119. sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
  5120. }
  5121. // Background layer (RED)
  5122. // Foregroud layer (GREEN)
  5123. Transaction{}.reparent(childLevel1, nullptr).apply();
  5124. // Background layer (RED)
  5125. // Foregroud layer (GREEN)
  5126. // child level 1 (WHITE)
  5127. // child level 2 back (BLUE)
  5128. // child level 3 (GREEN) (relative to child level 2b)
  5129. // child level 2 front (BLACK)
  5130. Transaction{}.reparent(childLevel1, mForegroundLayer->getHandle()).apply();
  5131. {
  5132. // Nothing should change at this point since relative z info was preserved.
  5133. ScreenCapture::captureScreen(&sc);
  5134. sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
  5135. }
  5136. }
  5137. // This test ensures that when we drop an app buffer in SurfaceFlinger, we merge
  5138. // the dropped buffer's damage region into the next buffer's damage region. If
  5139. // we don't do this, we'll report an incorrect damage region to hardware
  5140. // composer, resulting in broken rendering. This test checks the BufferQueue
  5141. // case.
  5142. //
  5143. // Unfortunately, we don't currently have a way to inspect the damage region
  5144. // SurfaceFlinger sends to hardware composer from a test, so this test requires
  5145. // the dev to manually watch the device's screen during the test to spot broken
  5146. // rendering. Because the results can't be automatically verified, this test is
  5147. // marked disabled.
  5148. TEST_F(LayerTransactionTest, DISABLED_BufferQueueLayerMergeDamageRegionWhenDroppingBuffers) {
  5149. const int width = mDisplayWidth;
  5150. const int height = mDisplayHeight;
  5151. sp<SurfaceControl> layer;
  5152. ASSERT_NO_FATAL_FAILURE(layer = createLayer("test", width, height));
  5153. const auto producer = layer->getIGraphicBufferProducer();
  5154. const sp<IProducerListener> dummyListener(new DummyProducerListener);
  5155. IGraphicBufferProducer::QueueBufferOutput queueBufferOutput;
  5156. ASSERT_EQ(OK,
  5157. producer->connect(dummyListener, NATIVE_WINDOW_API_CPU, true, &queueBufferOutput));
  5158. std::map<int, sp<GraphicBuffer>> slotMap;
  5159. auto slotToBuffer = [&](int slot, sp<GraphicBuffer>* buf) {
  5160. ASSERT_NE(nullptr, buf);
  5161. const auto iter = slotMap.find(slot);
  5162. ASSERT_NE(slotMap.end(), iter);
  5163. *buf = iter->second;
  5164. };
  5165. auto dequeue = [&](int* outSlot) {
  5166. ASSERT_NE(nullptr, outSlot);
  5167. *outSlot = -1;
  5168. int slot;
  5169. sp<Fence> fence;
  5170. uint64_t age;
  5171. FrameEventHistoryDelta timestamps;
  5172. const status_t dequeueResult =
  5173. producer->dequeueBuffer(&slot, &fence, width, height, PIXEL_FORMAT_RGBA_8888,
  5174. GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
  5175. &age, &timestamps);
  5176. if (dequeueResult == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) {
  5177. sp<GraphicBuffer> newBuf;
  5178. ASSERT_EQ(OK, producer->requestBuffer(slot, &newBuf));
  5179. ASSERT_NE(nullptr, newBuf.get());
  5180. slotMap[slot] = newBuf;
  5181. } else {
  5182. ASSERT_EQ(OK, dequeueResult);
  5183. }
  5184. *outSlot = slot;
  5185. };
  5186. auto queue = [&](int slot, const Region& damage, nsecs_t displayTime) {
  5187. IGraphicBufferProducer::QueueBufferInput input(
  5188. /*timestamp=*/displayTime, /*isAutoTimestamp=*/false, HAL_DATASPACE_UNKNOWN,
  5189. /*crop=*/Rect::EMPTY_RECT, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW,
  5190. /*transform=*/0, Fence::NO_FENCE);
  5191. input.setSurfaceDamage(damage);
  5192. IGraphicBufferProducer::QueueBufferOutput output;
  5193. ASSERT_EQ(OK, producer->queueBuffer(slot, input, &output));
  5194. };
  5195. auto fillAndPostBuffers = [&](const Color& color) {
  5196. int slot1;
  5197. ASSERT_NO_FATAL_FAILURE(dequeue(&slot1));
  5198. int slot2;
  5199. ASSERT_NO_FATAL_FAILURE(dequeue(&slot2));
  5200. sp<GraphicBuffer> buf1;
  5201. ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot1, &buf1));
  5202. sp<GraphicBuffer> buf2;
  5203. ASSERT_NO_FATAL_FAILURE(slotToBuffer(slot2, &buf2));
  5204. fillGraphicBufferColor(buf1, Rect(width, height), color);
  5205. fillGraphicBufferColor(buf2, Rect(width, height), color);
  5206. const auto displayTime = systemTime() + milliseconds_to_nanoseconds(100);
  5207. ASSERT_NO_FATAL_FAILURE(queue(slot1, Region::INVALID_REGION, displayTime));
  5208. ASSERT_NO_FATAL_FAILURE(
  5209. queue(slot2, Region(Rect(width / 3, height / 3, 2 * width / 3, 2 * height / 3)),
  5210. displayTime));
  5211. };
  5212. const auto startTime = systemTime();
  5213. const std::array<Color, 3> colors = {Color::RED, Color::GREEN, Color::BLUE};
  5214. int colorIndex = 0;
  5215. while (nanoseconds_to_seconds(systemTime() - startTime) < 10) {
  5216. ASSERT_NO_FATAL_FAILURE(fillAndPostBuffers(colors[colorIndex++ % colors.size()]));
  5217. std::this_thread::sleep_for(1s);
  5218. }
  5219. ASSERT_EQ(OK, producer->disconnect(NATIVE_WINDOW_API_CPU));
  5220. }
  5221. } // namespace android