scheduler_state_machine_unittest.cc 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028
  1. // Copyright 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "cc/scheduler/scheduler_state_machine.h"
  5. #include <stddef.h>
  6. #include "base/test/gtest_util.h"
  7. #include "base/trace_event/trace_event.h"
  8. #include "cc/scheduler/scheduler.h"
  9. #include "components/viz/common/frame_sinks/begin_frame_args.h"
  10. #include "components/viz/test/begin_frame_args_test.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. // Macro to compare two enum values and get nice output.
  14. // Without:
  15. // Value of: actual() Actual: 7
  16. // Expected: expected() Which is: 0
  17. // With:
  18. // Value of: actual() Actual: "ACTION_DRAW"
  19. // Expected: expected() Which is: "ACTION_NONE"
  20. #define EXPECT_ENUM_EQ(enum_tostring, expected, actual) \
  21. EXPECT_THAT(enum_tostring(actual), testing::Eq(enum_tostring(expected)))
  22. #define EXPECT_IMPL_FRAME_STATE(expected) \
  23. EXPECT_ENUM_EQ(BeginImplFrameStateToString, expected, \
  24. state.begin_impl_frame_state())
  25. #define EXPECT_MAIN_FRAME_STATE(expected) \
  26. EXPECT_ENUM_EQ(BeginMainFrameStateToString, expected, \
  27. state.BeginMainFrameState())
  28. #define EXPECT_ACTION(expected) \
  29. EXPECT_ENUM_EQ(ActionToString, expected, state.NextAction())
  30. #define EXPECT_ACTION_UPDATE_STATE(action) \
  31. EXPECT_ACTION(action); \
  32. if (action == SchedulerStateMachine::Action::DRAW_IF_POSSIBLE || \
  33. action == SchedulerStateMachine::Action::DRAW_FORCED) { \
  34. EXPECT_IMPL_FRAME_STATE( \
  35. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE); \
  36. } \
  37. PerformAction(&state, action); \
  38. if (action == SchedulerStateMachine::Action::NONE) { \
  39. if (state.begin_impl_frame_state() == \
  40. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE) \
  41. state.OnBeginImplFrameIdle(); \
  42. }
  43. #define SET_UP_STATE(state) \
  44. state.SetVisible(true); \
  45. EXPECT_ACTION_UPDATE_STATE( \
  46. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION); \
  47. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE); \
  48. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit(); \
  49. state.SetCanDraw(true)
  50. namespace cc {
  51. namespace {
  52. const char* BeginImplFrameStateToString(
  53. SchedulerStateMachine::BeginImplFrameState state) {
  54. using BeginImplFrameState = SchedulerStateMachine::BeginImplFrameState;
  55. switch (state) {
  56. case BeginImplFrameState::IDLE:
  57. return "BeginImplFrameState::IDLE";
  58. case BeginImplFrameState::INSIDE_BEGIN_FRAME:
  59. return "BeginImplFrameState::INSIDE_BEGIN_FRAME";
  60. case BeginImplFrameState::INSIDE_DEADLINE:
  61. return "BeginImplFrameState::INSIDE_DEADLINE";
  62. }
  63. NOTREACHED();
  64. return "???";
  65. }
  66. const char* BeginMainFrameStateToString(
  67. SchedulerStateMachine::BeginMainFrameState state) {
  68. using BeginMainFrameState = SchedulerStateMachine::BeginMainFrameState;
  69. switch (state) {
  70. case BeginMainFrameState::IDLE:
  71. return "BeginMainFrameState::IDLE";
  72. case BeginMainFrameState::SENT:
  73. return "BeginMainFrameState::SENT";
  74. case BeginMainFrameState::READY_TO_COMMIT:
  75. return "BeginMainFrameState::READY_TO_COMMIT";
  76. }
  77. NOTREACHED();
  78. return "???";
  79. }
  80. const char* ActionToString(SchedulerStateMachine::Action action) {
  81. using Action = SchedulerStateMachine::Action;
  82. switch (action) {
  83. case Action::NONE:
  84. return "Action::NONE";
  85. case Action::SEND_BEGIN_MAIN_FRAME:
  86. return "Action::SEND_BEGIN_MAIN_FRAME";
  87. case Action::COMMIT:
  88. return "Action::COMMIT";
  89. case Action::POST_COMMIT:
  90. return "Action::POST_COMMIT";
  91. case Action::ACTIVATE_SYNC_TREE:
  92. return "Action::ACTIVATE_SYNC_TREE";
  93. case Action::DRAW_IF_POSSIBLE:
  94. return "Action::DRAW_IF_POSSIBLE";
  95. case Action::DRAW_FORCED:
  96. return "Action::DRAW_FORCED";
  97. case Action::DRAW_ABORT:
  98. return "Action::DRAW_ABORT";
  99. case Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION:
  100. return "Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION";
  101. case Action::PREPARE_TILES:
  102. return "Action::PREPARE_TILES";
  103. case Action::INVALIDATE_LAYER_TREE_FRAME_SINK:
  104. return "Action::INVALIDATE_LAYER_TREE_FRAME_SINK";
  105. case Action::PERFORM_IMPL_SIDE_INVALIDATION:
  106. return "Action::PERFORM_IMPL_SIDE_INVALIDATION";
  107. case Action::NOTIFY_BEGIN_MAIN_FRAME_NOT_EXPECTED_UNTIL:
  108. return "Action::NOTIFY_BEGIN_MAIN_FRAME_NOT_EXPECTED_UNTIL";
  109. case Action::NOTIFY_BEGIN_MAIN_FRAME_NOT_EXPECTED_SOON:
  110. return "Action::NOTIFY_BEGIN_MAIN_FRAME_NOT_EXPECTED_SOON";
  111. }
  112. NOTREACHED();
  113. return "???";
  114. }
  115. const bool kAnimateOnly = false;
  116. const SchedulerStateMachine::BeginImplFrameState all_begin_impl_frame_states[] =
  117. {
  118. SchedulerStateMachine::BeginImplFrameState::IDLE,
  119. SchedulerStateMachine::BeginImplFrameState::INSIDE_BEGIN_FRAME,
  120. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE,
  121. };
  122. const SchedulerStateMachine::BeginMainFrameState begin_main_frame_states[] = {
  123. SchedulerStateMachine::BeginMainFrameState::IDLE,
  124. SchedulerStateMachine::BeginMainFrameState::SENT,
  125. SchedulerStateMachine::BeginMainFrameState::READY_TO_COMMIT};
  126. // Exposes the protected state fields of the SchedulerStateMachine for testing
  127. class StateMachine : public SchedulerStateMachine {
  128. public:
  129. explicit StateMachine(const SchedulerSettings& scheduler_settings)
  130. : SchedulerStateMachine(scheduler_settings),
  131. draw_result_for_test_(DRAW_SUCCESS) {}
  132. void CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit() {
  133. DidCreateAndInitializeLayerTreeFrameSink();
  134. layer_tree_frame_sink_state_ = LayerTreeFrameSinkState::ACTIVE;
  135. }
  136. void IssueNextBeginImplFrame() {
  137. OnBeginImplFrame(viz::BeginFrameId(0, next_begin_frame_number_++),
  138. kAnimateOnly);
  139. }
  140. void SetBeginMainFrameState(BeginMainFrameState cs) {
  141. begin_main_frame_state_ = cs;
  142. }
  143. BeginMainFrameState BeginMainFrameState() const {
  144. return begin_main_frame_state_;
  145. }
  146. ForcedRedrawOnTimeoutState ForcedRedrawState() const {
  147. return forced_redraw_state_;
  148. }
  149. void SetBeginImplFrameState(BeginImplFrameState bifs) {
  150. begin_impl_frame_state_ = bifs;
  151. }
  152. BeginImplFrameState begin_impl_frame_state() const {
  153. return begin_impl_frame_state_;
  154. }
  155. LayerTreeFrameSinkState layer_tree_frame_sink_state() const {
  156. return layer_tree_frame_sink_state_;
  157. }
  158. void SetNeedsBeginMainFrameForTest(bool needs_begin_main_frame) {
  159. needs_begin_main_frame_ = needs_begin_main_frame;
  160. }
  161. bool NeedsCommit() const { return needs_begin_main_frame_; }
  162. void SetNeedsOneBeginImplFrame(bool needs_frame) {
  163. needs_one_begin_impl_frame_ = needs_frame;
  164. }
  165. void SetNeedsRedraw(bool needs_redraw) { needs_redraw_ = needs_redraw; }
  166. void SetDrawResultForTest(DrawResult draw_result) {
  167. draw_result_for_test_ = draw_result;
  168. }
  169. DrawResult draw_result_for_test() { return draw_result_for_test_; }
  170. void SetNeedsForcedRedrawForTimeout(bool b) {
  171. forced_redraw_state_ = ForcedRedrawOnTimeoutState::WAITING_FOR_COMMIT;
  172. active_tree_needs_first_draw_ = true;
  173. }
  174. bool NeedsForcedRedrawForTimeout() const {
  175. return forced_redraw_state_ != ForcedRedrawOnTimeoutState::IDLE;
  176. }
  177. void SetActiveTreeNeedsFirstDraw(bool needs_first_draw) {
  178. active_tree_needs_first_draw_ = needs_first_draw;
  179. }
  180. bool CanDraw() const { return can_draw_; }
  181. bool Visible() const { return visible_; }
  182. bool ShouldAbortCurrentFrame() const {
  183. return SchedulerStateMachine::ShouldAbortCurrentFrame();
  184. }
  185. bool has_pending_tree() const { return has_pending_tree_; }
  186. void SetHasPendingTree(bool has_pending_tree) {
  187. has_pending_tree_ = has_pending_tree;
  188. }
  189. bool needs_impl_side_invalidation() const {
  190. return needs_impl_side_invalidation_;
  191. }
  192. using SchedulerStateMachine::ProactiveBeginFrameWanted;
  193. using SchedulerStateMachine::ShouldDraw;
  194. using SchedulerStateMachine::ShouldPrepareTiles;
  195. using SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately;
  196. using SchedulerStateMachine::WillCommit;
  197. protected:
  198. DrawResult draw_result_for_test_;
  199. uint64_t next_begin_frame_number_ = viz::BeginFrameArgs::kStartingFrameNumber;
  200. };
  201. void PerformAction(StateMachine* sm, SchedulerStateMachine::Action action) {
  202. switch (action) {
  203. case SchedulerStateMachine::Action::NONE:
  204. return;
  205. case SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE:
  206. sm->WillActivate();
  207. return;
  208. case SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME:
  209. sm->WillSendBeginMainFrame();
  210. return;
  211. case SchedulerStateMachine::Action::
  212. NOTIFY_BEGIN_MAIN_FRAME_NOT_EXPECTED_UNTIL:
  213. sm->WillNotifyBeginMainFrameNotExpectedUntil();
  214. return;
  215. case SchedulerStateMachine::Action::
  216. NOTIFY_BEGIN_MAIN_FRAME_NOT_EXPECTED_SOON:
  217. sm->WillNotifyBeginMainFrameNotExpectedSoon();
  218. return;
  219. case SchedulerStateMachine::Action::COMMIT: {
  220. bool commit_has_no_updates = false;
  221. sm->WillCommit(commit_has_no_updates);
  222. sm->DidCommit();
  223. return;
  224. }
  225. case SchedulerStateMachine::Action::POST_COMMIT:
  226. sm->DidPostCommit();
  227. return;
  228. case SchedulerStateMachine::Action::DRAW_FORCED:
  229. case SchedulerStateMachine::Action::DRAW_IF_POSSIBLE: {
  230. sm->WillDraw();
  231. sm->DidDraw(sm->draw_result_for_test());
  232. return;
  233. }
  234. case SchedulerStateMachine::Action::DRAW_ABORT: {
  235. sm->AbortDraw();
  236. return;
  237. }
  238. case SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION:
  239. sm->WillBeginLayerTreeFrameSinkCreation();
  240. return;
  241. case SchedulerStateMachine::Action::PREPARE_TILES:
  242. sm->WillPrepareTiles();
  243. return;
  244. case SchedulerStateMachine::Action::INVALIDATE_LAYER_TREE_FRAME_SINK:
  245. sm->WillInvalidateLayerTreeFrameSink();
  246. return;
  247. case SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION:
  248. sm->WillPerformImplSideInvalidation();
  249. return;
  250. }
  251. }
  252. TEST(SchedulerStateMachineTest, BeginFrameNeeded) {
  253. SchedulerSettings default_scheduler_settings;
  254. StateMachine state(default_scheduler_settings);
  255. state.SetVisible(true);
  256. EXPECT_ACTION_UPDATE_STATE(
  257. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  258. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  259. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  260. state.SetBeginMainFrameState(
  261. SchedulerStateMachine::BeginMainFrameState::IDLE);
  262. // Don't request BeginFrames if we are idle.
  263. state.SetNeedsRedraw(false);
  264. state.SetNeedsOneBeginImplFrame(false);
  265. EXPECT_FALSE(state.BeginFrameNeeded());
  266. // Request BeginFrames if we one is needed.
  267. state.SetNeedsRedraw(false);
  268. state.SetNeedsOneBeginImplFrame(true);
  269. EXPECT_TRUE(state.BeginFrameNeeded());
  270. // Request BeginFrames if we are ready to draw.
  271. state.SetVisible(true);
  272. state.SetNeedsRedraw(true);
  273. state.SetNeedsOneBeginImplFrame(false);
  274. EXPECT_TRUE(state.BeginFrameNeeded());
  275. // Don't background tick for needs_redraw.
  276. state.SetVisible(false);
  277. state.SetNeedsRedraw(true);
  278. state.SetNeedsOneBeginImplFrame(false);
  279. EXPECT_FALSE(state.BeginFrameNeeded());
  280. // Proactively request BeginFrames when commit is pending.
  281. state.SetVisible(true);
  282. state.SetNeedsRedraw(false);
  283. state.SetNeedsOneBeginImplFrame(false);
  284. state.SetNeedsBeginMainFrameForTest(true);
  285. EXPECT_TRUE(state.BeginFrameNeeded());
  286. // Don't request BeginFrames when commit is pending if
  287. // we are currently deferring commits.
  288. state.SetVisible(true);
  289. state.SetNeedsRedraw(false);
  290. state.SetNeedsOneBeginImplFrame(false);
  291. state.SetNeedsBeginMainFrameForTest(true);
  292. state.SetDeferBeginMainFrame(true);
  293. EXPECT_FALSE(state.BeginFrameNeeded());
  294. }
  295. TEST(SchedulerStateMachineTest, BeginMainFrameIsHighestPriorityAction) {
  296. SchedulerSettings default_scheduler_settings;
  297. default_scheduler_settings.main_frame_before_activation_enabled = true;
  298. StateMachine state(default_scheduler_settings);
  299. SET_UP_STATE(state);
  300. state.SetNeedsBeginMainFrame();
  301. state.IssueNextBeginImplFrame();
  302. EXPECT_ACTION_UPDATE_STATE(
  303. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  304. state.NotifyReadyToCommit();
  305. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  306. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  307. state.NotifyReadyToActivate();
  308. EXPECT_ACTION(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  309. // Still need to active, but sending BMF takes priority.
  310. state.SetNeedsBeginMainFrame();
  311. state.IssueNextBeginImplFrame();
  312. EXPECT_ACTION_UPDATE_STATE(
  313. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  314. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  315. state.NotifyReadyToCommit();
  316. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  317. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  318. state.OnBeginImplFrameDeadline();
  319. EXPECT_ACTION(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  320. // Still need to draw, but sending BMF takes priority.
  321. state.SetNeedsBeginMainFrame();
  322. state.IssueNextBeginImplFrame();
  323. EXPECT_ACTION(SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  324. }
  325. TEST(SchedulerStateMachineTest,
  326. TestNextActionNotifyBeginMainFrameNotExpectedUntil) {
  327. SchedulerSettings default_scheduler_settings;
  328. StateMachine state(default_scheduler_settings);
  329. state.SetMainThreadWantsBeginMainFrameNotExpectedMessages(true);
  330. state.SetVisible(true);
  331. EXPECT_ACTION_UPDATE_STATE(
  332. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  333. state.IssueNextBeginImplFrame();
  334. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  335. state.SetNeedsOneBeginImplFrame(true);
  336. EXPECT_TRUE(state.BeginFrameNeeded());
  337. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::
  338. NOTIFY_BEGIN_MAIN_FRAME_NOT_EXPECTED_UNTIL);
  339. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  340. state.SetNeedsRedraw(true);
  341. state.SetNeedsBeginMainFrame();
  342. EXPECT_ACTION_UPDATE_STATE(
  343. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  344. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  345. }
  346. TEST(SchedulerStateMachineTest,
  347. TestNextActionNotifyBeginMainFrameNotExpectedSoon) {
  348. SchedulerSettings default_scheduler_settings;
  349. StateMachine state(default_scheduler_settings);
  350. state.SetMainThreadWantsBeginMainFrameNotExpectedMessages(true);
  351. state.SetVisible(true);
  352. EXPECT_ACTION_UPDATE_STATE(
  353. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  354. state.IssueNextBeginImplFrame();
  355. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  356. state.SetNeedsOneBeginImplFrame(true);
  357. EXPECT_TRUE(state.BeginFrameNeeded());
  358. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::
  359. NOTIFY_BEGIN_MAIN_FRAME_NOT_EXPECTED_UNTIL);
  360. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  361. state.SetNeedsOneBeginImplFrame(false);
  362. EXPECT_FALSE(state.BeginFrameNeeded());
  363. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  364. state.SetBeginImplFrameState(
  365. SchedulerStateMachine::BeginImplFrameState::IDLE);
  366. EXPECT_ACTION_UPDATE_STATE(
  367. SchedulerStateMachine::Action::NOTIFY_BEGIN_MAIN_FRAME_NOT_EXPECTED_SOON);
  368. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  369. }
  370. TEST(SchedulerStateMachineTest, TestNextActionBeginsMainFrameIfNeeded) {
  371. SchedulerSettings default_scheduler_settings;
  372. // If no commit needed, do nothing.
  373. {
  374. StateMachine state(default_scheduler_settings);
  375. state.SetVisible(true);
  376. EXPECT_ACTION_UPDATE_STATE(
  377. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  378. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  379. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  380. state.SetBeginMainFrameState(
  381. SchedulerStateMachine::BeginMainFrameState::IDLE);
  382. state.SetNeedsRedraw(false);
  383. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  384. EXPECT_FALSE(state.NeedsCommit());
  385. state.IssueNextBeginImplFrame();
  386. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  387. state.OnBeginImplFrameDeadline();
  388. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  389. EXPECT_FALSE(state.NeedsCommit());
  390. }
  391. // If commit requested but not visible yet, do nothing.
  392. {
  393. StateMachine state(default_scheduler_settings);
  394. state.SetBeginMainFrameState(
  395. SchedulerStateMachine::BeginMainFrameState::IDLE);
  396. state.SetNeedsRedraw(false);
  397. state.SetNeedsBeginMainFrame();
  398. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  399. EXPECT_TRUE(state.NeedsCommit());
  400. state.IssueNextBeginImplFrame();
  401. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  402. state.OnBeginImplFrameDeadline();
  403. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  404. EXPECT_TRUE(state.NeedsCommit());
  405. }
  406. // If commit requested, begin a main frame.
  407. {
  408. StateMachine state(default_scheduler_settings);
  409. state.SetBeginMainFrameState(
  410. SchedulerStateMachine::BeginMainFrameState::IDLE);
  411. state.SetVisible(true);
  412. EXPECT_ACTION_UPDATE_STATE(
  413. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  414. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  415. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  416. state.SetNeedsRedraw(false);
  417. state.SetNeedsBeginMainFrame();
  418. // Expect nothing to happen until after OnBeginImplFrame.
  419. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  420. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  421. EXPECT_IMPL_FRAME_STATE(SchedulerStateMachine::BeginImplFrameState::IDLE);
  422. EXPECT_TRUE(state.NeedsCommit());
  423. EXPECT_TRUE(state.BeginFrameNeeded());
  424. state.IssueNextBeginImplFrame();
  425. EXPECT_ACTION_UPDATE_STATE(
  426. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  427. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  428. EXPECT_FALSE(state.NeedsCommit());
  429. }
  430. // If commit requested and can't draw, still begin a main frame.
  431. {
  432. StateMachine state(default_scheduler_settings);
  433. state.SetBeginMainFrameState(
  434. SchedulerStateMachine::BeginMainFrameState::IDLE);
  435. state.SetVisible(true);
  436. EXPECT_ACTION_UPDATE_STATE(
  437. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  438. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  439. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  440. state.SetNeedsRedraw(false);
  441. state.SetNeedsBeginMainFrame();
  442. state.SetCanDraw(false);
  443. // Expect nothing to happen until after OnBeginImplFrame.
  444. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  445. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  446. EXPECT_IMPL_FRAME_STATE(SchedulerStateMachine::BeginImplFrameState::IDLE);
  447. EXPECT_TRUE(state.BeginFrameNeeded());
  448. state.IssueNextBeginImplFrame();
  449. EXPECT_ACTION_UPDATE_STATE(
  450. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  451. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  452. EXPECT_FALSE(state.NeedsCommit());
  453. }
  454. }
  455. // Explicitly test main_frame_before_activation_enabled = true
  456. TEST(SchedulerStateMachineTest, MainFrameBeforeActivationEnabled) {
  457. SchedulerSettings scheduler_settings;
  458. scheduler_settings.main_frame_before_activation_enabled = true;
  459. StateMachine state(scheduler_settings);
  460. state.SetBeginMainFrameState(
  461. SchedulerStateMachine::BeginMainFrameState::IDLE);
  462. SET_UP_STATE(state);
  463. state.SetNeedsRedraw(false);
  464. state.SetNeedsBeginMainFrame();
  465. EXPECT_TRUE(state.BeginFrameNeeded());
  466. // Commit to the pending tree.
  467. state.IssueNextBeginImplFrame();
  468. EXPECT_ACTION_UPDATE_STATE(
  469. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  470. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  471. state.NotifyReadyToCommit();
  472. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  473. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  474. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  475. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  476. state.OnBeginImplFrameDeadline();
  477. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  478. // Verify that the next commit starts while there is still a pending tree.
  479. state.SetNeedsBeginMainFrame();
  480. state.IssueNextBeginImplFrame();
  481. EXPECT_ACTION_UPDATE_STATE(
  482. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  483. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  484. // Verify the pending commit doesn't overwrite the pending
  485. // tree until the pending tree has been activated.
  486. state.NotifyReadyToCommit();
  487. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  488. // Verify NotifyReadyToActivate unblocks activation, commit, and
  489. // draw in that order.
  490. state.NotifyReadyToActivate();
  491. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  492. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  493. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  494. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  495. EXPECT_TRUE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  496. state.OnBeginImplFrameDeadline();
  497. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  498. state.DidSubmitCompositorFrame();
  499. state.DidReceiveCompositorFrameAck();
  500. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  501. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  502. }
  503. TEST(SchedulerStateMachineTest,
  504. FailedDrawForAnimationCheckerboardSetsNeedsCommitAndRetriesDraw) {
  505. SchedulerSettings default_scheduler_settings;
  506. StateMachine state(default_scheduler_settings);
  507. SET_UP_STATE(state);
  508. state.SetNeedsRedraw(true);
  509. EXPECT_TRUE(state.RedrawPending());
  510. EXPECT_TRUE(state.BeginFrameNeeded());
  511. // Start a frame.
  512. state.IssueNextBeginImplFrame();
  513. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  514. EXPECT_FALSE(state.CommitPending());
  515. // Failing a draw triggers request for a new BeginMainFrame.
  516. state.OnBeginImplFrameDeadline();
  517. state.SetDrawResultForTest(DRAW_ABORTED_CHECKERBOARD_ANIMATIONS);
  518. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  519. EXPECT_ACTION_UPDATE_STATE(
  520. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  521. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  522. state.OnBeginImplFrameIdle();
  523. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  524. // It's okay to attempt more draws just in case additional raster
  525. // finishes and the requested commit wasn't actually necessary.
  526. EXPECT_TRUE(state.CommitPending());
  527. EXPECT_TRUE(state.RedrawPending());
  528. state.IssueNextBeginImplFrame();
  529. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  530. state.OnBeginImplFrameDeadline();
  531. state.SetDrawResultForTest(DRAW_ABORTED_CHECKERBOARD_ANIMATIONS);
  532. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  533. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  534. state.OnBeginImplFrameIdle();
  535. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  536. }
  537. TEST(SchedulerStateMachineTest, FailedDrawForMissingHighResNeedsCommit) {
  538. SchedulerSettings default_scheduler_settings;
  539. StateMachine state(default_scheduler_settings);
  540. SET_UP_STATE(state);
  541. state.SetNeedsRedraw(true);
  542. EXPECT_TRUE(state.RedrawPending());
  543. EXPECT_TRUE(state.BeginFrameNeeded());
  544. // Start a frame.
  545. state.IssueNextBeginImplFrame();
  546. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  547. EXPECT_FALSE(state.CommitPending());
  548. // Failing a draw triggers because of high res tiles missing
  549. // request for a new BeginMainFrame.
  550. state.OnBeginImplFrameDeadline();
  551. state.SetDrawResultForTest(DRAW_ABORTED_MISSING_HIGH_RES_CONTENT);
  552. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  553. EXPECT_ACTION_UPDATE_STATE(
  554. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  555. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  556. state.OnBeginImplFrameIdle();
  557. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  558. // It doesn't request a draw until we get a new commit though.
  559. EXPECT_TRUE(state.CommitPending());
  560. EXPECT_FALSE(state.RedrawPending());
  561. state.IssueNextBeginImplFrame();
  562. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  563. state.OnBeginImplFrameDeadline();
  564. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  565. state.OnBeginImplFrameIdle();
  566. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  567. // Finish the commit and activation.
  568. state.NotifyReadyToCommit();
  569. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  570. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  571. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  572. state.NotifyReadyToActivate();
  573. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  574. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  575. EXPECT_TRUE(state.RedrawPending());
  576. // Verify we draw with the new frame.
  577. state.IssueNextBeginImplFrame();
  578. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  579. state.OnBeginImplFrameDeadline();
  580. state.SetDrawResultForTest(DRAW_SUCCESS);
  581. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  582. state.DidSubmitCompositorFrame();
  583. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  584. state.OnBeginImplFrameIdle();
  585. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  586. }
  587. TEST(SchedulerStateMachineTest,
  588. TestFailedDrawsEventuallyForceDrawAfterNextCommit) {
  589. SchedulerSettings scheduler_settings;
  590. scheduler_settings.maximum_number_of_failed_draws_before_draw_is_forced = 1;
  591. StateMachine state(scheduler_settings);
  592. SET_UP_STATE(state);
  593. // Start a commit.
  594. state.SetNeedsBeginMainFrame();
  595. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  596. state.IssueNextBeginImplFrame();
  597. EXPECT_ACTION_UPDATE_STATE(
  598. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  599. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  600. EXPECT_TRUE(state.CommitPending());
  601. // Then initiate a draw that fails.
  602. state.SetNeedsRedraw(true);
  603. state.OnBeginImplFrameDeadline();
  604. state.SetDrawResultForTest(DRAW_ABORTED_CHECKERBOARD_ANIMATIONS);
  605. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  606. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  607. EXPECT_TRUE(state.BeginFrameNeeded());
  608. EXPECT_TRUE(state.RedrawPending());
  609. EXPECT_TRUE(state.CommitPending());
  610. // Finish the commit. Note, we should not yet be forcing a draw, but should
  611. // continue the commit as usual.
  612. state.NotifyReadyToCommit();
  613. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  614. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  615. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  616. EXPECT_TRUE(state.RedrawPending());
  617. // Activate so we're ready for a new main frame.
  618. state.NotifyReadyToActivate();
  619. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  620. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  621. EXPECT_TRUE(state.RedrawPending());
  622. // The redraw should be forced at the end of the next BeginImplFrame.
  623. state.IssueNextBeginImplFrame();
  624. EXPECT_ACTION_UPDATE_STATE(
  625. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  626. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  627. state.OnBeginImplFrameDeadline();
  628. state.SetDrawResultForTest(DRAW_SUCCESS);
  629. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_FORCED);
  630. state.DidSubmitCompositorFrame();
  631. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  632. state.DidReceiveCompositorFrameAck();
  633. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  634. }
  635. TEST(SchedulerStateMachineTest, TestFailedDrawsDoNotRestartForcedDraw) {
  636. SchedulerSettings scheduler_settings;
  637. int draw_limit = 2;
  638. scheduler_settings.maximum_number_of_failed_draws_before_draw_is_forced =
  639. draw_limit;
  640. StateMachine state(scheduler_settings);
  641. SET_UP_STATE(state);
  642. // Start a commit.
  643. state.SetNeedsBeginMainFrame();
  644. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  645. state.IssueNextBeginImplFrame();
  646. EXPECT_ACTION_UPDATE_STATE(
  647. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  648. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  649. EXPECT_TRUE(state.CommitPending());
  650. // Then initiate a draw.
  651. state.SetNeedsRedraw(true);
  652. state.OnBeginImplFrameDeadline();
  653. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  654. // Fail the draw enough times to force a redraw.
  655. for (int i = 0; i < draw_limit; ++i) {
  656. state.SetNeedsRedraw(true);
  657. state.IssueNextBeginImplFrame();
  658. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  659. state.OnBeginImplFrameDeadline();
  660. state.SetDrawResultForTest(DRAW_ABORTED_CHECKERBOARD_ANIMATIONS);
  661. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  662. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  663. state.OnBeginImplFrameIdle();
  664. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  665. }
  666. EXPECT_TRUE(state.BeginFrameNeeded());
  667. EXPECT_TRUE(state.RedrawPending());
  668. // But the commit is ongoing.
  669. EXPECT_TRUE(state.CommitPending());
  670. EXPECT_TRUE(
  671. state.ForcedRedrawState() ==
  672. SchedulerStateMachine::ForcedRedrawOnTimeoutState::WAITING_FOR_COMMIT);
  673. // After failing additional draws, we should still be in a forced
  674. // redraw, but not back in IDLE.
  675. for (int i = 0; i < draw_limit; ++i) {
  676. state.SetNeedsRedraw(true);
  677. state.IssueNextBeginImplFrame();
  678. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  679. state.OnBeginImplFrameDeadline();
  680. state.SetDrawResultForTest(DRAW_ABORTED_CHECKERBOARD_ANIMATIONS);
  681. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  682. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  683. state.OnBeginImplFrameIdle();
  684. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  685. }
  686. EXPECT_TRUE(state.RedrawPending());
  687. EXPECT_TRUE(
  688. state.ForcedRedrawState() ==
  689. SchedulerStateMachine::ForcedRedrawOnTimeoutState::WAITING_FOR_COMMIT);
  690. }
  691. TEST(SchedulerStateMachineTest, TestFailedDrawIsRetriedInNextBeginImplFrame) {
  692. SchedulerSettings default_scheduler_settings;
  693. StateMachine state(default_scheduler_settings);
  694. SET_UP_STATE(state);
  695. // Start a draw.
  696. state.SetNeedsRedraw(true);
  697. EXPECT_TRUE(state.BeginFrameNeeded());
  698. state.IssueNextBeginImplFrame();
  699. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  700. state.OnBeginImplFrameDeadline();
  701. EXPECT_TRUE(state.RedrawPending());
  702. state.SetDrawResultForTest(DRAW_ABORTED_CHECKERBOARD_ANIMATIONS);
  703. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  704. // Failing the draw for animation checkerboards makes us require a commit.
  705. EXPECT_ACTION_UPDATE_STATE(
  706. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  707. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  708. EXPECT_TRUE(state.RedrawPending());
  709. // We should not be trying to draw again now, but we have a commit pending.
  710. EXPECT_TRUE(state.BeginFrameNeeded());
  711. state.IssueNextBeginImplFrame();
  712. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  713. // We should try to draw again at the end of the next BeginImplFrame on
  714. // the impl thread.
  715. state.OnBeginImplFrameDeadline();
  716. state.SetDrawResultForTest(DRAW_SUCCESS);
  717. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  718. state.DidSubmitCompositorFrame();
  719. state.DidReceiveCompositorFrameAck();
  720. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  721. }
  722. TEST(SchedulerStateMachineTest, TestDoestDrawTwiceInSameFrame) {
  723. SchedulerSettings default_scheduler_settings;
  724. StateMachine state(default_scheduler_settings);
  725. SET_UP_STATE(state);
  726. state.SetNeedsRedraw(true);
  727. // Draw the first frame.
  728. EXPECT_TRUE(state.BeginFrameNeeded());
  729. state.IssueNextBeginImplFrame();
  730. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  731. state.OnBeginImplFrameDeadline();
  732. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  733. state.DidSubmitCompositorFrame();
  734. state.DidReceiveCompositorFrameAck();
  735. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  736. // Before the next BeginImplFrame, set needs redraw again.
  737. // This should not redraw until the next BeginImplFrame.
  738. state.SetNeedsRedraw(true);
  739. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  740. // Move to another frame. This should now draw.
  741. EXPECT_TRUE(state.BeginFrameNeeded());
  742. state.IssueNextBeginImplFrame();
  743. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  744. state.OnBeginImplFrameDeadline();
  745. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  746. state.DidSubmitCompositorFrame();
  747. state.DidReceiveCompositorFrameAck();
  748. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  749. // We just submitted, so we should proactively request another BeginImplFrame.
  750. EXPECT_TRUE(state.BeginFrameNeeded());
  751. }
  752. TEST(SchedulerStateMachineTest, TestNextActionDrawsOnBeginImplFrame) {
  753. SchedulerSettings default_scheduler_settings;
  754. // When not in BeginImplFrame deadline, or in BeginImplFrame deadline
  755. // but not visible, don't draw.
  756. size_t num_begin_main_frame_states =
  757. sizeof(begin_main_frame_states) /
  758. sizeof(SchedulerStateMachine::BeginMainFrameState);
  759. size_t num_begin_impl_frame_states =
  760. sizeof(all_begin_impl_frame_states) /
  761. sizeof(SchedulerStateMachine::BeginImplFrameState);
  762. for (size_t i = 0; i < num_begin_main_frame_states; ++i) {
  763. for (size_t j = 0; j < num_begin_impl_frame_states; ++j) {
  764. StateMachine state(default_scheduler_settings);
  765. state.SetVisible(true);
  766. EXPECT_ACTION_UPDATE_STATE(
  767. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  768. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  769. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  770. state.SetBeginMainFrameState(begin_main_frame_states[i]);
  771. state.SetBeginImplFrameState(all_begin_impl_frame_states[j]);
  772. bool visible =
  773. (all_begin_impl_frame_states[j] !=
  774. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE);
  775. state.SetVisible(visible);
  776. // Case 1: needs_begin_main_frame=false
  777. EXPECT_NE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE,
  778. state.NextAction());
  779. // Case 2: needs_begin_main_frame=true
  780. state.SetNeedsBeginMainFrame();
  781. EXPECT_NE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE,
  782. state.NextAction());
  783. }
  784. }
  785. // When in BeginImplFrame deadline we should always draw for SetNeedsRedraw
  786. // except if we're ready to commit, in which case we expect a commit first.
  787. for (size_t i = 0; i < num_begin_main_frame_states; ++i) {
  788. StateMachine state(default_scheduler_settings);
  789. state.SetVisible(true);
  790. EXPECT_ACTION_UPDATE_STATE(
  791. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  792. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  793. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  794. state.SetCanDraw(true);
  795. state.SetBeginMainFrameState(begin_main_frame_states[i]);
  796. state.SetBeginImplFrameState(
  797. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE);
  798. state.SetNeedsRedraw(true);
  799. SchedulerStateMachine::Action expected_action;
  800. if (begin_main_frame_states[i] ==
  801. SchedulerStateMachine::BeginMainFrameState::READY_TO_COMMIT) {
  802. expected_action = SchedulerStateMachine::Action::COMMIT;
  803. } else {
  804. expected_action = SchedulerStateMachine::Action::DRAW_IF_POSSIBLE;
  805. }
  806. // Case 1: needs_begin_main_frame=false.
  807. EXPECT_ACTION(expected_action);
  808. // Case 2: needs_begin_main_frame=true.
  809. state.SetNeedsBeginMainFrame();
  810. EXPECT_ACTION(expected_action);
  811. }
  812. }
  813. TEST(SchedulerStateMachineTest, TestNoBeginMainFrameStatesRedrawWhenInvisible) {
  814. SchedulerSettings default_scheduler_settings;
  815. size_t num_begin_main_frame_states =
  816. sizeof(begin_main_frame_states) /
  817. sizeof(SchedulerStateMachine::BeginMainFrameState);
  818. for (size_t i = 0; i < num_begin_main_frame_states; ++i) {
  819. // There shouldn't be any drawing regardless of BeginImplFrame.
  820. for (size_t j = 0; j < 2; ++j) {
  821. StateMachine state(default_scheduler_settings);
  822. state.SetVisible(true);
  823. EXPECT_ACTION_UPDATE_STATE(
  824. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  825. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  826. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  827. state.SetBeginMainFrameState(begin_main_frame_states[i]);
  828. state.SetVisible(false);
  829. state.SetNeedsRedraw(true);
  830. if (j == 1) {
  831. state.SetBeginImplFrameState(
  832. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE);
  833. }
  834. // Case 1: needs_begin_main_frame=false.
  835. EXPECT_NE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE,
  836. state.NextAction());
  837. // Case 2: needs_begin_main_frame=true.
  838. state.SetNeedsBeginMainFrame();
  839. EXPECT_NE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE,
  840. state.NextAction());
  841. }
  842. }
  843. }
  844. TEST(SchedulerStateMachineTest, TestCanRedraw_StopsDraw) {
  845. SchedulerSettings default_scheduler_settings;
  846. size_t num_begin_main_frame_states =
  847. sizeof(begin_main_frame_states) /
  848. sizeof(SchedulerStateMachine::BeginMainFrameState);
  849. for (size_t i = 0; i < num_begin_main_frame_states; ++i) {
  850. // There shouldn't be any drawing regardless of BeginImplFrame.
  851. for (size_t j = 0; j < 2; ++j) {
  852. StateMachine state(default_scheduler_settings);
  853. state.SetVisible(true);
  854. EXPECT_ACTION_UPDATE_STATE(
  855. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  856. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  857. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  858. state.SetBeginMainFrameState(begin_main_frame_states[i]);
  859. state.SetVisible(false);
  860. state.SetNeedsRedraw(true);
  861. if (j == 1)
  862. state.IssueNextBeginImplFrame();
  863. state.SetCanDraw(false);
  864. EXPECT_NE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE,
  865. state.NextAction());
  866. }
  867. }
  868. }
  869. TEST(SchedulerStateMachineTest,
  870. TestCanRedrawWithWaitingForFirstDrawMakesProgress) {
  871. SchedulerSettings default_scheduler_settings;
  872. StateMachine state(default_scheduler_settings);
  873. state.SetVisible(true);
  874. EXPECT_ACTION_UPDATE_STATE(
  875. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  876. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  877. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  878. state.SetActiveTreeNeedsFirstDraw(true);
  879. state.SetNeedsBeginMainFrame();
  880. state.SetNeedsRedraw(true);
  881. state.SetCanDraw(false);
  882. state.IssueNextBeginImplFrame();
  883. EXPECT_ACTION_UPDATE_STATE(
  884. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  885. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_ABORT);
  886. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  887. state.NotifyReadyToCommit();
  888. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  889. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  890. state.NotifyReadyToActivate();
  891. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  892. state.OnBeginImplFrameDeadline();
  893. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_ABORT);
  894. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  895. }
  896. TEST(SchedulerStateMachineTest, TestSetNeedsBeginMainFrameIsNotLost) {
  897. SchedulerSettings scheduler_settings;
  898. StateMachine state(scheduler_settings);
  899. SET_UP_STATE(state);
  900. state.SetNeedsBeginMainFrame();
  901. EXPECT_TRUE(state.BeginFrameNeeded());
  902. // Begin the frame.
  903. state.IssueNextBeginImplFrame();
  904. EXPECT_ACTION_UPDATE_STATE(
  905. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  906. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  907. // Now, while the frame is in progress, set another commit.
  908. state.SetNeedsBeginMainFrame();
  909. EXPECT_TRUE(state.NeedsCommit());
  910. // Let the frame finish.
  911. state.NotifyReadyToCommit();
  912. EXPECT_MAIN_FRAME_STATE(
  913. SchedulerStateMachine::BeginMainFrameState::READY_TO_COMMIT);
  914. // Expect to commit regardless of BeginImplFrame state.
  915. EXPECT_IMPL_FRAME_STATE(
  916. SchedulerStateMachine::BeginImplFrameState::INSIDE_BEGIN_FRAME);
  917. EXPECT_ACTION(SchedulerStateMachine::Action::COMMIT);
  918. state.OnBeginImplFrameDeadline();
  919. EXPECT_IMPL_FRAME_STATE(
  920. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE);
  921. EXPECT_ACTION(SchedulerStateMachine::Action::COMMIT);
  922. state.OnBeginImplFrameIdle();
  923. EXPECT_IMPL_FRAME_STATE(SchedulerStateMachine::BeginImplFrameState::IDLE);
  924. EXPECT_ACTION(SchedulerStateMachine::Action::COMMIT);
  925. state.IssueNextBeginImplFrame();
  926. EXPECT_IMPL_FRAME_STATE(
  927. SchedulerStateMachine::BeginImplFrameState::INSIDE_BEGIN_FRAME);
  928. EXPECT_ACTION(SchedulerStateMachine::Action::COMMIT);
  929. // Finish the commit and activate, then make sure we start the next commit
  930. // immediately and draw on the next BeginImplFrame.
  931. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  932. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  933. state.NotifyReadyToActivate();
  934. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  935. EXPECT_ACTION_UPDATE_STATE(
  936. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  937. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  938. state.OnBeginImplFrameDeadline();
  939. EXPECT_TRUE(state.active_tree_needs_first_draw());
  940. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  941. state.DidSubmitCompositorFrame();
  942. state.DidReceiveCompositorFrameAck();
  943. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  944. }
  945. TEST(SchedulerStateMachineTest, TestFullCycle) {
  946. SchedulerSettings default_scheduler_settings;
  947. StateMachine state(default_scheduler_settings);
  948. SET_UP_STATE(state);
  949. // Start clean and set commit.
  950. state.SetNeedsBeginMainFrame();
  951. // Begin the frame.
  952. state.IssueNextBeginImplFrame();
  953. EXPECT_ACTION_UPDATE_STATE(
  954. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  955. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  956. EXPECT_FALSE(state.NeedsCommit());
  957. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  958. // Tell the scheduler the frame finished.
  959. state.NotifyReadyToCommit();
  960. EXPECT_MAIN_FRAME_STATE(
  961. SchedulerStateMachine::BeginMainFrameState::READY_TO_COMMIT);
  962. // Commit.
  963. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  964. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  965. // Activate.
  966. state.NotifyReadyToActivate();
  967. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  968. EXPECT_TRUE(state.active_tree_needs_first_draw());
  969. EXPECT_TRUE(state.needs_redraw());
  970. // Expect to do nothing until BeginImplFrame deadline
  971. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  972. // At BeginImplFrame deadline, draw.
  973. state.OnBeginImplFrameDeadline();
  974. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  975. state.DidSubmitCompositorFrame();
  976. state.DidReceiveCompositorFrameAck();
  977. // Should be synchronized, no draw needed, no action needed.
  978. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  979. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  980. EXPECT_FALSE(state.needs_redraw());
  981. }
  982. TEST(SchedulerStateMachineTest, CommitWithoutDrawWithPendingTree) {
  983. SchedulerSettings default_scheduler_settings;
  984. StateMachine state(default_scheduler_settings);
  985. SET_UP_STATE(state);
  986. // Start clean and set commit.
  987. state.SetNeedsBeginMainFrame();
  988. // Make a main frame, commit and activate it. But don't draw it.
  989. state.IssueNextBeginImplFrame();
  990. EXPECT_ACTION_UPDATE_STATE(
  991. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  992. state.NotifyReadyToCommit();
  993. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  994. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  995. state.NotifyReadyToActivate();
  996. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  997. // Try to make a new main frame before drawing. Since we will commit it to a
  998. // pending tree and not clobber the active tree, we're able to start a new
  999. // begin frame and commit it.
  1000. state.SetNeedsBeginMainFrame();
  1001. state.IssueNextBeginImplFrame();
  1002. EXPECT_ACTION_UPDATE_STATE(
  1003. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1004. state.NotifyReadyToCommit();
  1005. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1006. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1007. }
  1008. TEST(SchedulerStateMachineTest, DontCommitWithoutDrawWithoutPendingTree) {
  1009. SchedulerSettings scheduler_settings;
  1010. scheduler_settings.commit_to_active_tree = true;
  1011. scheduler_settings.main_frame_before_activation_enabled = false;
  1012. StateMachine state(scheduler_settings);
  1013. SET_UP_STATE(state);
  1014. // Start clean and set commit.
  1015. state.SetNeedsBeginMainFrame();
  1016. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 10);
  1017. // Make a main frame, commit and activate it. But don't draw it.
  1018. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  1019. EXPECT_ACTION_UPDATE_STATE(
  1020. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1021. state.NotifyReadyToCommit();
  1022. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1023. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1024. state.NotifyReadyToActivate();
  1025. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1026. // Try to make a new main frame before drawing, but since we would clobber the
  1027. // active tree, we will not do so.
  1028. state.SetNeedsBeginMainFrame();
  1029. frame_id.sequence_number++;
  1030. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  1031. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1032. }
  1033. TEST(SchedulerStateMachineTest, AbortedMainFrameDoesNotResetPendingTree) {
  1034. SchedulerSettings scheduler_settings;
  1035. scheduler_settings.main_frame_before_activation_enabled = true;
  1036. StateMachine state(scheduler_settings);
  1037. SET_UP_STATE(state);
  1038. // Perform a commit so that we have an active tree.
  1039. state.SetNeedsBeginMainFrame();
  1040. state.IssueNextBeginImplFrame();
  1041. EXPECT_ACTION_UPDATE_STATE(
  1042. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1043. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1044. state.NotifyReadyToCommit();
  1045. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1046. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1047. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1048. EXPECT_TRUE(state.has_pending_tree());
  1049. state.OnBeginImplFrameDeadline();
  1050. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1051. // Ask for another commit but abort it. Verify that we didn't reset pending
  1052. // tree state.
  1053. state.SetNeedsBeginMainFrame();
  1054. state.IssueNextBeginImplFrame();
  1055. EXPECT_ACTION_UPDATE_STATE(
  1056. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1057. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1058. EXPECT_TRUE(state.has_pending_tree());
  1059. state.BeginMainFrameAborted(CommitEarlyOutReason::FINISHED_NO_UPDATES);
  1060. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1061. EXPECT_TRUE(state.has_pending_tree());
  1062. state.OnBeginImplFrameDeadline();
  1063. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1064. // Ask for another commit that doesn't abort.
  1065. state.SetNeedsBeginMainFrame();
  1066. state.IssueNextBeginImplFrame();
  1067. EXPECT_ACTION_UPDATE_STATE(
  1068. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1069. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1070. state.NotifyReadyToCommit();
  1071. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1072. EXPECT_TRUE(state.has_pending_tree());
  1073. // Verify that commit is delayed until the pending tree is activated.
  1074. state.NotifyReadyToActivate();
  1075. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1076. EXPECT_FALSE(state.has_pending_tree());
  1077. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1078. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1079. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1080. EXPECT_TRUE(state.has_pending_tree());
  1081. }
  1082. TEST(SchedulerStateMachineTest, TestFullCycleWithCommitToActive) {
  1083. SchedulerSettings scheduler_settings;
  1084. scheduler_settings.commit_to_active_tree = true;
  1085. scheduler_settings.main_frame_before_activation_enabled = false;
  1086. StateMachine state(scheduler_settings);
  1087. SET_UP_STATE(state);
  1088. // Start clean and set commit.
  1089. state.SetNeedsBeginMainFrame();
  1090. // Begin the frame.
  1091. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 10);
  1092. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  1093. EXPECT_ACTION_UPDATE_STATE(
  1094. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1095. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  1096. EXPECT_FALSE(state.NeedsCommit());
  1097. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1098. // Tell the scheduler the frame finished.
  1099. state.NotifyReadyToCommit();
  1100. EXPECT_MAIN_FRAME_STATE(
  1101. SchedulerStateMachine::BeginMainFrameState::READY_TO_COMMIT);
  1102. // Commit.
  1103. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1104. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1105. // Commit always calls NotifyReadyToActivate in this mode.
  1106. state.NotifyReadyToActivate();
  1107. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1108. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1109. // No draw because we haven't received NotifyReadyToDraw yet.
  1110. state.OnBeginImplFrameDeadline();
  1111. EXPECT_TRUE(state.active_tree_needs_first_draw());
  1112. EXPECT_TRUE(state.needs_redraw());
  1113. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1114. // Can't BeginMainFrame yet since last commit hasn't been drawn yet.
  1115. state.SetNeedsBeginMainFrame();
  1116. frame_id.sequence_number++;
  1117. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  1118. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1119. // Now call ready to draw which will allow the draw to happen and
  1120. // BeginMainFrame to be sent.
  1121. state.NotifyReadyToDraw();
  1122. state.OnBeginImplFrameDeadline();
  1123. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1124. // Submit throttled from this point.
  1125. state.DidSubmitCompositorFrame();
  1126. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1127. // Can't BeginMainFrame yet since we're submit-frame throttled.
  1128. frame_id.sequence_number++;
  1129. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  1130. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1131. // CompositorFrameAck unblocks BeginMainFrame.
  1132. state.DidReceiveCompositorFrameAck();
  1133. EXPECT_ACTION_UPDATE_STATE(
  1134. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1135. state.NotifyReadyToCommit();
  1136. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1137. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1138. state.NotifyReadyToActivate();
  1139. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1140. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1141. // Draw the newly activated tree.
  1142. state.NotifyReadyToDraw();
  1143. state.OnBeginImplFrameDeadline();
  1144. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1145. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1146. // When commits are deferred, we don't block the deadline.
  1147. state.SetDeferBeginMainFrame(true);
  1148. frame_id.sequence_number++;
  1149. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  1150. EXPECT_NE(SchedulerStateMachine::BeginImplFrameDeadlineMode::BLOCKED,
  1151. state.CurrentBeginImplFrameDeadlineMode());
  1152. }
  1153. TEST(SchedulerStateMachineTest, TestFullCycleWithCommitRequestInbetween) {
  1154. SchedulerSettings default_scheduler_settings;
  1155. StateMachine state(default_scheduler_settings);
  1156. SET_UP_STATE(state);
  1157. // Start clean and set commit.
  1158. state.SetNeedsBeginMainFrame();
  1159. // Begin the frame.
  1160. state.IssueNextBeginImplFrame();
  1161. EXPECT_ACTION_UPDATE_STATE(
  1162. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1163. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  1164. EXPECT_FALSE(state.NeedsCommit());
  1165. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1166. // Request another commit while the commit is in flight.
  1167. state.SetNeedsBeginMainFrame();
  1168. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1169. // Tell the scheduler the frame finished.
  1170. state.NotifyReadyToCommit();
  1171. EXPECT_MAIN_FRAME_STATE(
  1172. SchedulerStateMachine::BeginMainFrameState::READY_TO_COMMIT);
  1173. // First commit and activate.
  1174. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1175. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1176. state.NotifyReadyToActivate();
  1177. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1178. EXPECT_TRUE(state.active_tree_needs_first_draw());
  1179. EXPECT_TRUE(state.needs_redraw());
  1180. // Expect to do nothing until BeginImplFrame deadline.
  1181. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1182. // At BeginImplFrame deadline, draw.
  1183. state.OnBeginImplFrameDeadline();
  1184. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1185. state.DidSubmitCompositorFrame();
  1186. state.DidReceiveCompositorFrameAck();
  1187. // Should be synchronized, no draw needed, no action needed.
  1188. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1189. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  1190. EXPECT_FALSE(state.needs_redraw());
  1191. // Next BeginImplFrame should initiate second commit.
  1192. state.IssueNextBeginImplFrame();
  1193. EXPECT_ACTION_UPDATE_STATE(
  1194. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1195. }
  1196. TEST(SchedulerStateMachineTest, TestNoRequestCommitWhenInvisible) {
  1197. SchedulerSettings default_scheduler_settings;
  1198. StateMachine state(default_scheduler_settings);
  1199. state.SetVisible(true);
  1200. EXPECT_ACTION_UPDATE_STATE(
  1201. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1202. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1203. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1204. state.SetVisible(false);
  1205. state.SetNeedsBeginMainFrame();
  1206. EXPECT_FALSE(state.CouldSendBeginMainFrame());
  1207. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1208. }
  1209. TEST(SchedulerStateMachineTest, TestNoRequestCommitWhenBeginFrameSourcePaused) {
  1210. SchedulerSettings default_scheduler_settings;
  1211. StateMachine state(default_scheduler_settings);
  1212. state.SetVisible(true);
  1213. EXPECT_ACTION_UPDATE_STATE(
  1214. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1215. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1216. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1217. state.SetBeginFrameSourcePaused(true);
  1218. state.SetNeedsBeginMainFrame();
  1219. EXPECT_FALSE(state.CouldSendBeginMainFrame());
  1220. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1221. }
  1222. TEST(SchedulerStateMachineTest, TestNoRequestLayerTreeFrameSinkWhenInvisible) {
  1223. SchedulerSettings default_scheduler_settings;
  1224. StateMachine state(default_scheduler_settings);
  1225. // We should not request a LayerTreeFrameSink when we are still invisible.
  1226. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1227. state.SetVisible(true);
  1228. EXPECT_ACTION_UPDATE_STATE(
  1229. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1230. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1231. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1232. state.SetVisible(false);
  1233. state.DidLoseLayerTreeFrameSink();
  1234. state.SetNeedsBeginMainFrame();
  1235. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1236. state.SetVisible(true);
  1237. EXPECT_ACTION_UPDATE_STATE(
  1238. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1239. }
  1240. // See ProxyMain::BeginMainFrame "EarlyOut_NotVisible" /
  1241. // "EarlyOut_LayerTreeFrameSinkLost" cases.
  1242. TEST(SchedulerStateMachineTest, TestAbortBeginMainFrameBecauseInvisible) {
  1243. SchedulerSettings default_scheduler_settings;
  1244. StateMachine state(default_scheduler_settings);
  1245. SET_UP_STATE(state);
  1246. // Start clean and set commit.
  1247. state.SetNeedsBeginMainFrame();
  1248. // Begin the frame while visible.
  1249. state.IssueNextBeginImplFrame();
  1250. EXPECT_ACTION_UPDATE_STATE(
  1251. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1252. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  1253. EXPECT_FALSE(state.NeedsCommit());
  1254. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1255. // Become invisible and abort BeginMainFrame.
  1256. state.SetVisible(false);
  1257. state.BeginMainFrameAborted(CommitEarlyOutReason::ABORTED_NOT_VISIBLE);
  1258. // NeedsCommit should now be true again because we never actually did a
  1259. // commit.
  1260. EXPECT_TRUE(state.NeedsCommit());
  1261. // We should now be back in the idle state as if we never started the frame.
  1262. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  1263. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1264. // We shouldn't do anything on the BeginImplFrame deadline.
  1265. state.OnBeginImplFrameDeadline();
  1266. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1267. // Become visible again.
  1268. state.SetVisible(true);
  1269. // Although we have aborted on this frame and haven't cancelled the commit
  1270. // (i.e. need another), don't send another BeginMainFrame yet.
  1271. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  1272. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1273. EXPECT_TRUE(state.NeedsCommit());
  1274. // Start a new frame.
  1275. state.IssueNextBeginImplFrame();
  1276. EXPECT_ACTION_UPDATE_STATE(
  1277. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1278. // We should be starting the commit now.
  1279. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  1280. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1281. }
  1282. // See ProxyMain::BeginMainFrame "EarlyOut_NoUpdates" case.
  1283. TEST(SchedulerStateMachineTest, TestAbortBeginMainFrameBecauseCommitNotNeeded) {
  1284. SchedulerSettings default_scheduler_settings;
  1285. StateMachine state(default_scheduler_settings);
  1286. state.SetVisible(true);
  1287. EXPECT_ACTION_UPDATE_STATE(
  1288. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1289. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1290. state.DidCreateAndInitializeLayerTreeFrameSink();
  1291. state.SetCanDraw(true);
  1292. // Get into a begin frame / commit state.
  1293. state.SetNeedsBeginMainFrame();
  1294. state.IssueNextBeginImplFrame();
  1295. EXPECT_ACTION_UPDATE_STATE(
  1296. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1297. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  1298. EXPECT_FALSE(state.NeedsCommit());
  1299. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1300. // Abort the commit, true means that the BeginMainFrame was sent but there
  1301. // was no work to do on the main thread.
  1302. state.BeginMainFrameAborted(CommitEarlyOutReason::FINISHED_NO_UPDATES);
  1303. // NeedsCommit should now be false because the commit was actually handled.
  1304. EXPECT_FALSE(state.NeedsCommit());
  1305. // Since the commit was aborted, we don't need to try and draw.
  1306. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1307. state.OnBeginImplFrameDeadline();
  1308. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1309. // Verify another commit doesn't start on another frame either.
  1310. EXPECT_FALSE(state.NeedsCommit());
  1311. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  1312. state.IssueNextBeginImplFrame();
  1313. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1314. state.OnBeginImplFrameDeadline();
  1315. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1316. // Verify another commit can start if requested, though.
  1317. state.SetNeedsBeginMainFrame();
  1318. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  1319. state.IssueNextBeginImplFrame();
  1320. EXPECT_ACTION(SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1321. }
  1322. TEST(SchedulerStateMachineTest, TestFirstContextCreation) {
  1323. SchedulerSettings default_scheduler_settings;
  1324. StateMachine state(default_scheduler_settings);
  1325. state.SetVisible(true);
  1326. state.SetCanDraw(true);
  1327. EXPECT_ACTION_UPDATE_STATE(
  1328. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1329. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1330. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1331. // Check that the first init does not SetNeedsBeginMainFrame.
  1332. state.IssueNextBeginImplFrame();
  1333. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1334. state.OnBeginImplFrameDeadline();
  1335. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1336. // Check that a needs commit initiates a BeginMainFrame.
  1337. state.SetNeedsBeginMainFrame();
  1338. state.IssueNextBeginImplFrame();
  1339. EXPECT_ACTION_UPDATE_STATE(
  1340. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1341. }
  1342. TEST(SchedulerStateMachineTest, TestContextLostWhenCompletelyIdle) {
  1343. SchedulerSettings default_scheduler_settings;
  1344. StateMachine state(default_scheduler_settings);
  1345. SET_UP_STATE(state);
  1346. EXPECT_NE(SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION,
  1347. state.NextAction());
  1348. state.DidLoseLayerTreeFrameSink();
  1349. EXPECT_ACTION_UPDATE_STATE(
  1350. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1351. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1352. // Once context recreation begins, nothing should happen.
  1353. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1354. // Recreate the context.
  1355. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1356. // When the context is recreated, we should begin a commit.
  1357. state.IssueNextBeginImplFrame();
  1358. EXPECT_ACTION_UPDATE_STATE(
  1359. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1360. }
  1361. TEST(SchedulerStateMachineTest,
  1362. TestContextLostWhenIdleAndCommitRequestedWhileRecreating) {
  1363. SchedulerSettings default_scheduler_settings;
  1364. StateMachine state(default_scheduler_settings);
  1365. SET_UP_STATE(state);
  1366. EXPECT_NE(SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION,
  1367. state.NextAction());
  1368. state.DidLoseLayerTreeFrameSink();
  1369. EXPECT_EQ(state.layer_tree_frame_sink_state(),
  1370. SchedulerStateMachine::LayerTreeFrameSinkState::NONE);
  1371. EXPECT_ACTION_UPDATE_STATE(
  1372. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1373. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1374. // Once context recreation begins, nothing should happen.
  1375. state.IssueNextBeginImplFrame();
  1376. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1377. state.OnBeginImplFrameDeadline();
  1378. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1379. // While context is recreating, commits shouldn't begin.
  1380. state.SetNeedsBeginMainFrame();
  1381. state.IssueNextBeginImplFrame();
  1382. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1383. state.OnBeginImplFrameDeadline();
  1384. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1385. // Recreate the context
  1386. state.DidCreateAndInitializeLayerTreeFrameSink();
  1387. EXPECT_EQ(
  1388. state.layer_tree_frame_sink_state(),
  1389. SchedulerStateMachine::LayerTreeFrameSinkState::WAITING_FOR_FIRST_COMMIT);
  1390. EXPECT_FALSE(state.RedrawPending());
  1391. // When the context is recreated, we wait until the next BeginImplFrame
  1392. // before starting.
  1393. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1394. // When the BeginFrame comes in we should begin a commit
  1395. state.IssueNextBeginImplFrame();
  1396. EXPECT_ACTION_UPDATE_STATE(
  1397. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1398. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1399. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  1400. // Until that commit finishes, we shouldn't be drawing.
  1401. state.OnBeginImplFrameDeadline();
  1402. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1403. // Finish the commit, which should make the surface active.
  1404. state.NotifyReadyToCommit();
  1405. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1406. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1407. EXPECT_EQ(state.layer_tree_frame_sink_state(),
  1408. SchedulerStateMachine::LayerTreeFrameSinkState::
  1409. WAITING_FOR_FIRST_ACTIVATION);
  1410. state.NotifyReadyToActivate();
  1411. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1412. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1413. EXPECT_EQ(state.layer_tree_frame_sink_state(),
  1414. SchedulerStateMachine::LayerTreeFrameSinkState::ACTIVE);
  1415. // Finishing the first commit after initializing a LayerTreeFrameSink should
  1416. // automatically cause a redraw.
  1417. EXPECT_TRUE(state.RedrawPending());
  1418. state.IssueNextBeginImplFrame();
  1419. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1420. state.OnBeginImplFrameDeadline();
  1421. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1422. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1423. EXPECT_FALSE(state.RedrawPending());
  1424. // Next frame as no work to do.
  1425. state.IssueNextBeginImplFrame();
  1426. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1427. state.OnBeginImplFrameDeadline();
  1428. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1429. // Once the context is recreated, whether we draw should be based on
  1430. // SetCanDraw if waiting on first draw after activate.
  1431. state.SetNeedsRedraw(true);
  1432. state.IssueNextBeginImplFrame();
  1433. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1434. state.OnBeginImplFrameDeadline();
  1435. EXPECT_ACTION(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1436. state.SetCanDraw(false);
  1437. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1438. state.SetCanDraw(true);
  1439. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1440. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1441. // Once the context is recreated, whether we draw should be based on
  1442. // SetCanDraw if waiting on first draw after activate.
  1443. state.SetNeedsRedraw(true);
  1444. state.SetNeedsBeginMainFrame();
  1445. state.IssueNextBeginImplFrame();
  1446. EXPECT_ACTION_UPDATE_STATE(
  1447. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1448. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1449. // Activate so we need the first draw
  1450. state.NotifyReadyToCommit();
  1451. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1452. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1453. state.NotifyReadyToActivate();
  1454. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1455. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1456. EXPECT_TRUE(state.active_tree_needs_first_draw());
  1457. EXPECT_TRUE(state.needs_redraw());
  1458. state.OnBeginImplFrameDeadline();
  1459. EXPECT_ACTION(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1460. state.SetCanDraw(false);
  1461. EXPECT_ACTION(SchedulerStateMachine::Action::DRAW_ABORT);
  1462. state.SetCanDraw(true);
  1463. EXPECT_ACTION(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1464. }
  1465. TEST(SchedulerStateMachineTest, TestContextLostWhileCommitInProgress) {
  1466. SchedulerSettings scheduler_settings;
  1467. StateMachine state(scheduler_settings);
  1468. SET_UP_STATE(state);
  1469. // Get a commit in flight.
  1470. state.SetNeedsBeginMainFrame();
  1471. // Set damage and expect a draw.
  1472. state.SetNeedsRedraw(true);
  1473. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 10);
  1474. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  1475. EXPECT_ACTION_UPDATE_STATE(
  1476. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1477. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1478. state.OnBeginImplFrameDeadline();
  1479. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1480. state.DidSubmitCompositorFrame();
  1481. state.DidReceiveCompositorFrameAck();
  1482. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1483. // Cause a lost context while the BeginMainFrame is in flight.
  1484. state.DidLoseLayerTreeFrameSink();
  1485. EXPECT_FALSE(state.BeginFrameNeeded());
  1486. // Ask for another draw. Expect nothing happens.
  1487. state.SetNeedsRedraw(true);
  1488. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1489. // Finish the frame, commit and activate.
  1490. state.NotifyReadyToCommit();
  1491. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1492. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1493. state.NotifyReadyToActivate();
  1494. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1495. // We will abort the draw when the LayerTreeFrameSink is lost if we are
  1496. // waiting for the first draw to unblock the main thread.
  1497. EXPECT_TRUE(state.active_tree_needs_first_draw());
  1498. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_ABORT);
  1499. // Expect to begin context recreation only in BeginImplFrameState::IDLE
  1500. EXPECT_IMPL_FRAME_STATE(SchedulerStateMachine::BeginImplFrameState::IDLE);
  1501. EXPECT_ACTION(
  1502. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1503. frame_id.sequence_number++;
  1504. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  1505. EXPECT_IMPL_FRAME_STATE(
  1506. SchedulerStateMachine::BeginImplFrameState::INSIDE_BEGIN_FRAME);
  1507. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1508. state.OnBeginImplFrameDeadline();
  1509. EXPECT_IMPL_FRAME_STATE(
  1510. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE);
  1511. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1512. }
  1513. TEST(SchedulerStateMachineTest,
  1514. TestContextLostWhileCommitInProgressAndAnotherCommitRequested) {
  1515. SchedulerSettings scheduler_settings;
  1516. StateMachine state(scheduler_settings);
  1517. SET_UP_STATE(state);
  1518. // Get a commit in flight.
  1519. state.SetNeedsBeginMainFrame();
  1520. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1521. // Set damage and expect a draw.
  1522. state.SetNeedsRedraw(true);
  1523. state.IssueNextBeginImplFrame();
  1524. EXPECT_ACTION_UPDATE_STATE(
  1525. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1526. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1527. state.OnBeginImplFrameDeadline();
  1528. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1529. state.DidSubmitCompositorFrame();
  1530. state.DidReceiveCompositorFrameAck();
  1531. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1532. // Cause a lost context while the BeginMainFrame is in flight.
  1533. state.DidLoseLayerTreeFrameSink();
  1534. // Ask for another draw and also set needs commit. Expect nothing happens.
  1535. state.SetNeedsRedraw(true);
  1536. state.SetNeedsBeginMainFrame();
  1537. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1538. // Finish the frame, and commit and activate.
  1539. state.NotifyReadyToCommit();
  1540. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1541. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1542. state.NotifyReadyToActivate();
  1543. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1544. EXPECT_TRUE(state.active_tree_needs_first_draw());
  1545. // Because the LayerTreeFrameSink is missing, we expect the draw to abort.
  1546. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_ABORT);
  1547. // Expect to begin context recreation only in BeginImplFrameState::IDLE
  1548. EXPECT_IMPL_FRAME_STATE(SchedulerStateMachine::BeginImplFrameState::IDLE);
  1549. EXPECT_ACTION(
  1550. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1551. state.IssueNextBeginImplFrame();
  1552. EXPECT_IMPL_FRAME_STATE(
  1553. SchedulerStateMachine::BeginImplFrameState::INSIDE_BEGIN_FRAME);
  1554. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1555. state.OnBeginImplFrameDeadline();
  1556. EXPECT_IMPL_FRAME_STATE(
  1557. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE);
  1558. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1559. state.OnBeginImplFrameIdle();
  1560. EXPECT_ACTION_UPDATE_STATE(
  1561. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1562. // After we get a new LayerTreeFrameSink, the commit flow should start.
  1563. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1564. state.IssueNextBeginImplFrame();
  1565. EXPECT_ACTION_UPDATE_STATE(
  1566. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1567. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1568. state.NotifyReadyToCommit();
  1569. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1570. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1571. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1572. state.NotifyReadyToActivate();
  1573. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1574. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1575. state.OnBeginImplFrameDeadline();
  1576. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1577. state.DidSubmitCompositorFrame();
  1578. state.DidReceiveCompositorFrameAck();
  1579. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1580. }
  1581. TEST(SchedulerStateMachineTest,
  1582. DontDrawBeforeCommitAfterLostLayerTreeFrameSink) {
  1583. SchedulerSettings default_scheduler_settings;
  1584. StateMachine state(default_scheduler_settings);
  1585. SET_UP_STATE(state);
  1586. state.SetNeedsRedraw(true);
  1587. // Cause a lost LayerTreeFrameSink, and restore it.
  1588. state.DidLoseLayerTreeFrameSink();
  1589. EXPECT_ACTION_UPDATE_STATE(
  1590. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1591. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1592. state.DidCreateAndInitializeLayerTreeFrameSink();
  1593. EXPECT_FALSE(state.RedrawPending());
  1594. state.IssueNextBeginImplFrame();
  1595. EXPECT_ACTION(SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1596. }
  1597. TEST(SchedulerStateMachineTest,
  1598. TestShouldAbortCurrentFrameAfterLostLayerTreeFrameSink) {
  1599. SchedulerSettings default_scheduler_settings;
  1600. StateMachine state(default_scheduler_settings);
  1601. SET_UP_STATE(state);
  1602. state.SetBeginMainFrameState(
  1603. SchedulerStateMachine::BeginMainFrameState::SENT);
  1604. // Cause a lost context.
  1605. state.DidLoseLayerTreeFrameSink();
  1606. state.NotifyReadyToCommit();
  1607. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1608. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1609. EXPECT_TRUE(state.ShouldAbortCurrentFrame());
  1610. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1611. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1612. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_ABORT);
  1613. }
  1614. TEST(SchedulerStateMachineTest, TestNoBeginFrameNeededWhenInvisible) {
  1615. SchedulerSettings default_scheduler_settings;
  1616. StateMachine state(default_scheduler_settings);
  1617. state.SetVisible(true);
  1618. EXPECT_ACTION_UPDATE_STATE(
  1619. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1620. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1621. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1622. EXPECT_FALSE(state.BeginFrameNeeded());
  1623. state.SetNeedsRedraw(true);
  1624. EXPECT_TRUE(state.BeginFrameNeeded());
  1625. state.SetVisible(false);
  1626. EXPECT_FALSE(state.BeginFrameNeeded());
  1627. state.SetVisible(true);
  1628. EXPECT_TRUE(state.BeginFrameNeeded());
  1629. }
  1630. TEST(SchedulerStateMachineTest, TestNoBeginMainFrameWhenInvisible) {
  1631. SchedulerSettings default_scheduler_settings;
  1632. StateMachine state(default_scheduler_settings);
  1633. state.SetVisible(true);
  1634. EXPECT_ACTION_UPDATE_STATE(
  1635. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1636. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1637. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1638. state.SetVisible(false);
  1639. state.SetNeedsBeginMainFrame();
  1640. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1641. EXPECT_FALSE(state.BeginFrameNeeded());
  1642. // When become visible again, the needs commit should still be pending.
  1643. state.SetVisible(true);
  1644. EXPECT_TRUE(state.BeginFrameNeeded());
  1645. state.IssueNextBeginImplFrame();
  1646. EXPECT_ACTION_UPDATE_STATE(
  1647. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1648. }
  1649. TEST(SchedulerStateMachineTest, TestFinishCommitWhenCommitInProgress) {
  1650. SchedulerSettings default_scheduler_settings;
  1651. StateMachine state(default_scheduler_settings);
  1652. state.SetVisible(true);
  1653. EXPECT_ACTION_UPDATE_STATE(
  1654. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1655. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1656. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1657. state.SetVisible(false);
  1658. state.SetBeginMainFrameState(
  1659. SchedulerStateMachine::BeginMainFrameState::SENT);
  1660. state.SetNeedsBeginMainFrame();
  1661. // After the commit completes, activation and draw happen immediately
  1662. // because we are not visible.
  1663. state.NotifyReadyToCommit();
  1664. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1665. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1666. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1667. EXPECT_TRUE(state.active_tree_needs_first_draw());
  1668. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_ABORT);
  1669. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1670. }
  1671. TEST(SchedulerStateMachineTest,
  1672. TestFinishCommitWhenCommitInProgressAndBeginFrameSourcePaused) {
  1673. SchedulerSettings default_scheduler_settings;
  1674. StateMachine state(default_scheduler_settings);
  1675. state.SetVisible(true);
  1676. EXPECT_ACTION_UPDATE_STATE(
  1677. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1678. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1679. state.CreateAndInitializeLayerTreeFrameSinkWithActivatedCommit();
  1680. state.SetBeginFrameSourcePaused(true);
  1681. state.SetBeginMainFrameState(
  1682. SchedulerStateMachine::BeginMainFrameState::SENT);
  1683. state.SetNeedsBeginMainFrame();
  1684. // After the commit completes, activation and draw happen immediately
  1685. // because we are not visible.
  1686. state.NotifyReadyToCommit();
  1687. EXPECT_TRUE(state.ShouldAbortCurrentFrame());
  1688. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1689. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1690. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1691. EXPECT_TRUE(state.active_tree_needs_first_draw());
  1692. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_ABORT);
  1693. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1694. }
  1695. TEST(SchedulerStateMachineTest, TestInitialActionsWhenContextLost) {
  1696. SchedulerSettings default_scheduler_settings;
  1697. StateMachine state(default_scheduler_settings);
  1698. SET_UP_STATE(state);
  1699. state.SetNeedsBeginMainFrame();
  1700. state.DidLoseLayerTreeFrameSink();
  1701. // When we are visible, we normally want to begin LayerTreeFrameSink creation
  1702. // as soon as possible.
  1703. EXPECT_ACTION_UPDATE_STATE(
  1704. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1705. state.DidCreateAndInitializeLayerTreeFrameSink();
  1706. EXPECT_EQ(
  1707. state.layer_tree_frame_sink_state(),
  1708. SchedulerStateMachine::LayerTreeFrameSinkState::WAITING_FOR_FIRST_COMMIT);
  1709. // We should not send a BeginMainFrame when we are invisible, even if we've
  1710. // lost the LayerTreeFrameSink and are trying to get the first commit, since
  1711. // the
  1712. // main thread will just abort anyway.
  1713. state.SetVisible(false);
  1714. EXPECT_ACTION(SchedulerStateMachine::Action::NONE);
  1715. }
  1716. TEST(SchedulerStateMachineTest, ReportIfNotDrawing) {
  1717. SchedulerSettings default_scheduler_settings;
  1718. StateMachine state(default_scheduler_settings);
  1719. SET_UP_STATE(state);
  1720. EXPECT_FALSE(state.PendingDrawsShouldBeAborted());
  1721. state.SetCanDraw(false);
  1722. state.SetVisible(true);
  1723. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1724. state.SetCanDraw(true);
  1725. state.SetVisible(false);
  1726. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1727. state.SetCanDraw(false);
  1728. state.SetVisible(false);
  1729. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1730. state.SetCanDraw(true);
  1731. state.SetVisible(true);
  1732. state.SetBeginFrameSourcePaused(true);
  1733. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1734. state.SetBeginFrameSourcePaused(false);
  1735. EXPECT_FALSE(state.PendingDrawsShouldBeAborted());
  1736. }
  1737. TEST(SchedulerStateMachineTest, ForceDrawForResourcelessSoftwareDraw) {
  1738. SchedulerSettings default_scheduler_settings;
  1739. StateMachine state(default_scheduler_settings);
  1740. SET_UP_STATE(state);
  1741. state.SetResourcelessSoftwareDraw(true);
  1742. EXPECT_FALSE(state.PendingDrawsShouldBeAborted());
  1743. state.SetVisible(false);
  1744. EXPECT_FALSE(state.PendingDrawsShouldBeAborted());
  1745. state.SetResourcelessSoftwareDraw(false);
  1746. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1747. state.SetResourcelessSoftwareDraw(true);
  1748. EXPECT_FALSE(state.PendingDrawsShouldBeAborted());
  1749. state.SetVisible(true);
  1750. state.SetBeginFrameSourcePaused(true);
  1751. EXPECT_FALSE(state.PendingDrawsShouldBeAborted());
  1752. state.SetResourcelessSoftwareDraw(false);
  1753. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1754. state.SetResourcelessSoftwareDraw(true);
  1755. EXPECT_FALSE(state.PendingDrawsShouldBeAborted());
  1756. state.SetBeginFrameSourcePaused(false);
  1757. state.SetVisible(false);
  1758. state.DidLoseLayerTreeFrameSink();
  1759. state.SetCanDraw(false);
  1760. state.WillBeginLayerTreeFrameSinkCreation();
  1761. state.DidCreateAndInitializeLayerTreeFrameSink();
  1762. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1763. state.SetCanDraw(true);
  1764. state.DidLoseLayerTreeFrameSink();
  1765. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1766. state.SetCanDraw(true);
  1767. state.WillBeginLayerTreeFrameSinkCreation();
  1768. state.DidCreateAndInitializeLayerTreeFrameSink();
  1769. EXPECT_FALSE(state.PendingDrawsShouldBeAborted());
  1770. state.SetCanDraw(false);
  1771. state.DidLoseLayerTreeFrameSink();
  1772. EXPECT_TRUE(state.PendingDrawsShouldBeAborted());
  1773. }
  1774. TEST(SchedulerStateMachineTest,
  1775. TestTriggerDeadlineImmediatelyAfterAbortedCommit) {
  1776. SchedulerSettings default_scheduler_settings;
  1777. StateMachine state(default_scheduler_settings);
  1778. SET_UP_STATE(state);
  1779. // This test mirrors what happens during the first frame of a scroll gesture.
  1780. // First we get the input event and a BeginFrame.
  1781. state.IssueNextBeginImplFrame();
  1782. // As a response the compositor requests a redraw and a commit to tell the
  1783. // main thread about the new scroll offset.
  1784. state.SetNeedsRedraw(true);
  1785. state.SetNeedsBeginMainFrame();
  1786. // We should start the commit normally.
  1787. EXPECT_ACTION_UPDATE_STATE(
  1788. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1789. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1790. // Since only the scroll offset changed, the main thread will abort the
  1791. // commit.
  1792. state.BeginMainFrameAborted(CommitEarlyOutReason::FINISHED_NO_UPDATES);
  1793. // Since the commit was aborted, we should draw right away instead of waiting
  1794. // for the deadline.
  1795. EXPECT_TRUE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1796. }
  1797. void FinishPreviousCommitAndDrawWithoutExitingDeadline(
  1798. StateMachine* state_ptr) {
  1799. // Gross, but allows us to use macros below.
  1800. StateMachine& state = *state_ptr;
  1801. state.NotifyReadyToCommit();
  1802. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  1803. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  1804. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1805. state.NotifyReadyToActivate();
  1806. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  1807. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1808. state.IssueNextBeginImplFrame();
  1809. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1810. EXPECT_TRUE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1811. state.OnBeginImplFrameDeadline();
  1812. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1813. state.DidSubmitCompositorFrame();
  1814. }
  1815. TEST(SchedulerStateMachineTest, TestImplLatencyTakesPriority) {
  1816. SchedulerSettings default_scheduler_settings;
  1817. StateMachine state(default_scheduler_settings);
  1818. SET_UP_STATE(state);
  1819. // This test ensures that impl-draws are prioritized over main thread updates
  1820. // in prefer impl latency mode.
  1821. state.SetNeedsRedraw(true);
  1822. state.SetNeedsBeginMainFrame();
  1823. state.IssueNextBeginImplFrame();
  1824. EXPECT_ACTION_UPDATE_STATE(
  1825. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1826. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1827. // Verify the deadline is not triggered early until we enter
  1828. // prefer impl latency mode.
  1829. EXPECT_FALSE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1830. state.SetTreePrioritiesAndScrollState(
  1831. SMOOTHNESS_TAKES_PRIORITY,
  1832. ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER);
  1833. EXPECT_TRUE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1834. // Trigger the deadline.
  1835. state.OnBeginImplFrameDeadline();
  1836. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  1837. state.DidSubmitCompositorFrame();
  1838. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1839. state.DidReceiveCompositorFrameAck();
  1840. // Request a new commit and finish the previous one.
  1841. state.SetNeedsBeginMainFrame();
  1842. FinishPreviousCommitAndDrawWithoutExitingDeadline(&state);
  1843. EXPECT_ACTION_UPDATE_STATE(
  1844. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1845. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1846. state.DidReceiveCompositorFrameAck();
  1847. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1848. // Finish the previous commit and draw it.
  1849. FinishPreviousCommitAndDrawWithoutExitingDeadline(&state);
  1850. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1851. // Verify we do not send another BeginMainFrame if was are submit-frame
  1852. // throttled and did not just submit one.
  1853. state.SetNeedsBeginMainFrame();
  1854. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1855. state.IssueNextBeginImplFrame();
  1856. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1857. EXPECT_FALSE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1858. state.OnBeginImplFrameDeadline();
  1859. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1860. }
  1861. TEST(SchedulerStateMachineTest,
  1862. TestTriggerDeadlineImmediatelyOnLostLayerTreeFrameSink) {
  1863. SchedulerSettings default_scheduler_settings;
  1864. StateMachine state(default_scheduler_settings);
  1865. SET_UP_STATE(state);
  1866. state.SetNeedsBeginMainFrame();
  1867. state.IssueNextBeginImplFrame();
  1868. EXPECT_ACTION_UPDATE_STATE(
  1869. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1870. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1871. EXPECT_FALSE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1872. state.DidLoseLayerTreeFrameSink();
  1873. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1874. // The deadline should be triggered immediately when LayerTreeFrameSink is
  1875. // lost.
  1876. EXPECT_TRUE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1877. }
  1878. TEST(SchedulerStateMachineTest, TestTriggerDeadlineImmediatelyWhenInvisible) {
  1879. SchedulerSettings default_scheduler_settings;
  1880. StateMachine state(default_scheduler_settings);
  1881. SET_UP_STATE(state);
  1882. state.SetNeedsBeginMainFrame();
  1883. state.IssueNextBeginImplFrame();
  1884. EXPECT_ACTION_UPDATE_STATE(
  1885. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1886. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1887. EXPECT_FALSE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1888. state.SetVisible(false);
  1889. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1890. EXPECT_TRUE(state.ShouldAbortCurrentFrame());
  1891. EXPECT_TRUE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1892. }
  1893. TEST(SchedulerStateMachineTest,
  1894. TestTriggerDeadlineImmediatelyWhenBeginFrameSourcePaused) {
  1895. SchedulerSettings default_scheduler_settings;
  1896. StateMachine state(default_scheduler_settings);
  1897. SET_UP_STATE(state);
  1898. state.SetNeedsBeginMainFrame();
  1899. state.IssueNextBeginImplFrame();
  1900. EXPECT_ACTION_UPDATE_STATE(
  1901. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1902. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1903. EXPECT_FALSE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1904. state.SetBeginFrameSourcePaused(true);
  1905. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1906. EXPECT_TRUE(state.ShouldAbortCurrentFrame());
  1907. EXPECT_TRUE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  1908. }
  1909. TEST(SchedulerStateMachineTest, TestDeferBeginMainFrame) {
  1910. SchedulerSettings settings;
  1911. StateMachine state(settings);
  1912. SET_UP_STATE(state);
  1913. state.SetDeferBeginMainFrame(true);
  1914. state.SetNeedsBeginMainFrame();
  1915. EXPECT_FALSE(state.BeginFrameNeeded());
  1916. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1917. state.IssueNextBeginImplFrame();
  1918. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1919. state.OnBeginImplFrameDeadline();
  1920. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1921. state.SetDeferBeginMainFrame(false);
  1922. state.IssueNextBeginImplFrame();
  1923. EXPECT_ACTION_UPDATE_STATE(
  1924. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1925. }
  1926. TEST(SchedulerStateMachineTest, EarlyOutCommitWantsProactiveBeginFrame) {
  1927. SchedulerSettings settings;
  1928. StateMachine state(settings);
  1929. SET_UP_STATE(state);
  1930. EXPECT_FALSE(state.ProactiveBeginFrameWanted());
  1931. bool commit_has_no_updates = true;
  1932. state.WillCommit(commit_has_no_updates);
  1933. EXPECT_TRUE(state.ProactiveBeginFrameWanted());
  1934. state.IssueNextBeginImplFrame();
  1935. EXPECT_FALSE(state.ProactiveBeginFrameWanted());
  1936. }
  1937. TEST(SchedulerStateMachineTest,
  1938. NoLayerTreeFrameSinkCreationWhileCommitPending) {
  1939. SchedulerSettings settings;
  1940. StateMachine state(settings);
  1941. SET_UP_STATE(state);
  1942. // Set up the request for a commit and start a frame.
  1943. state.SetNeedsBeginMainFrame();
  1944. state.IssueNextBeginImplFrame();
  1945. PerformAction(&state, SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  1946. // Lose the LayerTreeFrameSink.
  1947. state.DidLoseLayerTreeFrameSink();
  1948. // The scheduler shouldn't trigger the LayerTreeFrameSink creation till the
  1949. // previous commit has been cleared.
  1950. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1951. // Trigger the deadline and ensure that the scheduler does not trigger any
  1952. // actions until we receive a response for the pending commit.
  1953. state.OnBeginImplFrameDeadline();
  1954. state.OnBeginImplFrameIdle();
  1955. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1956. // Abort the commit, since that is what we expect the main thread to do if the
  1957. // LayerTreeFrameSink was lost due to a synchronous call from the main thread
  1958. // to release the LayerTreeFrameSink.
  1959. state.BeginMainFrameAborted(CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT);
  1960. // The scheduler should begin the LayerTreeFrameSink creation now.
  1961. EXPECT_ACTION_UPDATE_STATE(
  1962. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  1963. }
  1964. TEST(SchedulerStateMachineTest, NoImplSideInvalidationsWhileInvisible) {
  1965. SchedulerSettings settings;
  1966. StateMachine state(settings);
  1967. SET_UP_STATE(state);
  1968. // No impl-side invalidations should be performed while we are not visible.
  1969. state.SetVisible(false);
  1970. bool needs_first_draw_on_activation = true;
  1971. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  1972. state.IssueNextBeginImplFrame();
  1973. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1974. }
  1975. TEST(SchedulerStateMachineTest,
  1976. NoImplSideInvalidationsWhenBeginFrameSourcePaused) {
  1977. SchedulerSettings settings;
  1978. StateMachine state(settings);
  1979. SET_UP_STATE(state);
  1980. // No impl-side invalidations should be performed while we can not make impl
  1981. // frames.
  1982. state.SetBeginFrameSourcePaused(true);
  1983. bool needs_first_draw_on_activation = true;
  1984. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  1985. state.IssueNextBeginImplFrame();
  1986. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  1987. }
  1988. TEST(SchedulerStateMachineTest,
  1989. ImplSideInvalidationAndMainFrame_NoMainFrameRequest) {
  1990. // No main frame request or any update in the last frame, invalidation runs
  1991. // immediately.
  1992. SchedulerSettings settings;
  1993. StateMachine state(settings);
  1994. SET_UP_STATE(state);
  1995. bool needs_first_draw_on_activation = true;
  1996. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  1997. state.IssueNextBeginImplFrame();
  1998. EXPECT_ACTION_UPDATE_STATE(
  1999. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2000. }
  2001. TEST(SchedulerStateMachineTest,
  2002. ImplSideInvalidationAndMainFrame_MainFrameRequest_FastMainThread) {
  2003. // Main frame request, no abort history and the main thread is fast,
  2004. // invalidation waits for main frame.
  2005. SchedulerSettings settings;
  2006. StateMachine state(settings);
  2007. SET_UP_STATE(state);
  2008. bool needs_first_draw_on_activation = true;
  2009. state.set_should_defer_invalidation_for_fast_main_frame(true);
  2010. state.SetNeedsBeginMainFrame();
  2011. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2012. state.IssueNextBeginImplFrame();
  2013. EXPECT_ACTION_UPDATE_STATE(
  2014. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2015. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2016. }
  2017. TEST(SchedulerStateMachineTest,
  2018. ImplSideInvalidationAndMainFrame_LastFrameCommit) {
  2019. // Main frame committed in the last impl frame and is fast, invalidation waits
  2020. // for main frame request.
  2021. SchedulerSettings settings;
  2022. StateMachine state(settings);
  2023. SET_UP_STATE(state);
  2024. state.SetNeedsBeginMainFrame();
  2025. state.IssueNextBeginImplFrame();
  2026. EXPECT_ACTION_UPDATE_STATE(
  2027. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2028. state.NotifyReadyToCommit();
  2029. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2030. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2031. state.NotifyReadyToActivate();
  2032. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2033. state.OnBeginImplFrameDeadline();
  2034. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  2035. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2036. bool needs_first_draw_on_activation = true;
  2037. state.set_should_defer_invalidation_for_fast_main_frame(true);
  2038. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2039. state.IssueNextBeginImplFrame();
  2040. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2041. }
  2042. TEST(SchedulerStateMachineTest,
  2043. ImplSideInvalidationAndMainFrame_LastFrameAborted) {
  2044. // Last main frame was aborted. An invalidation is performed even if a main
  2045. // frame request is pending.
  2046. SchedulerSettings settings;
  2047. StateMachine state(settings);
  2048. SET_UP_STATE(state);
  2049. state.SetNeedsBeginMainFrame();
  2050. state.IssueNextBeginImplFrame();
  2051. EXPECT_ACTION_UPDATE_STATE(
  2052. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2053. state.BeginMainFrameAborted(CommitEarlyOutReason::FINISHED_NO_UPDATES);
  2054. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2055. state.OnBeginImplFrameDeadline();
  2056. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2057. bool needs_first_draw_on_activation = true;
  2058. state.set_should_defer_invalidation_for_fast_main_frame(true);
  2059. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2060. state.SetNeedsBeginMainFrame();
  2061. state.IssueNextBeginImplFrame();
  2062. EXPECT_ACTION_UPDATE_STATE(
  2063. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2064. EXPECT_ACTION_UPDATE_STATE(
  2065. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2066. }
  2067. TEST(SchedulerStateMachineTest,
  2068. ImplSideInvalidationAndMainFrame_MainFrameRequest_SlowMainThread) {
  2069. // Main frame request but the main thread is slow, invalidation runs
  2070. // immediately.
  2071. SchedulerSettings settings;
  2072. StateMachine state(settings);
  2073. SET_UP_STATE(state);
  2074. bool needs_first_draw_on_activation = true;
  2075. state.set_should_defer_invalidation_for_fast_main_frame(false);
  2076. state.SetNeedsBeginMainFrame();
  2077. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2078. state.IssueNextBeginImplFrame();
  2079. EXPECT_ACTION_UPDATE_STATE(
  2080. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2081. EXPECT_ACTION_UPDATE_STATE(
  2082. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2083. }
  2084. TEST(SchedulerStateMachineTest, NoImplSideInvalidationUntilFrameSinkActive) {
  2085. SchedulerSettings settings;
  2086. StateMachine state(settings);
  2087. SET_UP_STATE(state);
  2088. // Prefer impl side invalidation over begin main frame.
  2089. state.set_should_defer_invalidation_for_fast_main_frame(false);
  2090. state.DidLoseLayerTreeFrameSink();
  2091. // Create new frame sink but don't commit or activate yet.
  2092. EXPECT_ACTION_UPDATE_STATE(
  2093. SchedulerStateMachine::Action::BEGIN_LAYER_TREE_FRAME_SINK_CREATION);
  2094. state.DidCreateAndInitializeLayerTreeFrameSink();
  2095. state.SetNeedsBeginMainFrame();
  2096. bool needs_first_draw_on_activation = true;
  2097. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2098. state.IssueNextBeginImplFrame();
  2099. EXPECT_ACTION_UPDATE_STATE(
  2100. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2101. // No impl side invalidation because we're still waiting for first commit.
  2102. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2103. state.NotifyReadyToCommit();
  2104. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2105. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2106. state.OnBeginImplFrameDeadline();
  2107. state.OnBeginImplFrameIdle();
  2108. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2109. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2110. state.IssueNextBeginImplFrame();
  2111. // No impl side invalidation because we're still waiting for first activation.
  2112. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2113. state.NotifyReadyToActivate();
  2114. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2115. state.OnBeginImplFrameDeadline();
  2116. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  2117. state.OnBeginImplFrameIdle();
  2118. state.SetNeedsBeginMainFrame();
  2119. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2120. state.IssueNextBeginImplFrame();
  2121. EXPECT_ACTION_UPDATE_STATE(
  2122. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2123. // Impl side invalidation only after receiving first commit and activation for
  2124. // new frame sink.
  2125. EXPECT_ACTION_UPDATE_STATE(
  2126. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2127. }
  2128. TEST(SchedulerStateMachineTest, ImplSideInvalidationWhenPendingTreeExists) {
  2129. SchedulerSettings settings;
  2130. StateMachine state(settings);
  2131. SET_UP_STATE(state);
  2132. // Set up request for the main frame, commit and create the pending tree.
  2133. state.SetNeedsBeginMainFrame();
  2134. state.IssueNextBeginImplFrame();
  2135. EXPECT_ACTION_UPDATE_STATE(
  2136. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2137. state.NotifyReadyToCommit();
  2138. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2139. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2140. // Request an impl-side invalidation after the commit. The request should wait
  2141. // till the current pending tree is activated.
  2142. bool needs_first_draw_on_activation = true;
  2143. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2144. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2145. // Activate the pending tree. Since the commit fills the impl-side
  2146. // invalidation funnel as well, the request should wait until the next
  2147. // BeginFrame.
  2148. state.NotifyReadyToActivate();
  2149. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2150. state.OnBeginImplFrameDeadline();
  2151. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  2152. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2153. // Since there is no main frame request, this should perform impl-side
  2154. // invalidations.
  2155. state.set_should_defer_invalidation_for_fast_main_frame(false);
  2156. state.IssueNextBeginImplFrame();
  2157. EXPECT_ACTION_UPDATE_STATE(
  2158. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2159. }
  2160. TEST(SchedulerStateMachineTest, ImplSideInvalidationWhileReadyToCommit) {
  2161. SchedulerSettings settings;
  2162. StateMachine state(settings);
  2163. SET_UP_STATE(state);
  2164. // Set up request for the main frame with a slow main thread.
  2165. state.set_should_defer_invalidation_for_fast_main_frame(false);
  2166. state.SetNeedsBeginMainFrame();
  2167. state.IssueNextBeginImplFrame();
  2168. EXPECT_ACTION_UPDATE_STATE(
  2169. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2170. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2171. state.NotifyReadyToCommit();
  2172. // Request an impl-side invalidation after we are ready to commit. The
  2173. // invalidations are merged.
  2174. bool needs_first_draw_on_activation = true;
  2175. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2176. EXPECT_TRUE(state.needs_impl_side_invalidation());
  2177. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2178. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2179. EXPECT_FALSE(state.needs_impl_side_invalidation());
  2180. }
  2181. TEST(SchedulerStateMachineTest,
  2182. ConsecutiveImplSideInvalidationsWaitForBeginFrame) {
  2183. SchedulerSettings settings;
  2184. StateMachine state(settings);
  2185. SET_UP_STATE(state);
  2186. // Set up a request for impl-side invalidation.
  2187. bool needs_first_draw_on_activation = true;
  2188. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2189. state.IssueNextBeginImplFrame();
  2190. state.OnBeginImplFrameDeadline();
  2191. EXPECT_ACTION_UPDATE_STATE(
  2192. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2193. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2194. // Request another invalidation, which should wait until the pending tree is
  2195. // activated *and* we start the next BeginFrame.
  2196. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2197. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2198. state.NotifyReadyToActivate();
  2199. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2200. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2201. // Now start the next frame, which will first draw the active tree and then
  2202. // perform the pending impl-side invalidation request.
  2203. state.IssueNextBeginImplFrame();
  2204. state.OnBeginImplFrameDeadline();
  2205. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  2206. EXPECT_ACTION_UPDATE_STATE(
  2207. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2208. }
  2209. TEST(SchedulerStateMachineTest, ImplSideInvalidationsThrottledOnDraw) {
  2210. // In commit_to_active_tree mode, performing the next invalidation should be
  2211. // throttled on the active tree being drawn.
  2212. SchedulerSettings settings;
  2213. settings.commit_to_active_tree = true;
  2214. StateMachine state(settings);
  2215. SET_UP_STATE(state);
  2216. // Commit to the sync tree, activate and draw.
  2217. state.SetNeedsBeginMainFrame();
  2218. state.IssueNextBeginImplFrame();
  2219. EXPECT_ACTION_UPDATE_STATE(
  2220. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2221. state.NotifyReadyToCommit();
  2222. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2223. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2224. state.NotifyReadyToActivate();
  2225. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2226. state.NotifyReadyToDraw();
  2227. state.OnBeginImplFrameDeadline();
  2228. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  2229. state.DidSubmitCompositorFrame();
  2230. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2231. // Request impl-side invalidation and start a new frame, which should be
  2232. // blocked on the ack for the previous frame.
  2233. bool needs_first_draw_on_activation = true;
  2234. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2235. state.IssueNextBeginImplFrame();
  2236. state.OnBeginImplFrameDeadline();
  2237. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2238. // Ack the previous frame and begin impl frame, which should perform the
  2239. // invalidation now.
  2240. state.DidReceiveCompositorFrameAck();
  2241. state.IssueNextBeginImplFrame();
  2242. state.OnBeginImplFrameDeadline();
  2243. EXPECT_ACTION_UPDATE_STATE(
  2244. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2245. }
  2246. TEST(SchedulerStateMachineTest, PrepareTilesWaitForImplSideInvalidation) {
  2247. // PrepareTiles
  2248. SchedulerSettings settings;
  2249. StateMachine state(settings);
  2250. SET_UP_STATE(state);
  2251. // Request a PrepareTiles and impl-side invalidation. The impl-side
  2252. // invalidation should run first, since it will perform PrepareTiles as well.
  2253. bool needs_first_draw_on_activation = true;
  2254. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2255. state.SetNeedsPrepareTiles();
  2256. state.IssueNextBeginImplFrame();
  2257. state.OnBeginImplFrameDeadline();
  2258. EXPECT_ACTION_UPDATE_STATE(
  2259. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2260. state.DidPrepareTiles();
  2261. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2262. }
  2263. TEST(SchedulerStateMachineTest, TestFullPipelineMode) {
  2264. SchedulerSettings scheduler_settings;
  2265. scheduler_settings.wait_for_all_pipeline_stages_before_draw = true;
  2266. StateMachine state(scheduler_settings);
  2267. SET_UP_STATE(state);
  2268. // Start clean and set commit.
  2269. state.SetNeedsBeginMainFrame();
  2270. // While we are waiting for an main frame or pending tree activation, we
  2271. // should even block while we can't draw.
  2272. state.SetCanDraw(false);
  2273. // Begin the frame.
  2274. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 10);
  2275. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2276. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::BLOCKED,
  2277. state.CurrentBeginImplFrameDeadlineMode());
  2278. // If main thread defers commits, don't wait for it.
  2279. state.SetDeferBeginMainFrame(true);
  2280. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::IMMEDIATE,
  2281. state.CurrentBeginImplFrameDeadlineMode());
  2282. state.SetDeferBeginMainFrame(false);
  2283. EXPECT_ACTION_UPDATE_STATE(
  2284. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2285. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::SENT);
  2286. EXPECT_FALSE(state.NeedsCommit());
  2287. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2288. // We are blocking on the main frame.
  2289. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::BLOCKED,
  2290. state.CurrentBeginImplFrameDeadlineMode());
  2291. // Tell the scheduler the frame finished.
  2292. state.NotifyReadyToCommit();
  2293. EXPECT_MAIN_FRAME_STATE(
  2294. SchedulerStateMachine::BeginMainFrameState::READY_TO_COMMIT);
  2295. // We are blocking on commit.
  2296. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::BLOCKED,
  2297. state.CurrentBeginImplFrameDeadlineMode());
  2298. // Commit.
  2299. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2300. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2301. // We are blocking on activation.
  2302. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::BLOCKED,
  2303. state.CurrentBeginImplFrameDeadlineMode());
  2304. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2305. // We should prepare tiles even though we are not in the deadline, otherwise
  2306. // we would get stuck here.
  2307. EXPECT_FALSE(state.ShouldPrepareTiles());
  2308. state.SetNeedsPrepareTiles();
  2309. EXPECT_TRUE(state.ShouldPrepareTiles());
  2310. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::PREPARE_TILES);
  2311. // Ready to activate, but not draw.
  2312. state.NotifyReadyToActivate();
  2313. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2314. // We should no longer block, because can_draw is still false, and we are no
  2315. // longer waiting for activation.
  2316. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::IMMEDIATE,
  2317. state.CurrentBeginImplFrameDeadlineMode());
  2318. // However, we should continue to block on ready to draw if we can draw.
  2319. state.SetCanDraw(true);
  2320. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::BLOCKED,
  2321. state.CurrentBeginImplFrameDeadlineMode());
  2322. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2323. // Ready to draw triggers immediate deadline.
  2324. state.NotifyReadyToDraw();
  2325. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::IMMEDIATE,
  2326. state.CurrentBeginImplFrameDeadlineMode());
  2327. state.OnBeginImplFrameDeadline();
  2328. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  2329. state.DidSubmitCompositorFrame();
  2330. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2331. // In full-pipe mode, CompositorFrameAck should always arrive before any
  2332. // subsequent BeginFrame.
  2333. state.DidReceiveCompositorFrameAck();
  2334. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2335. // Request a redraw without main frame.
  2336. state.SetNeedsRedraw(true);
  2337. // Redraw should happen immediately since there is no pending tree and active
  2338. // tree is ready to draw.
  2339. frame_id.sequence_number++;
  2340. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2341. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2342. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::IMMEDIATE,
  2343. state.CurrentBeginImplFrameDeadlineMode());
  2344. // Redraw on impl-side only.
  2345. state.OnBeginImplFrameDeadline();
  2346. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  2347. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2348. state.DidSubmitCompositorFrame();
  2349. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2350. // In full-pipe mode, CompositorFrameAck should always arrive before any
  2351. // subsequent BeginFrame.
  2352. state.DidReceiveCompositorFrameAck();
  2353. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2354. // Request a redraw on active frame and a main frame.
  2355. state.SetNeedsRedraw(true);
  2356. state.SetNeedsBeginMainFrame();
  2357. frame_id.sequence_number++;
  2358. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2359. EXPECT_ACTION_UPDATE_STATE(
  2360. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2361. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2362. // Blocked on main frame.
  2363. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::BLOCKED,
  2364. state.CurrentBeginImplFrameDeadlineMode());
  2365. // Even with SMOOTHNESS_TAKES_PRIORITY, we don't prioritize impl thread and we
  2366. // should wait for main frame.
  2367. state.SetTreePrioritiesAndScrollState(
  2368. SMOOTHNESS_TAKES_PRIORITY,
  2369. ScrollHandlerState::SCROLL_DOES_NOT_AFFECT_SCROLL_HANDLER);
  2370. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::BLOCKED,
  2371. state.CurrentBeginImplFrameDeadlineMode());
  2372. // Abort commit and ensure that we don't block anymore.
  2373. state.BeginMainFrameAborted(CommitEarlyOutReason::FINISHED_NO_UPDATES);
  2374. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2375. EXPECT_MAIN_FRAME_STATE(SchedulerStateMachine::BeginMainFrameState::IDLE);
  2376. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::IMMEDIATE,
  2377. state.CurrentBeginImplFrameDeadlineMode());
  2378. }
  2379. TEST(SchedulerStateMachineTest, AllowSkippingActiveTreeFirstDraws) {
  2380. SchedulerSettings settings;
  2381. StateMachine state(settings);
  2382. SET_UP_STATE(state);
  2383. // Impl-side invalidation creates a pending tree which is activated but not
  2384. // drawn in this frame.
  2385. bool needs_first_draw_on_activation = false;
  2386. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2387. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 1);
  2388. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2389. state.OnBeginImplFrameDeadline();
  2390. EXPECT_ACTION_UPDATE_STATE(
  2391. SchedulerStateMachine::Action::PERFORM_IMPL_SIDE_INVALIDATION);
  2392. state.NotifyReadyToActivate();
  2393. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2394. state.OnBeginImplFrameIdle();
  2395. // Now we have a main frame.
  2396. state.SetNeedsBeginMainFrame();
  2397. frame_id.sequence_number++;
  2398. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2399. EXPECT_ACTION_UPDATE_STATE(
  2400. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2401. state.NotifyReadyToCommit();
  2402. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2403. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2404. // We should be able to activate this tree without drawing the active tree.
  2405. state.NotifyReadyToActivate();
  2406. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2407. }
  2408. TEST(SchedulerStateMachineTest, DelayDrawIfAnimationWorkletsPending) {
  2409. SchedulerSettings default_scheduler_settings;
  2410. StateMachine state(default_scheduler_settings);
  2411. SET_UP_STATE(state);
  2412. // This test verifies that having pending mutations from Animation Worklets on
  2413. // the active tree will not trigger the deadline early.
  2414. state.SetNeedsBeginMainFrame();
  2415. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 10);
  2416. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2417. EXPECT_ACTION_UPDATE_STATE(
  2418. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2419. state.NotifyReadyToCommit();
  2420. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2421. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2422. state.NotifyReadyToActivate();
  2423. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2424. EXPECT_TRUE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  2425. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::IMMEDIATE,
  2426. state.CurrentBeginImplFrameDeadlineMode());
  2427. // Started async mutation cycle for animation worklets.
  2428. state.NotifyAnimationWorkletStateChange(
  2429. SchedulerStateMachine::AnimationWorkletState::PROCESSING,
  2430. SchedulerStateMachine::TreeType::ACTIVE);
  2431. EXPECT_FALSE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  2432. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::REGULAR,
  2433. state.CurrentBeginImplFrameDeadlineMode());
  2434. // Second queued state change.
  2435. state.NotifyAnimationWorkletStateChange(
  2436. SchedulerStateMachine::AnimationWorkletState::PROCESSING,
  2437. SchedulerStateMachine::TreeType::ACTIVE);
  2438. EXPECT_FALSE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  2439. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::REGULAR,
  2440. state.CurrentBeginImplFrameDeadlineMode());
  2441. // First mutation cycle completes. Still waiting on queued mutation cycle
  2442. // before being ready to draw.
  2443. state.NotifyAnimationWorkletStateChange(
  2444. SchedulerStateMachine::AnimationWorkletState::IDLE,
  2445. SchedulerStateMachine::TreeType::ACTIVE);
  2446. EXPECT_FALSE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  2447. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::REGULAR,
  2448. state.CurrentBeginImplFrameDeadlineMode());
  2449. // Queued mutation cycle completes. Now ready to draw.
  2450. state.NotifyAnimationWorkletStateChange(
  2451. SchedulerStateMachine::AnimationWorkletState::IDLE,
  2452. SchedulerStateMachine::TreeType::ACTIVE);
  2453. EXPECT_TRUE(state.ShouldTriggerBeginImplFrameDeadlineImmediately());
  2454. EXPECT_EQ(SchedulerStateMachine::BeginImplFrameDeadlineMode::IMMEDIATE,
  2455. state.CurrentBeginImplFrameDeadlineMode());
  2456. // AnimationWorkletState does not effect CanDraw, only whether an early draw
  2457. // deadline should be used (crbug/937975).
  2458. state.SetNeedsRedraw(true);
  2459. state.OnBeginImplFrameDeadline();
  2460. EXPECT_IMPL_FRAME_STATE(
  2461. SchedulerStateMachine::BeginImplFrameState::INSIDE_DEADLINE);
  2462. EXPECT_TRUE(state.ShouldDraw());
  2463. state.NotifyAnimationWorkletStateChange(
  2464. SchedulerStateMachine::AnimationWorkletState::PROCESSING,
  2465. SchedulerStateMachine::TreeType::ACTIVE);
  2466. EXPECT_TRUE(state.ShouldDraw());
  2467. state.NotifyAnimationWorkletStateChange(
  2468. SchedulerStateMachine::AnimationWorkletState::IDLE,
  2469. SchedulerStateMachine::TreeType::ACTIVE);
  2470. EXPECT_TRUE(state.ShouldDraw());
  2471. }
  2472. TEST(SchedulerStateMachineTest, BlockActivationIfAnimationWorkletsPending) {
  2473. SchedulerSettings settings;
  2474. StateMachine state(settings);
  2475. SET_UP_STATE(state);
  2476. // Verify that pending mutations from Animation Worklets block activation.
  2477. state.SetNeedsBeginMainFrame();
  2478. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 10);
  2479. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2480. EXPECT_ACTION_UPDATE_STATE(
  2481. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2482. state.NotifyReadyToCommit();
  2483. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2484. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2485. state.NotifyAnimationWorkletStateChange(
  2486. SchedulerStateMachine::AnimationWorkletState::PROCESSING,
  2487. SchedulerStateMachine::TreeType::PENDING);
  2488. state.NotifyReadyToActivate();
  2489. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2490. state.NotifyAnimationWorkletStateChange(
  2491. SchedulerStateMachine::AnimationWorkletState::IDLE,
  2492. SchedulerStateMachine::TreeType::PENDING);
  2493. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2494. }
  2495. TEST(SchedulerStateMachineTest, BlockActivationIfPaintWorkletsPending) {
  2496. SchedulerSettings settings;
  2497. StateMachine state(settings);
  2498. SET_UP_STATE(state);
  2499. state.SetNeedsBeginMainFrame();
  2500. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 10);
  2501. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2502. EXPECT_ACTION_UPDATE_STATE(
  2503. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2504. state.NotifyReadyToCommit();
  2505. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2506. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2507. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2508. // Post-commit, we start working on PaintWorklets. It is not valid to activate
  2509. // until they are done.
  2510. state.NotifyPaintWorkletStateChange(
  2511. SchedulerStateMachine::PaintWorkletState::PROCESSING);
  2512. // We (correctly) cannot call state.NotifyReadyToActivate() here as it hits a
  2513. // DCHECK because PaintWorklets are ongoing.
  2514. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2515. state.NotifyPaintWorkletStateChange(
  2516. SchedulerStateMachine::PaintWorkletState::IDLE);
  2517. state.NotifyReadyToActivate();
  2518. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2519. }
  2520. TEST(SchedulerStateMachineTest,
  2521. BlockActivationIfPaintWorkletsPendingEvenWhenAbortingFrame) {
  2522. SchedulerSettings settings;
  2523. StateMachine state(settings);
  2524. SET_UP_STATE(state);
  2525. state.SetNeedsBeginMainFrame();
  2526. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 10);
  2527. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2528. EXPECT_ACTION_UPDATE_STATE(
  2529. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2530. state.NotifyReadyToCommit();
  2531. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2532. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2533. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2534. // Even if we are aborting the frame, we must paint the pending tree before we
  2535. // activate it (because we cannot have an unpainted active tree).
  2536. state.NotifyPaintWorkletStateChange(
  2537. SchedulerStateMachine::PaintWorkletState::PROCESSING);
  2538. state.SetVisible(false);
  2539. ASSERT_TRUE(state.ShouldAbortCurrentFrame());
  2540. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2541. state.NotifyPaintWorkletStateChange(
  2542. SchedulerStateMachine::PaintWorkletState::IDLE);
  2543. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2544. }
  2545. TEST(SchedulerStateMachineTest, TestFullPipelineModeDoesntBlockAfterCommit) {
  2546. SchedulerSettings settings;
  2547. settings.wait_for_all_pipeline_stages_before_draw = true;
  2548. StateMachine state(settings);
  2549. SET_UP_STATE(state);
  2550. const bool needs_first_draw_on_activation = true;
  2551. state.SetNeedsImplSideInvalidation(needs_first_draw_on_activation);
  2552. state.SetNeedsBeginMainFrame();
  2553. state.SetNeedsRedraw(true);
  2554. viz::BeginFrameId frame_id = viz::BeginFrameId(0, 10);
  2555. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2556. EXPECT_ACTION_UPDATE_STATE(
  2557. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2558. state.NotifyReadyToCommit();
  2559. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2560. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2561. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::NONE);
  2562. state.NotifyReadyToActivate();
  2563. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::ACTIVATE_SYNC_TREE);
  2564. state.NotifyReadyToDraw();
  2565. EXPECT_TRUE(state.active_tree_needs_first_draw());
  2566. EXPECT_IMPL_FRAME_STATE(
  2567. SchedulerStateMachine::BeginImplFrameState::INSIDE_BEGIN_FRAME);
  2568. // Go all the way until ready to draw, but make sure we're not within
  2569. // the frame deadline, so actual draw doesn't happen...
  2570. EXPECT_FALSE(state.ShouldDraw());
  2571. // ... then have another commit ...
  2572. state.SetNeedsBeginMainFrame();
  2573. frame_id.sequence_number++;
  2574. state.OnBeginImplFrame(frame_id, kAnimateOnly);
  2575. EXPECT_ACTION_UPDATE_STATE(
  2576. SchedulerStateMachine::Action::SEND_BEGIN_MAIN_FRAME);
  2577. state.NotifyReadyToCommit();
  2578. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::COMMIT);
  2579. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::POST_COMMIT);
  2580. // ... and make sure we're in a state where we can proceed,
  2581. // rather than draw being blocked by the pending tree.
  2582. state.OnBeginImplFrameDeadline();
  2583. EXPECT_TRUE(state.ShouldDraw());
  2584. EXPECT_ACTION_UPDATE_STATE(SchedulerStateMachine::Action::DRAW_IF_POSSIBLE);
  2585. }
  2586. } // namespace
  2587. } // namespace cc