layer_unittest.cc 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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/layers/layer.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/containers/contains.h"
  10. #include "base/threading/thread_task_runner_handle.h"
  11. #include "cc/animation/animation_host.h"
  12. #include "cc/animation/animation_id_provider.h"
  13. #include "cc/base/math_util.h"
  14. #include "cc/layers/layer_impl.h"
  15. #include "cc/layers/picture_layer.h"
  16. #include "cc/layers/solid_color_scrollbar_layer.h"
  17. #include "cc/test/animation_test_common.h"
  18. #include "cc/test/cc_test_suite.h"
  19. #include "cc/test/fake_content_layer_client.h"
  20. #include "cc/test/fake_impl_task_runner_provider.h"
  21. #include "cc/test/fake_layer_tree_host.h"
  22. #include "cc/test/fake_layer_tree_host_client.h"
  23. #include "cc/test/fake_layer_tree_host_impl.h"
  24. #include "cc/test/layer_test_common.h"
  25. #include "cc/test/stub_layer_tree_host_single_thread_client.h"
  26. #include "cc/test/test_task_graph_runner.h"
  27. #include "cc/trees/clip_node.h"
  28. #include "cc/trees/layer_tree_host.h"
  29. #include "cc/trees/single_thread_proxy.h"
  30. #include "cc/trees/transform_node.h"
  31. #include "components/viz/common/frame_sinks/copy_output_request.h"
  32. #include "components/viz/common/frame_sinks/copy_output_result.h"
  33. #include "testing/gmock/include/gmock/gmock.h"
  34. #include "testing/gtest/include/gtest/gtest.h"
  35. #include "third_party/skia/include/core/SkColor.h"
  36. #include "ui/gfx/animation/keyframe/keyframed_animation_curve.h"
  37. #include "ui/gfx/geometry/point3_f.h"
  38. #include "ui/gfx/geometry/point_f.h"
  39. #include "ui/gfx/geometry/size.h"
  40. #include "ui/gfx/geometry/transform.h"
  41. #include "ui/gfx/geometry/vector2d_f.h"
  42. using ::testing::AnyNumber;
  43. using ::testing::AtLeast;
  44. using ::testing::Mock;
  45. using ::testing::StrictMock;
  46. using ::testing::_;
  47. #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
  48. do { \
  49. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
  50. code_to_test; \
  51. Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
  52. } while (false)
  53. #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(code_to_test) \
  54. code_to_test; \
  55. root->layer_tree_host()->BuildPropertyTreesForTesting(); \
  56. EXPECT_FALSE(root->subtree_property_changed()); \
  57. EXPECT_TRUE(top->subtree_property_changed()); \
  58. EXPECT_TRUE( \
  59. base::Contains(const_cast<const LayerTreeHost*>(top->layer_tree_host()) \
  60. ->pending_commit_state() \
  61. ->layers_that_should_push_properties, \
  62. top.get())); \
  63. EXPECT_TRUE(child->subtree_property_changed()); \
  64. EXPECT_TRUE(base::Contains( \
  65. const_cast<const LayerTreeHost*>(child->layer_tree_host()) \
  66. ->pending_commit_state() \
  67. ->layers_that_should_push_properties, \
  68. child.get())); \
  69. EXPECT_TRUE(grand_child->subtree_property_changed()); \
  70. EXPECT_TRUE(base::Contains( \
  71. const_cast<const LayerTreeHost*>(grand_child->layer_tree_host()) \
  72. ->pending_commit_state() \
  73. ->layers_that_should_push_properties, \
  74. grand_child.get()));
  75. #define EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(code_to_test) \
  76. code_to_test; \
  77. EXPECT_FALSE(root->subtree_property_changed()); \
  78. EXPECT_FALSE(top->subtree_property_changed()); \
  79. EXPECT_FALSE(child->subtree_property_changed()); \
  80. EXPECT_FALSE(grand_child->subtree_property_changed());
  81. // TODO(https://crbug.com/1330728): tests should be cleaned up to eliminate
  82. // mixing of EXPECT_CALL with calls to the mock functions. This method
  83. // should be deduped with EXPECT_SET_NEEDS_COMMIT as part of this cleanup.
  84. #define EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(code_to_test) \
  85. do { \
  86. code_to_test; \
  87. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset()); \
  88. } while (false)
  89. #define EXPECT_SET_NEEDS_COMMIT_WAS_NOT_CALLED(code_to_test) \
  90. do { \
  91. code_to_test; \
  92. EXPECT_FALSE(layer_tree_host_->GetNeedsCommitAndReset()); \
  93. } while (false)
  94. namespace cc {
  95. namespace {
  96. static auto kArbitrarySourceId1 =
  97. base::UnguessableToken::Deserialize(0xdead, 0xbeef);
  98. static auto kArbitrarySourceId2 =
  99. base::UnguessableToken::Deserialize(0xdead, 0xbee0);
  100. class MockLayerTreeHost : public LayerTreeHost {
  101. public:
  102. MockLayerTreeHost(LayerTreeHostSingleThreadClient* single_thread_client,
  103. LayerTreeHost::InitParams params)
  104. : LayerTreeHost(std::move(params), CompositorMode::SINGLE_THREADED) {
  105. InitializeSingleThreaded(single_thread_client,
  106. base::ThreadTaskRunnerHandle::Get());
  107. }
  108. CommitState* GetPendingCommitState() { return pending_commit_state(); }
  109. ThreadUnsafeCommitState& GetThreadUnsafeCommitState() {
  110. return thread_unsafe_commit_state();
  111. }
  112. MOCK_METHOD(void, SetNeedsUpdateLayers, (), (override));
  113. MOCK_METHOD(void, SetNeedsFullTreeSync, (), (override));
  114. void SetNeedsCommit() override { needs_commit_ = true; }
  115. bool GetNeedsCommitAndReset() {
  116. const bool out = needs_commit_;
  117. needs_commit_ = false;
  118. return out;
  119. }
  120. private:
  121. bool needs_commit_ = false;
  122. };
  123. bool LayerNeedsDisplay(Layer* layer) {
  124. return !layer->update_rect().IsEmpty();
  125. }
  126. class LayerTest : public testing::Test {
  127. public:
  128. LayerTest()
  129. : host_impl_(LayerTreeSettings(),
  130. &task_runner_provider_,
  131. &task_graph_runner_) {
  132. timeline_impl_ =
  133. AnimationTimeline::Create(AnimationIdProvider::NextTimelineId());
  134. timeline_impl_->set_is_impl_only(true);
  135. host_impl_.animation_host()->AddAnimationTimeline(timeline_impl_);
  136. }
  137. const LayerTreeSettings& settings() { return settings_; }
  138. scoped_refptr<AnimationTimeline> timeline_impl() { return timeline_impl_; }
  139. protected:
  140. void SetUp() override {
  141. animation_host_ = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  142. LayerTreeHost::InitParams params;
  143. params.client = &fake_client_;
  144. params.settings = &settings_;
  145. params.task_graph_runner = &task_graph_runner_;
  146. params.mutator_host = animation_host_.get();
  147. layer_tree_host_ = std::make_unique<StrictMock<MockLayerTreeHost>>(
  148. &single_thread_client_, std::move(params));
  149. }
  150. void TearDown() override {
  151. Mock::VerifyAndClearExpectations(layer_tree_host_.get());
  152. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
  153. parent_ = nullptr;
  154. child1_ = nullptr;
  155. child2_ = nullptr;
  156. child3_ = nullptr;
  157. grand_child1_ = nullptr;
  158. grand_child2_ = nullptr;
  159. grand_child3_ = nullptr;
  160. layer_tree_host_->SetRootLayer(nullptr);
  161. animation_host_->SetMutatorHostClient(nullptr);
  162. layer_tree_host_ = nullptr;
  163. animation_host_ = nullptr;
  164. }
  165. void SimulateCommitForLayer(Layer* layer) {
  166. layer->PushPropertiesTo(
  167. layer->CreateLayerImpl(host_impl_.active_tree()).get(),
  168. *layer_tree_host_->GetPendingCommitState(),
  169. layer_tree_host_->GetThreadUnsafeCommitState());
  170. }
  171. void CommitAndPushProperties(Layer* layer, LayerImpl* layer_impl) {
  172. auto& unsafe_state = layer_tree_host_->GetThreadUnsafeCommitState();
  173. std::unique_ptr<CommitState> commit_state = layer_tree_host_->WillCommit(
  174. /*completion=*/nullptr, /*has_updates=*/true);
  175. layer->PushPropertiesTo(layer_impl, *commit_state, unsafe_state);
  176. layer_tree_host_->CommitComplete(
  177. {base::TimeTicks(), base::TimeTicks::Now()});
  178. }
  179. void VerifyTestTreeInitialState() const {
  180. ASSERT_EQ(3U, parent_->children().size());
  181. EXPECT_EQ(child1_, parent_->children()[0]);
  182. EXPECT_EQ(child2_, parent_->children()[1]);
  183. EXPECT_EQ(child3_, parent_->children()[2]);
  184. EXPECT_EQ(parent_.get(), child1_->parent());
  185. EXPECT_EQ(parent_.get(), child2_->parent());
  186. EXPECT_EQ(parent_.get(), child3_->parent());
  187. ASSERT_EQ(2U, child1_->children().size());
  188. EXPECT_EQ(grand_child1_, child1_->children()[0]);
  189. EXPECT_EQ(grand_child2_, child1_->children()[1]);
  190. EXPECT_EQ(child1_.get(), grand_child1_->parent());
  191. EXPECT_EQ(child1_.get(), grand_child2_->parent());
  192. ASSERT_EQ(1U, child2_->children().size());
  193. EXPECT_EQ(grand_child3_, child2_->children()[0]);
  194. EXPECT_EQ(child2_.get(), grand_child3_->parent());
  195. ASSERT_EQ(0U, child3_->children().size());
  196. }
  197. void CreateSimpleTestTree() {
  198. parent_ = Layer::Create();
  199. child1_ = Layer::Create();
  200. child2_ = Layer::Create();
  201. child3_ = Layer::Create();
  202. grand_child1_ = Layer::Create();
  203. grand_child2_ = Layer::Create();
  204. grand_child3_ = Layer::Create();
  205. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
  206. layer_tree_host_->SetRootLayer(parent_);
  207. parent_->AddChild(child1_);
  208. parent_->AddChild(child2_);
  209. parent_->AddChild(child3_);
  210. child1_->AddChild(grand_child1_);
  211. child1_->AddChild(grand_child2_);
  212. child2_->AddChild(grand_child3_);
  213. Mock::VerifyAndClearExpectations(layer_tree_host_.get());
  214. VerifyTestTreeInitialState();
  215. }
  216. FakeImplTaskRunnerProvider task_runner_provider_;
  217. TestTaskGraphRunner task_graph_runner_;
  218. FakeLayerTreeHostImpl host_impl_;
  219. StubLayerTreeHostSingleThreadClient single_thread_client_;
  220. FakeLayerTreeHostClient fake_client_;
  221. std::unique_ptr<StrictMock<MockLayerTreeHost>> layer_tree_host_;
  222. std::unique_ptr<AnimationHost> animation_host_;
  223. scoped_refptr<Layer> parent_;
  224. scoped_refptr<Layer> child1_;
  225. scoped_refptr<Layer> child2_;
  226. scoped_refptr<Layer> child3_;
  227. scoped_refptr<Layer> grand_child1_;
  228. scoped_refptr<Layer> grand_child2_;
  229. scoped_refptr<Layer> grand_child3_;
  230. scoped_refptr<AnimationTimeline> timeline_impl_;
  231. LayerTreeSettings settings_;
  232. };
  233. class LayerTestWithLayerList : public LayerTest {
  234. void SetUp() override {
  235. settings_.use_layer_lists = true;
  236. LayerTest::SetUp();
  237. }
  238. };
  239. TEST_F(LayerTest, BasicCreateAndDestroy) {
  240. scoped_refptr<Layer> test_layer = Layer::Create();
  241. ASSERT_TRUE(test_layer.get());
  242. test_layer->SetLayerTreeHost(layer_tree_host_.get());
  243. test_layer->SetLayerTreeHost(nullptr);
  244. }
  245. TEST_F(LayerTest, LayerPropertyChangedForSubtree) {
  246. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
  247. scoped_refptr<Layer> root = Layer::Create();
  248. scoped_refptr<Layer> top = Layer::Create();
  249. scoped_refptr<Layer> child = Layer::Create();
  250. scoped_refptr<Layer> child2 = Layer::Create();
  251. scoped_refptr<Layer> grand_child = Layer::Create();
  252. FakeContentLayerClient client;
  253. scoped_refptr<PictureLayer> mask_layer1 = PictureLayer::Create(&client);
  254. mask_layer1->SetElementId(LayerIdToElementIdForTesting(mask_layer1->id()));
  255. layer_tree_host_->SetRootLayer(root);
  256. root->AddChild(top);
  257. top->AddChild(child);
  258. top->AddChild(child2);
  259. child->AddChild(grand_child);
  260. // To force a transform node for |top|.
  261. gfx::Transform top_transform;
  262. top_transform.Scale3d(1, 2, 3);
  263. top->SetTransform(top_transform);
  264. child->SetForceRenderSurfaceForTesting(true);
  265. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  266. // Resizing without a mask layer or masks_to_bounds, should only require a
  267. // regular commit. Note that a layer and its mask should match sizes, but
  268. // the mask isn't in the tree yet, so won't need its own commit.
  269. gfx::Size arbitrary_size = gfx::Size(1, 2);
  270. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(top->SetBounds(arbitrary_size));
  271. EXPECT_SET_NEEDS_COMMIT_WAS_NOT_CALLED(
  272. mask_layer1->SetBounds(arbitrary_size));
  273. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync());
  274. layer_tree_host_->WillCommit(/*completion=*/nullptr,
  275. /*has_updates=*/true);
  276. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetMaskLayer(mask_layer1));
  277. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  278. // Set up the impl layers after the full tree is constructed, including the
  279. // mask layer.
  280. SkBlendMode arbitrary_blend_mode = SkBlendMode::kMultiply;
  281. std::unique_ptr<LayerImpl> top_impl =
  282. LayerImpl::Create(host_impl_.active_tree(), top->id());
  283. std::unique_ptr<LayerImpl> child_impl =
  284. LayerImpl::Create(host_impl_.active_tree(), child->id());
  285. std::unique_ptr<LayerImpl> child2_impl =
  286. LayerImpl::Create(host_impl_.active_tree(), child2->id());
  287. std::unique_ptr<LayerImpl> grand_child_impl =
  288. LayerImpl::Create(host_impl_.active_tree(), grand_child->id());
  289. std::unique_ptr<LayerImpl> mask_layer1_impl =
  290. mask_layer1->CreateLayerImpl(host_impl_.active_tree());
  291. host_impl_.active_tree()->set_source_frame_number(
  292. host_impl_.active_tree()->source_frame_number() + 1);
  293. auto& unsafe_state = layer_tree_host_->GetThreadUnsafeCommitState();
  294. std::unique_ptr<CommitState> commit_state = layer_tree_host_->WillCommit(
  295. /*completion=*/nullptr, /*has_updates=*/true);
  296. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  297. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  298. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  299. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  300. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  301. unsafe_state);
  302. mask_layer1->PushPropertiesTo(mask_layer1_impl.get(), *commit_state,
  303. unsafe_state););
  304. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  305. // Once there is a mask layer, resizes require subtree properties to update.
  306. arbitrary_size = gfx::Size(11, 22);
  307. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetBounds(arbitrary_size));
  308. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  309. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(mask_layer1->SetBounds(arbitrary_size));
  310. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  311. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetMasksToBounds(true));
  312. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  313. /*has_updates=*/true);
  314. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  315. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  316. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  317. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  318. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  319. unsafe_state));
  320. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  321. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  322. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetContentsOpaque(true));
  323. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  324. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  325. /*has_updates=*/true);
  326. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  327. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  328. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  329. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  330. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  331. unsafe_state));
  332. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  333. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetTrilinearFiltering(true));
  334. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  335. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  336. /*has_updates=*/true);
  337. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  338. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  339. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  340. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  341. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  342. unsafe_state));
  343. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  344. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetTrilinearFiltering(false));
  345. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  346. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  347. /*has_updates=*/true);
  348. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  349. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  350. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  351. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  352. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  353. unsafe_state));
  354. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  355. top->SetRoundedCorner({1, 2, 3, 4});
  356. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  357. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetIsFastRoundedCorner(true));
  358. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  359. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  360. /*has_updates=*/true);
  361. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  362. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  363. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  364. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  365. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  366. unsafe_state));
  367. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  368. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetHideLayerAndSubtree(true));
  369. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  370. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  371. /*has_updates=*/true);
  372. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  373. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  374. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  375. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  376. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  377. unsafe_state));
  378. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  379. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetBlendMode(arbitrary_blend_mode));
  380. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  381. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  382. /*has_updates=*/true);
  383. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  384. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  385. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  386. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  387. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  388. unsafe_state));
  389. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  390. // Should be a different size than previous call, to ensure it marks tree
  391. // changed.
  392. arbitrary_size = gfx::Size(111, 222);
  393. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetBounds(arbitrary_size));
  394. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  395. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(mask_layer1->SetBounds(arbitrary_size));
  396. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  397. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  398. /*has_updates=*/true);
  399. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  400. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  401. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  402. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  403. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  404. unsafe_state));
  405. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  406. FilterOperations arbitrary_filters;
  407. arbitrary_filters.Append(FilterOperation::CreateOpacityFilter(0.5f));
  408. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(top->SetFilters(arbitrary_filters));
  409. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  410. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  411. /*has_updates=*/true);
  412. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  413. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  414. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  415. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  416. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  417. unsafe_state));
  418. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  419. EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
  420. top->SetBackdropFilters(arbitrary_filters));
  421. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  422. /*has_updates=*/true);
  423. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  424. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  425. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  426. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  427. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  428. unsafe_state));
  429. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  430. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  431. gfx::PointF arbitrary_point_f = gfx::PointF(0.125f, 0.25f);
  432. top->SetPosition(arbitrary_point_f);
  433. TransformNode* node =
  434. layer_tree_host_->property_trees()->transform_tree_mutable().Node(
  435. top->transform_tree_index());
  436. EXPECT_TRUE(node->transform_changed);
  437. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  438. /*has_updates=*/true);
  439. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  440. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  441. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  442. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  443. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  444. unsafe_state);
  445. layer_tree_host_->property_trees()->ResetAllChangeTracking());
  446. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  447. EXPECT_FALSE(node->transform_changed);
  448. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  449. child->SetPosition(arbitrary_point_f);
  450. node = layer_tree_host_->property_trees()->transform_tree_mutable().Node(
  451. child->transform_tree_index());
  452. EXPECT_TRUE(node->transform_changed);
  453. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  454. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  455. /*has_updates=*/true);
  456. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  457. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  458. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  459. unsafe_state);
  460. layer_tree_host_->property_trees()->ResetAllChangeTracking());
  461. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  462. node = layer_tree_host_->property_trees()->transform_tree_mutable().Node(
  463. child->transform_tree_index());
  464. EXPECT_FALSE(node->transform_changed);
  465. gfx::Point3F arbitrary_point_3f = gfx::Point3F(0.125f, 0.25f, 0.f);
  466. top->SetTransformOrigin(arbitrary_point_3f);
  467. node = layer_tree_host_->property_trees()->transform_tree_mutable().Node(
  468. top->transform_tree_index());
  469. EXPECT_TRUE(node->transform_changed);
  470. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  471. commit_state = layer_tree_host_->WillCommit(/*completion=*/nullptr,
  472. /*has_updates=*/true);
  473. EXECUTE_AND_VERIFY_SUBTREE_CHANGES_RESET(
  474. top->PushPropertiesTo(top_impl.get(), *commit_state, unsafe_state);
  475. child->PushPropertiesTo(child_impl.get(), *commit_state, unsafe_state);
  476. child2->PushPropertiesTo(child2_impl.get(), *commit_state, unsafe_state);
  477. grand_child->PushPropertiesTo(grand_child_impl.get(), *commit_state,
  478. unsafe_state);
  479. layer_tree_host_->property_trees()->ResetAllChangeTracking());
  480. layer_tree_host_->CommitComplete({base::TimeTicks(), base::TimeTicks::Now()});
  481. gfx::Transform arbitrary_transform;
  482. arbitrary_transform.Scale3d(0.1f, 0.2f, 0.3f);
  483. top->SetTransform(arbitrary_transform);
  484. node = layer_tree_host_->property_trees()->transform_tree_mutable().Node(
  485. top->transform_tree_index());
  486. EXPECT_TRUE(node->transform_changed);
  487. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  488. }
  489. TEST_F(LayerTest, AddAndRemoveChild) {
  490. scoped_refptr<Layer> parent = Layer::Create();
  491. scoped_refptr<Layer> child = Layer::Create();
  492. // Upon creation, layers should not have children or parent.
  493. ASSERT_EQ(0U, parent->children().size());
  494. EXPECT_FALSE(child->parent());
  495. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
  496. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child));
  497. ASSERT_EQ(1U, parent->children().size());
  498. EXPECT_EQ(child.get(), parent->children()[0]);
  499. EXPECT_EQ(parent.get(), child->parent());
  500. EXPECT_EQ(parent.get(), child->RootLayer());
  501. EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent());
  502. }
  503. TEST_F(LayerTest, SetMaskLayer) {
  504. scoped_refptr<Layer> parent = Layer::Create();
  505. FakeContentLayerClient client;
  506. scoped_refptr<PictureLayer> mask = PictureLayer::Create(&client);
  507. mask->SetPosition(gfx::PointF(88, 99));
  508. parent->SetMaskLayer(mask);
  509. ASSERT_EQ(1u, parent->children().size());
  510. EXPECT_EQ(parent.get(), mask->parent());
  511. EXPECT_EQ(mask.get(), parent->children()[0]);
  512. EXPECT_TRUE(parent->mask_layer());
  513. // Should ignore mask layer's position.
  514. EXPECT_TRUE(mask->position().IsOrigin());
  515. mask->SetPosition(gfx::PointF(11, 22));
  516. EXPECT_TRUE(mask->position().IsOrigin());
  517. parent->SetMaskLayer(mask);
  518. ASSERT_EQ(1u, parent->children().size());
  519. EXPECT_EQ(parent.get(), mask->parent());
  520. EXPECT_EQ(mask.get(), parent->children()[0]);
  521. EXPECT_TRUE(parent->mask_layer());
  522. scoped_refptr<PictureLayer> mask2 = PictureLayer::Create(&client);
  523. parent->SetMaskLayer(mask2);
  524. EXPECT_FALSE(mask->parent());
  525. ASSERT_EQ(1u, parent->children().size());
  526. EXPECT_EQ(parent.get(), mask2->parent());
  527. EXPECT_EQ(mask2.get(), parent->children()[0]);
  528. EXPECT_TRUE(parent->mask_layer());
  529. parent->SetMaskLayer(nullptr);
  530. EXPECT_EQ(0u, parent->children().size());
  531. EXPECT_FALSE(mask2->parent());
  532. EXPECT_FALSE(parent->mask_layer());
  533. }
  534. TEST_F(LayerTest, RemoveMaskLayerFromParent) {
  535. scoped_refptr<Layer> parent = Layer::Create();
  536. FakeContentLayerClient client;
  537. scoped_refptr<PictureLayer> mask = PictureLayer::Create(&client);
  538. parent->SetMaskLayer(mask);
  539. mask->RemoveFromParent();
  540. EXPECT_EQ(0u, parent->children().size());
  541. EXPECT_FALSE(mask->parent());
  542. EXPECT_FALSE(parent->mask_layer());
  543. scoped_refptr<PictureLayer> mask2 = PictureLayer::Create(&client);
  544. parent->SetMaskLayer(mask2);
  545. EXPECT_TRUE(parent->mask_layer());
  546. }
  547. TEST_F(LayerTest, AddChildAfterSetMaskLayer) {
  548. scoped_refptr<Layer> parent = Layer::Create();
  549. FakeContentLayerClient client;
  550. scoped_refptr<PictureLayer> mask = PictureLayer::Create(&client);
  551. parent->SetMaskLayer(mask);
  552. EXPECT_TRUE(parent->mask_layer());
  553. parent->AddChild(Layer::Create());
  554. EXPECT_EQ(mask.get(), parent->children().back().get());
  555. EXPECT_TRUE(parent->mask_layer());
  556. parent->InsertChild(Layer::Create(), parent->children().size());
  557. EXPECT_EQ(mask.get(), parent->children().back().get());
  558. EXPECT_TRUE(parent->mask_layer());
  559. }
  560. TEST_F(LayerTest, AddSameChildTwice) {
  561. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
  562. scoped_refptr<Layer> parent = Layer::Create();
  563. scoped_refptr<Layer> child = Layer::Create();
  564. layer_tree_host_->SetRootLayer(parent);
  565. ASSERT_EQ(0u, parent->children().size());
  566. parent->AddChild(child);
  567. ASSERT_EQ(1u, parent->children().size());
  568. EXPECT_EQ(parent.get(), child->parent());
  569. parent->AddChild(child);
  570. ASSERT_EQ(1u, parent->children().size());
  571. EXPECT_EQ(parent.get(), child->parent());
  572. }
  573. TEST_F(LayerTest, ReorderChildren) {
  574. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
  575. scoped_refptr<Layer> parent = Layer::Create();
  576. scoped_refptr<Layer> child1 = Layer::Create();
  577. scoped_refptr<Layer> child2 = Layer::Create();
  578. scoped_refptr<Layer> child3 = Layer::Create();
  579. layer_tree_host_->SetRootLayer(parent);
  580. parent->AddChild(child1);
  581. parent->AddChild(child2);
  582. parent->AddChild(child3);
  583. EXPECT_EQ(child1, parent->children()[0]);
  584. EXPECT_EQ(child2, parent->children()[1]);
  585. EXPECT_EQ(child3, parent->children()[2]);
  586. // This is normally done by TreeSynchronizer::PushLayerProperties().
  587. layer_tree_host_->GetPendingCommitState()
  588. ->layers_that_should_push_properties.clear();
  589. LayerList new_children_order;
  590. new_children_order.emplace_back(child3);
  591. new_children_order.emplace_back(child1);
  592. new_children_order.emplace_back(child2);
  593. parent->ReorderChildren(&new_children_order);
  594. EXPECT_EQ(child3, parent->children()[0]);
  595. EXPECT_EQ(child1, parent->children()[1]);
  596. EXPECT_EQ(child2, parent->children()[2]);
  597. for (const auto& child : parent->children()) {
  598. EXPECT_FALSE(base::Contains(layer_tree_host_->GetPendingCommitState()
  599. ->layers_that_should_push_properties,
  600. child.get()));
  601. EXPECT_TRUE(child->subtree_property_changed());
  602. }
  603. }
  604. TEST_F(LayerTest, InsertChild) {
  605. scoped_refptr<Layer> parent = Layer::Create();
  606. scoped_refptr<Layer> child1 = Layer::Create();
  607. scoped_refptr<Layer> child2 = Layer::Create();
  608. scoped_refptr<Layer> child3 = Layer::Create();
  609. scoped_refptr<Layer> child4 = Layer::Create();
  610. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
  611. ASSERT_EQ(0U, parent->children().size());
  612. // Case 1: inserting to empty list.
  613. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child3, 0));
  614. ASSERT_EQ(1U, parent->children().size());
  615. EXPECT_EQ(child3, parent->children()[0]);
  616. EXPECT_EQ(parent.get(), child3->parent());
  617. // Case 2: inserting to beginning of list
  618. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
  619. ASSERT_EQ(2U, parent->children().size());
  620. EXPECT_EQ(child1, parent->children()[0]);
  621. EXPECT_EQ(child3, parent->children()[1]);
  622. EXPECT_EQ(parent.get(), child1->parent());
  623. // Case 3: inserting to middle of list
  624. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
  625. ASSERT_EQ(3U, parent->children().size());
  626. EXPECT_EQ(child1, parent->children()[0]);
  627. EXPECT_EQ(child2, parent->children()[1]);
  628. EXPECT_EQ(child3, parent->children()[2]);
  629. EXPECT_EQ(parent.get(), child2->parent());
  630. // Case 4: inserting to end of list
  631. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3));
  632. ASSERT_EQ(4U, parent->children().size());
  633. EXPECT_EQ(child1, parent->children()[0]);
  634. EXPECT_EQ(child2, parent->children()[1]);
  635. EXPECT_EQ(child3, parent->children()[2]);
  636. EXPECT_EQ(child4, parent->children()[3]);
  637. EXPECT_EQ(parent.get(), child4->parent());
  638. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
  639. }
  640. TEST_F(LayerTest, InsertChildPastEndOfList) {
  641. scoped_refptr<Layer> parent = Layer::Create();
  642. scoped_refptr<Layer> child1 = Layer::Create();
  643. scoped_refptr<Layer> child2 = Layer::Create();
  644. ASSERT_EQ(0U, parent->children().size());
  645. // insert to an out-of-bounds index
  646. parent->InsertChild(child1, 53);
  647. ASSERT_EQ(1U, parent->children().size());
  648. EXPECT_EQ(child1, parent->children()[0]);
  649. // insert another child to out-of-bounds, when list is not already empty.
  650. parent->InsertChild(child2, 2459);
  651. ASSERT_EQ(2U, parent->children().size());
  652. EXPECT_EQ(child1, parent->children()[0]);
  653. EXPECT_EQ(child2, parent->children()[1]);
  654. }
  655. TEST_F(LayerTest, InsertSameChildTwice) {
  656. scoped_refptr<Layer> parent = Layer::Create();
  657. scoped_refptr<Layer> child1 = Layer::Create();
  658. scoped_refptr<Layer> child2 = Layer::Create();
  659. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
  660. ASSERT_EQ(0U, parent->children().size());
  661. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
  662. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
  663. ASSERT_EQ(2U, parent->children().size());
  664. EXPECT_EQ(child1, parent->children()[0]);
  665. EXPECT_EQ(child2, parent->children()[1]);
  666. // Inserting the same child again should cause the child to be removed and
  667. // re-inserted at the new location.
  668. EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent->InsertChild(child1, 1));
  669. // child1 should now be at the end of the list.
  670. ASSERT_EQ(2U, parent->children().size());
  671. EXPECT_EQ(child2, parent->children()[0]);
  672. EXPECT_EQ(child1, parent->children()[1]);
  673. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
  674. }
  675. TEST_F(LayerTest, ReplaceChildWithNewChild) {
  676. CreateSimpleTestTree();
  677. scoped_refptr<Layer> child4 = Layer::Create();
  678. EXPECT_FALSE(child4->parent());
  679. EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1),
  680. parent_->ReplaceChild(child2_.get(), child4));
  681. EXPECT_FALSE(LayerNeedsDisplay(parent_.get()));
  682. EXPECT_FALSE(LayerNeedsDisplay(child1_.get()));
  683. EXPECT_FALSE(LayerNeedsDisplay(child2_.get()));
  684. EXPECT_FALSE(LayerNeedsDisplay(child3_.get()));
  685. EXPECT_FALSE(LayerNeedsDisplay(child4.get()));
  686. ASSERT_EQ(static_cast<size_t>(3), parent_->children().size());
  687. EXPECT_EQ(child1_, parent_->children()[0]);
  688. EXPECT_EQ(child4, parent_->children()[1]);
  689. EXPECT_EQ(child3_, parent_->children()[2]);
  690. EXPECT_EQ(parent_.get(), child4->parent());
  691. EXPECT_FALSE(child2_->parent());
  692. }
  693. TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) {
  694. CreateSimpleTestTree();
  695. // create another simple tree with test_layer and child4.
  696. scoped_refptr<Layer> test_layer = Layer::Create();
  697. scoped_refptr<Layer> child4 = Layer::Create();
  698. test_layer->AddChild(child4);
  699. ASSERT_EQ(1U, test_layer->children().size());
  700. EXPECT_EQ(child4, test_layer->children()[0]);
  701. EXPECT_EQ(test_layer.get(), child4->parent());
  702. EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1),
  703. parent_->ReplaceChild(child2_.get(), child4));
  704. ASSERT_EQ(3U, parent_->children().size());
  705. EXPECT_EQ(child1_, parent_->children()[0]);
  706. EXPECT_EQ(child4, parent_->children()[1]);
  707. EXPECT_EQ(child3_, parent_->children()[2]);
  708. EXPECT_EQ(parent_.get(), child4->parent());
  709. // test_layer should no longer have child4,
  710. // and child2 should no longer have a parent.
  711. ASSERT_EQ(0U, test_layer->children().size());
  712. EXPECT_FALSE(child2_->parent());
  713. }
  714. TEST_F(LayerTest, ReplaceChildWithSameChild) {
  715. CreateSimpleTestTree();
  716. // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its
  717. // the same child.
  718. parent_->ReplaceChild(child2_.get(), child2_);
  719. VerifyTestTreeInitialState();
  720. }
  721. TEST_F(LayerTest, RemoveAllChildren) {
  722. CreateSimpleTestTree();
  723. EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
  724. ASSERT_EQ(0U, parent_->children().size());
  725. EXPECT_FALSE(child1_->parent());
  726. EXPECT_FALSE(child2_->parent());
  727. EXPECT_FALSE(child3_->parent());
  728. }
  729. TEST_F(LayerTest, HasAncestor) {
  730. scoped_refptr<Layer> parent = Layer::Create();
  731. EXPECT_FALSE(parent->HasAncestor(parent.get()));
  732. scoped_refptr<Layer> child = Layer::Create();
  733. parent->AddChild(child);
  734. EXPECT_FALSE(child->HasAncestor(child.get()));
  735. EXPECT_TRUE(child->HasAncestor(parent.get()));
  736. EXPECT_FALSE(parent->HasAncestor(child.get()));
  737. scoped_refptr<Layer> child_child = Layer::Create();
  738. child->AddChild(child_child);
  739. EXPECT_FALSE(child_child->HasAncestor(child_child.get()));
  740. EXPECT_TRUE(child_child->HasAncestor(parent.get()));
  741. EXPECT_TRUE(child_child->HasAncestor(child.get()));
  742. EXPECT_FALSE(parent->HasAncestor(child.get()));
  743. EXPECT_FALSE(parent->HasAncestor(child_child.get()));
  744. }
  745. TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
  746. CreateSimpleTestTree();
  747. // For this test we don't care about SetNeedsFullTreeSync calls.
  748. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
  749. scoped_refptr<Layer> child4 = Layer::Create();
  750. EXPECT_EQ(parent_.get(), parent_->RootLayer());
  751. EXPECT_EQ(parent_.get(), child1_->RootLayer());
  752. EXPECT_EQ(parent_.get(), child2_->RootLayer());
  753. EXPECT_EQ(parent_.get(), child3_->RootLayer());
  754. EXPECT_EQ(child4.get(), child4->RootLayer());
  755. EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
  756. EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
  757. EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
  758. child1_->RemoveFromParent();
  759. // |child1| and its children, grand_child1 and grand_child2 are now on a
  760. // separate subtree.
  761. EXPECT_EQ(parent_.get(), parent_->RootLayer());
  762. EXPECT_EQ(child1_.get(), child1_->RootLayer());
  763. EXPECT_EQ(parent_.get(), child2_->RootLayer());
  764. EXPECT_EQ(parent_.get(), child3_->RootLayer());
  765. EXPECT_EQ(child4.get(), child4->RootLayer());
  766. EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
  767. EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
  768. EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
  769. grand_child3_->AddChild(child4);
  770. EXPECT_EQ(parent_.get(), parent_->RootLayer());
  771. EXPECT_EQ(child1_.get(), child1_->RootLayer());
  772. EXPECT_EQ(parent_.get(), child2_->RootLayer());
  773. EXPECT_EQ(parent_.get(), child3_->RootLayer());
  774. EXPECT_EQ(parent_.get(), child4->RootLayer());
  775. EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
  776. EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
  777. EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
  778. child2_->ReplaceChild(grand_child3_.get(), child1_);
  779. // |grand_child3| gets orphaned and the child1 subtree gets planted back
  780. // into the tree under child2.
  781. EXPECT_EQ(parent_.get(), parent_->RootLayer());
  782. EXPECT_EQ(parent_.get(), child1_->RootLayer());
  783. EXPECT_EQ(parent_.get(), child2_->RootLayer());
  784. EXPECT_EQ(parent_.get(), child3_->RootLayer());
  785. EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
  786. EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
  787. EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
  788. EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
  789. }
  790. TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
  791. // The semantics for SetNeedsDisplay which are tested here:
  792. // 1. sets NeedsDisplay flag appropriately.
  793. // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
  794. // SetNeedsDisplay.
  795. scoped_refptr<Layer> test_layer = Layer::Create();
  796. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
  797. layer_tree_host_->SetRootLayer(test_layer));
  798. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetIsDrawable(true));
  799. gfx::Size test_bounds = gfx::Size(501, 508);
  800. gfx::Rect dirty_rect = gfx::Rect(10, 15, 1, 2);
  801. gfx::Rect out_of_bounds_dirty_rect = gfx::Rect(400, 405, 500, 502);
  802. // Before anything, test_layer should not be dirty.
  803. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  804. // This is just initialization, but SetNeedsCommit behavior is verified
  805. // anyway to avoid warnings.
  806. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetBounds(test_bounds));
  807. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  808. // The real test begins here.
  809. SimulateCommitForLayer(test_layer.get());
  810. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  811. // Case 1: Layer should accept dirty rects that go beyond its bounds.
  812. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  813. EXPECT_SET_NEEDS_UPDATE(
  814. 1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
  815. EXPECT_TRUE(LayerNeedsDisplay(test_layer.get()));
  816. SimulateCommitForLayer(test_layer.get());
  817. // Case 2: SetNeedsDisplay() without the dirty rect arg.
  818. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  819. EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
  820. EXPECT_TRUE(LayerNeedsDisplay(test_layer.get()));
  821. SimulateCommitForLayer(test_layer.get());
  822. // Case 3: SetNeedsDisplay() with an empty rect.
  823. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  824. EXPECT_SET_NEEDS_COMMIT_WAS_NOT_CALLED(
  825. test_layer->SetNeedsDisplayRect(gfx::Rect()));
  826. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  827. SimulateCommitForLayer(test_layer.get());
  828. // Case 4: SetNeedsDisplay() with a non-drawable layer
  829. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetIsDrawable(false));
  830. SimulateCommitForLayer(test_layer.get());
  831. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  832. EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty_rect));
  833. EXPECT_TRUE(LayerNeedsDisplay(test_layer.get()));
  834. }
  835. TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
  836. scoped_refptr<Layer> test_layer = Layer::Create();
  837. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
  838. layer_tree_host_->SetRootLayer(test_layer));
  839. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetIsDrawable(true));
  840. FakeContentLayerClient client;
  841. scoped_refptr<PictureLayer> mask_layer1 = PictureLayer::Create(&client);
  842. // sanity check of initial test condition
  843. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  844. // Next, test properties that should call SetNeedsCommit (but not
  845. // SetNeedsDisplay). All properties need to be set to new values in order for
  846. // SetNeedsCommit to be called.
  847. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  848. test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
  849. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  850. test_layer->SetBackgroundColor(SkColors::kLtGray));
  851. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetMasksToBounds(true));
  852. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  853. test_layer->SetClipRect(gfx::Rect(1, 2, 3, 4)));
  854. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  855. test_layer->SetRoundedCorner({1, 2, 3, 4}));
  856. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetIsFastRoundedCorner(true));
  857. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetOpacity(0.5f));
  858. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  859. test_layer->SetBlendMode(SkBlendMode::kHue));
  860. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetContentsOpaque(true));
  861. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  862. test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
  863. // We can use any layer pointer here since we aren't syncing for real.
  864. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  865. test_layer->SetScrollable(gfx::Size(1, 1)));
  866. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  867. test_layer->SetUserScrollable(true, false));
  868. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  869. test_layer->SetScrollOffset(gfx::PointF(10, 10)));
  870. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  871. test_layer->SetNonFastScrollableRegion(Region(gfx::Rect(1, 1, 2, 2))));
  872. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  873. test_layer->SetTransform(gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
  874. TouchActionRegion touch_action_region;
  875. touch_action_region.Union(TouchAction::kNone, gfx::Rect(10, 10));
  876. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  877. test_layer->SetTouchActionRegion(std::move(touch_action_region)));
  878. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  879. test_layer->SetForceRenderSurfaceForTesting(true));
  880. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetHideLayerAndSubtree(true));
  881. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetElementId(ElementId(2)));
  882. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  883. test_layer->SetCaptureBounds(viz::RegionCaptureBounds(
  884. base::flat_map<viz::RegionCaptureCropId, gfx::Rect>{
  885. {viz::RegionCaptureCropId(123u, 456u),
  886. gfx::Rect(0, 0, 640, 480)}})));
  887. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(mask_layer1));
  888. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  889. // The above tests should not have caused a change to the needs_display
  890. // flag.
  891. EXPECT_FALSE(LayerNeedsDisplay(test_layer.get()));
  892. // As layers are removed from the tree, they will cause a tree sync.
  893. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
  894. }
  895. TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
  896. scoped_refptr<Layer> test_layer = Layer::Create();
  897. std::unique_ptr<LayerImpl> impl_layer =
  898. LayerImpl::Create(host_impl_.active_tree(), 1);
  899. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
  900. layer_tree_host_->SetRootLayer(test_layer));
  901. host_impl_.active_tree()->SetRootLayerForTesting(std::move(impl_layer));
  902. LayerImpl* impl_layer_ptr = host_impl_.active_tree()->LayerById(1);
  903. test_layer->SetNeedsDisplayRect(gfx::Rect(5, 5));
  904. CommitAndPushProperties(test_layer.get(), impl_layer_ptr);
  905. EXPECT_EQ(gfx::Rect(0, 0, 5, 5), impl_layer_ptr->update_rect());
  906. // The LayerImpl's update_rect() should be accumulated here, since we did
  907. // not do anything to clear it.
  908. test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
  909. CommitAndPushProperties(test_layer.get(), impl_layer_ptr);
  910. EXPECT_EQ(gfx::Rect(0, 0, 15, 15), impl_layer_ptr->update_rect());
  911. // If we do clear the LayerImpl side, then the next update_rect() should be
  912. // fresh without accumulation.
  913. host_impl_.active_tree()->ResetAllChangeTracking();
  914. test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
  915. CommitAndPushProperties(test_layer.get(), impl_layer_ptr);
  916. EXPECT_EQ(gfx::Rect(10, 10, 5, 5), impl_layer_ptr->update_rect());
  917. }
  918. TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
  919. scoped_refptr<Layer> test_layer = Layer::Create();
  920. std::unique_ptr<LayerImpl> impl_layer =
  921. LayerImpl::Create(host_impl_.active_tree(), 1);
  922. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
  923. layer_tree_host_->SetRootLayer(test_layer));
  924. gfx::Transform transform;
  925. transform.Rotate(45.0);
  926. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetTransform(transform));
  927. EXPECT_FALSE(impl_layer->LayerPropertyChanged());
  928. CommitAndPushProperties(test_layer.get(), impl_layer.get());
  929. EXPECT_TRUE(impl_layer->LayerPropertyChanged());
  930. EXPECT_FALSE(impl_layer->LayerPropertyChangedFromPropertyTrees());
  931. EXPECT_TRUE(impl_layer->LayerPropertyChangedNotFromPropertyTrees());
  932. }
  933. TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForRoundCorner) {
  934. scoped_refptr<Layer> test_layer = Layer::Create();
  935. test_layer->SetMasksToBounds(true);
  936. std::unique_ptr<LayerImpl> impl_layer =
  937. LayerImpl::Create(host_impl_.active_tree(), 1);
  938. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
  939. layer_tree_host_->SetRootLayer(test_layer));
  940. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(
  941. test_layer->SetRoundedCorner({1, 2, 3, 4}));
  942. EXPECT_FALSE(impl_layer->LayerPropertyChanged());
  943. CommitAndPushProperties(test_layer.get(), impl_layer.get());
  944. EXPECT_TRUE(impl_layer->LayerPropertyChanged());
  945. EXPECT_FALSE(impl_layer->LayerPropertyChangedFromPropertyTrees());
  946. EXPECT_TRUE(impl_layer->LayerPropertyChangedNotFromPropertyTrees());
  947. }
  948. TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
  949. scoped_refptr<Layer> test_layer = Layer::Create();
  950. std::unique_ptr<LayerImpl> impl_layer =
  951. LayerImpl::Create(host_impl_.active_tree(), 1);
  952. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
  953. layer_tree_host_->SetRootLayer(test_layer));
  954. EXPECT_SET_NEEDS_COMMIT_WAS_CALLED(test_layer->SetOpacity(0.5f));
  955. EXPECT_FALSE(impl_layer->LayerPropertyChanged());
  956. CommitAndPushProperties(test_layer.get(), impl_layer.get());
  957. EXPECT_TRUE(impl_layer->LayerPropertyChanged());
  958. EXPECT_FALSE(impl_layer->LayerPropertyChangedFromPropertyTrees());
  959. EXPECT_TRUE(impl_layer->LayerPropertyChangedNotFromPropertyTrees());
  960. }
  961. TEST_F(LayerTest, MaskHasParent) {
  962. scoped_refptr<Layer> parent = Layer::Create();
  963. scoped_refptr<Layer> child = Layer::Create();
  964. FakeContentLayerClient client;
  965. scoped_refptr<PictureLayer> mask = PictureLayer::Create(&client);
  966. scoped_refptr<PictureLayer> mask_replacement = PictureLayer::Create(&client);
  967. parent->AddChild(child);
  968. child->SetMaskLayer(mask);
  969. EXPECT_EQ(parent.get(), child->parent());
  970. EXPECT_EQ(child.get(), mask->parent());
  971. child->SetMaskLayer(mask_replacement);
  972. EXPECT_EQ(nullptr, mask->parent());
  973. EXPECT_EQ(child.get(), mask_replacement->parent());
  974. }
  975. class LayerTreeHostFactory {
  976. public:
  977. std::unique_ptr<LayerTreeHost> Create(MutatorHost* mutator_host) {
  978. return Create(LayerTreeSettings(), mutator_host);
  979. }
  980. std::unique_ptr<LayerTreeHost> Create(LayerTreeSettings settings,
  981. MutatorHost* mutator_host) {
  982. LayerTreeHost::InitParams params;
  983. params.client = &client_;
  984. params.task_graph_runner = &task_graph_runner_;
  985. params.settings = &settings;
  986. params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
  987. params.mutator_host = mutator_host;
  988. return LayerTreeHost::CreateSingleThreaded(&single_thread_client_,
  989. std::move(params));
  990. }
  991. private:
  992. FakeLayerTreeHostClient client_;
  993. StubLayerTreeHostSingleThreadClient single_thread_client_;
  994. TestTaskGraphRunner task_graph_runner_;
  995. };
  996. void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
  997. EXPECT_EQ(host, layer->layer_tree_host());
  998. for (size_t i = 0; i < layer->children().size(); ++i)
  999. AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
  1000. }
  1001. class LayerLayerTreeHostTest : public testing::Test {};
  1002. TEST_F(LayerLayerTreeHostTest, EnteringTree) {
  1003. scoped_refptr<Layer> parent = Layer::Create();
  1004. scoped_refptr<Layer> child = Layer::Create();
  1005. FakeContentLayerClient client;
  1006. scoped_refptr<PictureLayer> mask = PictureLayer::Create(&client);
  1007. // Set up a detached tree of layers. The host pointer should be nil for
  1008. // these layers.
  1009. parent->AddChild(child);
  1010. child->SetMaskLayer(mask);
  1011. AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
  1012. LayerTreeHostFactory factory;
  1013. auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  1014. std::unique_ptr<LayerTreeHost> layer_tree_host =
  1015. factory.Create(animation_host.get());
  1016. // Setting the root layer should set the host pointer for all layers in the
  1017. // tree.
  1018. layer_tree_host->SetRootLayer(parent.get());
  1019. AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
  1020. // Clearing the root layer should also clear out the host pointers for all
  1021. // layers in the tree.
  1022. layer_tree_host->SetRootLayer(nullptr);
  1023. AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
  1024. }
  1025. TEST_F(LayerLayerTreeHostTest, AddingLayerSubtree) {
  1026. scoped_refptr<Layer> parent = Layer::Create();
  1027. LayerTreeHostFactory factory;
  1028. auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  1029. std::unique_ptr<LayerTreeHost> layer_tree_host =
  1030. factory.Create(animation_host.get());
  1031. layer_tree_host->SetRootLayer(parent.get());
  1032. EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
  1033. // Adding a subtree to a layer already associated with a host should set the
  1034. // host pointer on all layers in that subtree.
  1035. scoped_refptr<Layer> child = Layer::Create();
  1036. scoped_refptr<Layer> grand_child = Layer::Create();
  1037. child->AddChild(grand_child);
  1038. // Masks should pick up the new host too.
  1039. FakeContentLayerClient client;
  1040. scoped_refptr<PictureLayer> child_mask = PictureLayer::Create(&client);
  1041. child->SetMaskLayer(child_mask);
  1042. parent->AddChild(child);
  1043. AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
  1044. layer_tree_host->SetRootLayer(nullptr);
  1045. }
  1046. TEST_F(LayerLayerTreeHostTest, ChangeHost) {
  1047. scoped_refptr<Layer> parent = Layer::Create();
  1048. scoped_refptr<Layer> child = Layer::Create();
  1049. FakeContentLayerClient client;
  1050. scoped_refptr<PictureLayer> mask = PictureLayer::Create(&client);
  1051. // Same setup as the previous test.
  1052. parent->AddChild(child);
  1053. child->SetMaskLayer(mask);
  1054. LayerTreeHostFactory factory;
  1055. auto animation_host1 = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  1056. std::unique_ptr<LayerTreeHost> first_layer_tree_host =
  1057. factory.Create(animation_host1.get());
  1058. first_layer_tree_host->SetRootLayer(parent.get());
  1059. AssertLayerTreeHostMatchesForSubtree(parent.get(),
  1060. first_layer_tree_host.get());
  1061. // Now re-root the tree to a new host (simulating what we do on a context
  1062. // lost event). This should update the host pointers for all layers in the
  1063. // tree.
  1064. auto animation_host2 = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  1065. std::unique_ptr<LayerTreeHost> second_layer_tree_host =
  1066. factory.Create(animation_host2.get());
  1067. second_layer_tree_host->SetRootLayer(parent.get());
  1068. AssertLayerTreeHostMatchesForSubtree(parent.get(),
  1069. second_layer_tree_host.get());
  1070. second_layer_tree_host->SetRootLayer(nullptr);
  1071. }
  1072. TEST_F(LayerLayerTreeHostTest, ChangeHostInSubtree) {
  1073. scoped_refptr<Layer> first_parent = Layer::Create();
  1074. scoped_refptr<Layer> first_child = Layer::Create();
  1075. scoped_refptr<Layer> second_parent = Layer::Create();
  1076. scoped_refptr<Layer> second_child = Layer::Create();
  1077. scoped_refptr<Layer> second_grand_child = Layer::Create();
  1078. // First put all children under the first parent and set the first host.
  1079. first_parent->AddChild(first_child);
  1080. second_child->AddChild(second_grand_child);
  1081. first_parent->AddChild(second_child);
  1082. LayerTreeHostFactory factory;
  1083. auto animation_host1 = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  1084. std::unique_ptr<LayerTreeHost> first_layer_tree_host =
  1085. factory.Create(animation_host1.get());
  1086. first_layer_tree_host->SetRootLayer(first_parent.get());
  1087. AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
  1088. first_layer_tree_host.get());
  1089. // Now reparent the subtree starting at second_child to a layer in a
  1090. // different tree.
  1091. auto animation_host2 = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  1092. std::unique_ptr<LayerTreeHost> second_layer_tree_host =
  1093. factory.Create(animation_host2.get());
  1094. second_layer_tree_host->SetRootLayer(second_parent.get());
  1095. second_parent->AddChild(second_child);
  1096. // The moved layer and its children should point to the new host.
  1097. EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
  1098. EXPECT_EQ(second_layer_tree_host.get(),
  1099. second_grand_child->layer_tree_host());
  1100. // Test over, cleanup time.
  1101. first_layer_tree_host->SetRootLayer(nullptr);
  1102. second_layer_tree_host->SetRootLayer(nullptr);
  1103. }
  1104. TEST_F(LayerLayerTreeHostTest, ReplaceMaskLayer) {
  1105. FakeContentLayerClient client;
  1106. scoped_refptr<Layer> parent = Layer::Create();
  1107. scoped_refptr<PictureLayer> mask = PictureLayer::Create(&client);
  1108. scoped_refptr<Layer> mask_child = Layer::Create();
  1109. scoped_refptr<PictureLayer> mask_replacement = PictureLayer::Create(&client);
  1110. parent->SetMaskLayer(mask);
  1111. mask->AddChild(mask_child);
  1112. LayerTreeHostFactory factory;
  1113. auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  1114. std::unique_ptr<LayerTreeHost> layer_tree_host =
  1115. factory.Create(animation_host.get());
  1116. layer_tree_host->SetRootLayer(parent.get());
  1117. AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
  1118. // Replacing the mask should clear out the old mask's subtree's host
  1119. // pointers.
  1120. parent->SetMaskLayer(mask_replacement);
  1121. EXPECT_EQ(nullptr, mask->layer_tree_host());
  1122. EXPECT_EQ(nullptr, mask_child->layer_tree_host());
  1123. // Test over, cleanup time.
  1124. layer_tree_host->SetRootLayer(nullptr);
  1125. }
  1126. TEST_F(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
  1127. scoped_refptr<Layer> root = Layer::Create();
  1128. scoped_refptr<Layer> child = Layer::Create();
  1129. root->AddChild(child);
  1130. LayerTreeHostFactory factory;
  1131. auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  1132. std::unique_ptr<LayerTreeHost> layer_tree_host =
  1133. factory.Create(animation_host.get());
  1134. layer_tree_host->SetRootLayer(root);
  1135. }
  1136. TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
  1137. LayerTreeHostFactory factory;
  1138. auto animation_host = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
  1139. std::unique_ptr<LayerTreeHost> layer_tree_host =
  1140. factory.Create(animation_host.get());
  1141. scoped_refptr<Layer> layer = Layer::Create();
  1142. layer_tree_host->SetRootLayer(layer);
  1143. for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
  1144. for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
  1145. for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
  1146. layer->SetContentsOpaque(!!contents_opaque);
  1147. layer->SetBackgroundColor(layer_opaque ? SkColors::kRed
  1148. : SkColors::kTransparent);
  1149. layer_tree_host->set_background_color(
  1150. host_opaque ? SkColors::kRed : SkColors::kTransparent);
  1151. layer_tree_host->property_trees()->set_needs_rebuild(true);
  1152. layer_tree_host->BuildPropertyTreesForTesting();
  1153. EXPECT_EQ(contents_opaque,
  1154. layer->SafeOpaqueBackgroundColor().isOpaque())
  1155. << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
  1156. << host_opaque << "\n";
  1157. }
  1158. }
  1159. }
  1160. }
  1161. class DrawsContentChangeLayer : public Layer {
  1162. public:
  1163. static scoped_refptr<DrawsContentChangeLayer> Create() {
  1164. return base::WrapRefCounted(new DrawsContentChangeLayer());
  1165. }
  1166. void SetLayerTreeHost(LayerTreeHost* host) override {
  1167. Layer::SetLayerTreeHost(host);
  1168. SetFakeDrawsContent(!fake_draws_content_);
  1169. }
  1170. bool HasDrawableContent() const override {
  1171. return fake_draws_content_ && Layer::HasDrawableContent();
  1172. }
  1173. void SetFakeDrawsContent(bool fake_draws_content) {
  1174. fake_draws_content_ = fake_draws_content;
  1175. SetDrawsContent(HasDrawableContent());
  1176. }
  1177. private:
  1178. DrawsContentChangeLayer() : fake_draws_content_(false) {}
  1179. ~DrawsContentChangeLayer() override = default;
  1180. bool fake_draws_content_;
  1181. };
  1182. TEST_F(LayerTest, DrawsContentChangedInSetLayerTreeHost) {
  1183. scoped_refptr<Layer> root_layer = Layer::Create();
  1184. scoped_refptr<DrawsContentChangeLayer> becomes_not_draws_content =
  1185. DrawsContentChangeLayer::Create();
  1186. scoped_refptr<DrawsContentChangeLayer> becomes_draws_content =
  1187. DrawsContentChangeLayer::Create();
  1188. root_layer->SetIsDrawable(true);
  1189. becomes_not_draws_content->SetIsDrawable(true);
  1190. becomes_not_draws_content->SetFakeDrawsContent(true);
  1191. EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
  1192. root_layer->AddChild(becomes_not_draws_content);
  1193. EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
  1194. becomes_draws_content->SetIsDrawable(true);
  1195. root_layer->AddChild(becomes_draws_content);
  1196. EXPECT_EQ(1, root_layer->NumDescendantsThatDrawContent());
  1197. }
  1198. TEST_F(LayerTest, PushUpdatesShouldHitTest) {
  1199. scoped_refptr<Layer> root_layer = Layer::Create();
  1200. std::unique_ptr<LayerImpl> impl_layer =
  1201. LayerImpl::Create(host_impl_.active_tree(), 1);
  1202. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
  1203. layer_tree_host_->SetRootLayer(root_layer));
  1204. // A layer that draws content should be hit testable.
  1205. root_layer->SetIsDrawable(true);
  1206. root_layer->SetHitTestable(true);
  1207. CommitAndPushProperties(root_layer.get(), impl_layer.get());
  1208. EXPECT_TRUE(impl_layer->draws_content());
  1209. EXPECT_TRUE(impl_layer->HitTestable());
  1210. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1211. // A layer that does not draw content and does not hit test without drawing
  1212. // content should not be hit testable.
  1213. root_layer->SetIsDrawable(false);
  1214. root_layer->SetHitTestable(false);
  1215. CommitAndPushProperties(root_layer.get(), impl_layer.get());
  1216. EXPECT_FALSE(impl_layer->draws_content());
  1217. EXPECT_FALSE(impl_layer->HitTestable());
  1218. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1219. // |SetHitTestableWithoutDrawsContent| should cause a layer to become hit
  1220. // testable even though it does not draw content.
  1221. root_layer->SetHitTestable(true);
  1222. CommitAndPushProperties(root_layer.get(), impl_layer.get());
  1223. EXPECT_FALSE(impl_layer->draws_content());
  1224. EXPECT_TRUE(impl_layer->HitTestable());
  1225. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1226. }
  1227. void ReceiveCopyOutputResult(int* result_count,
  1228. std::unique_ptr<viz::CopyOutputResult> result) {
  1229. ++(*result_count);
  1230. }
  1231. void ReceiveCopyOutputResultAtomic(
  1232. std::atomic<int>* result_count,
  1233. std::unique_ptr<viz::CopyOutputResult> result) {
  1234. ++(*result_count);
  1235. }
  1236. TEST_F(LayerTest, DedupesCopyOutputRequestsBySource) {
  1237. scoped_refptr<Layer> layer = Layer::Create();
  1238. std::atomic<int> result_count{0};
  1239. // Create identical requests without the source being set, and expect the
  1240. // layer does not abort either one.
  1241. std::unique_ptr<viz::CopyOutputRequest> request =
  1242. std::make_unique<viz::CopyOutputRequest>(
  1243. viz::CopyOutputRequest::ResultFormat::RGBA,
  1244. viz::CopyOutputRequest::ResultDestination::kSystemMemory,
  1245. base::BindOnce(&ReceiveCopyOutputResultAtomic,
  1246. base::Unretained(&result_count)));
  1247. layer->RequestCopyOfOutput(std::move(request));
  1248. // Because RequestCopyOfOutput could run as a PostTask to return results
  1249. // RunUntilIdle() to ensure that the result is not returned yet.
  1250. CCTestSuite::RunUntilIdle();
  1251. EXPECT_EQ(0, result_count.load());
  1252. request = std::make_unique<viz::CopyOutputRequest>(
  1253. viz::CopyOutputRequest::ResultFormat::RGBA,
  1254. viz::CopyOutputRequest::ResultDestination::kSystemMemory,
  1255. base::BindOnce(&ReceiveCopyOutputResultAtomic,
  1256. base::Unretained(&result_count)));
  1257. layer->RequestCopyOfOutput(std::move(request));
  1258. // Because RequestCopyOfOutput could run as a PostTask to return results
  1259. // RunUntilIdle() to ensure that the result is not returned yet.
  1260. CCTestSuite::RunUntilIdle();
  1261. EXPECT_EQ(0, result_count.load());
  1262. // When the layer is destroyed, expect both requests to be aborted.
  1263. layer = nullptr;
  1264. // Wait for any posted tasks to run so the results will be returned.
  1265. CCTestSuite::RunUntilIdle();
  1266. EXPECT_EQ(2, result_count.load());
  1267. layer = Layer::Create();
  1268. // Create identical requests, but this time the source is being set. Expect
  1269. // the first request using |kArbitrarySourceId1| aborts immediately when
  1270. // the second request using |kArbitrarySourceId1| is made.
  1271. int did_receive_first_result_from_this_source = 0;
  1272. request = std::make_unique<viz::CopyOutputRequest>(
  1273. viz::CopyOutputRequest::ResultFormat::RGBA,
  1274. viz::CopyOutputRequest::ResultDestination::kSystemMemory,
  1275. base::BindOnce(&ReceiveCopyOutputResult,
  1276. &did_receive_first_result_from_this_source));
  1277. request->set_source(kArbitrarySourceId1);
  1278. layer->RequestCopyOfOutput(std::move(request));
  1279. // Because RequestCopyOfOutput could run as a PostTask to return results
  1280. // RunUntilIdle() to ensure that the result is not returned yet.
  1281. CCTestSuite::RunUntilIdle();
  1282. EXPECT_EQ(0, did_receive_first_result_from_this_source);
  1283. // Make a request from a different source.
  1284. int did_receive_result_from_different_source = 0;
  1285. request = std::make_unique<viz::CopyOutputRequest>(
  1286. viz::CopyOutputRequest::ResultFormat::RGBA,
  1287. viz::CopyOutputRequest::ResultDestination::kSystemMemory,
  1288. base::BindOnce(&ReceiveCopyOutputResult,
  1289. &did_receive_result_from_different_source));
  1290. request->set_source(kArbitrarySourceId2);
  1291. layer->RequestCopyOfOutput(std::move(request));
  1292. // Because RequestCopyOfOutput could run as a PostTask to return results
  1293. // RunUntilIdle() to ensure that the result is not returned yet.
  1294. CCTestSuite::RunUntilIdle();
  1295. EXPECT_EQ(0, did_receive_result_from_different_source);
  1296. // Make a request without specifying the source.
  1297. int did_receive_result_from_anonymous_source = 0;
  1298. request = std::make_unique<viz::CopyOutputRequest>(
  1299. viz::CopyOutputRequest::ResultFormat::RGBA,
  1300. viz::CopyOutputRequest::ResultDestination::kSystemMemory,
  1301. base::BindOnce(&ReceiveCopyOutputResult,
  1302. &did_receive_result_from_anonymous_source));
  1303. layer->RequestCopyOfOutput(std::move(request));
  1304. // Because RequestCopyOfOutput could run as a PostTask to return results
  1305. // RunUntilIdle() to ensure that the result is not returned yet.
  1306. CCTestSuite::RunUntilIdle();
  1307. EXPECT_EQ(0, did_receive_result_from_anonymous_source);
  1308. // Make the second request from |kArbitrarySourceId1|.
  1309. int did_receive_second_result_from_this_source = 0;
  1310. request = std::make_unique<viz::CopyOutputRequest>(
  1311. viz::CopyOutputRequest::ResultFormat::RGBA,
  1312. viz::CopyOutputRequest::ResultDestination::kSystemMemory,
  1313. base::BindOnce(&ReceiveCopyOutputResult,
  1314. &did_receive_second_result_from_this_source));
  1315. request->set_source(kArbitrarySourceId1);
  1316. layer->RequestCopyOfOutput(
  1317. std::move(request)); // First request to be aborted.
  1318. // Wait for any posted tasks to run so the results will be returned.
  1319. CCTestSuite::RunUntilIdle();
  1320. EXPECT_EQ(1, did_receive_first_result_from_this_source);
  1321. EXPECT_EQ(0, did_receive_result_from_different_source);
  1322. EXPECT_EQ(0, did_receive_result_from_anonymous_source);
  1323. EXPECT_EQ(0, did_receive_second_result_from_this_source);
  1324. // When the layer is destroyed, the other three requests should be aborted.
  1325. layer = nullptr;
  1326. // Wait for any posted tasks to run so the results will be returned.
  1327. CCTestSuite::RunUntilIdle();
  1328. EXPECT_EQ(1, did_receive_first_result_from_this_source);
  1329. EXPECT_EQ(1, did_receive_result_from_different_source);
  1330. EXPECT_EQ(1, did_receive_result_from_anonymous_source);
  1331. EXPECT_EQ(1, did_receive_second_result_from_this_source);
  1332. }
  1333. TEST_F(LayerTest, AnimationSchedulesLayerUpdate) {
  1334. // TODO(weiliangc): This is really a LayerTreeHost unittest by this point,
  1335. // though currently there is no good place for this unittest to go. Move to
  1336. // LayerTreeHost unittest when there is a good setup.
  1337. scoped_refptr<Layer> layer = Layer::Create();
  1338. layer->SetElementId(ElementId(2));
  1339. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(layer));
  1340. auto element_id = layer->element_id();
  1341. EXPECT_CALL(*layer_tree_host_, SetNeedsUpdateLayers());
  1342. layer_tree_host_->SetElementOpacityMutated(element_id,
  1343. ElementListType::ACTIVE, 0.5f);
  1344. Mock::VerifyAndClearExpectations(layer_tree_host_.get());
  1345. EXPECT_CALL(*layer_tree_host_, SetNeedsUpdateLayers());
  1346. gfx::Transform transform;
  1347. transform.Rotate(45.0);
  1348. layer_tree_host_->SetElementTransformMutated(
  1349. element_id, ElementListType::ACTIVE, transform);
  1350. Mock::VerifyAndClearExpectations(layer_tree_host_.get());
  1351. // Scroll offset animation should not schedule a layer update since it is
  1352. // handled similarly to normal compositor scroll updates.
  1353. EXPECT_CALL(*layer_tree_host_, SetNeedsUpdateLayers()).Times(0);
  1354. layer_tree_host_->SetElementScrollOffsetMutated(
  1355. element_id, ElementListType::ACTIVE, gfx::PointF(10, 10));
  1356. Mock::VerifyAndClearExpectations(layer_tree_host_.get());
  1357. }
  1358. TEST_F(LayerTest, ElementIdIsPushed) {
  1359. scoped_refptr<Layer> test_layer = Layer::Create();
  1360. std::unique_ptr<LayerImpl> impl_layer =
  1361. LayerImpl::Create(host_impl_.active_tree(), 1);
  1362. EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
  1363. layer_tree_host_->SetRootLayer(test_layer));
  1364. test_layer->SetElementId(ElementId(2));
  1365. EXPECT_FALSE(impl_layer->element_id());
  1366. CommitAndPushProperties(test_layer.get(), impl_layer.get());
  1367. EXPECT_EQ(ElementId(2), impl_layer->element_id());
  1368. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1369. }
  1370. TEST_F(LayerTest, SetLayerTreeHostNotUsingLayerListsManagesElementId) {
  1371. scoped_refptr<Layer> test_layer = Layer::Create();
  1372. ElementId element_id = ElementId(2);
  1373. test_layer->SetElementId(element_id);
  1374. // Expect additional calls due to has-animation check and initialization
  1375. // of keyframes.
  1376. scoped_refptr<AnimationTimeline> timeline =
  1377. AnimationTimeline::Create(AnimationIdProvider::NextTimelineId());
  1378. animation_host_->AddAnimationTimeline(timeline);
  1379. AddOpacityTransitionToElementWithAnimation(element_id, timeline, 10.0, 1.f,
  1380. 0.f, false);
  1381. EXPECT_TRUE(animation_host_->IsElementAnimating(element_id));
  1382. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1383. EXPECT_EQ(nullptr, layer_tree_host_->LayerByElementId(element_id));
  1384. test_layer->SetLayerTreeHost(layer_tree_host_.get());
  1385. // Layer should now be registered by element id.
  1386. EXPECT_EQ(test_layer, layer_tree_host_->LayerByElementId(element_id));
  1387. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1388. // We're expected to remove the animations before calling
  1389. // SetLayerTreeHost(nullptr).
  1390. animation_host_->RemoveAnimationTimeline(timeline);
  1391. test_layer->SetLayerTreeHost(nullptr);
  1392. // Layer should have been un-registered.
  1393. EXPECT_EQ(nullptr, layer_tree_host_->LayerByElementId(element_id));
  1394. }
  1395. // Triggering a commit to push animation counts and raf presence to the
  1396. // compositor is expensive and updated counts can wait until the next
  1397. // commit to be pushed. See https://crbug.com/1083244.
  1398. TEST_F(LayerTest, PushAnimationCountsLazily) {
  1399. animation_host_->SetAnimationCounts(0);
  1400. animation_host_->SetCurrentFrameHadRaf(true);
  1401. animation_host_->SetNextFrameHasPendingRaf(true);
  1402. animation_host_->SetHasSmilAnimation(true);
  1403. EXPECT_FALSE(host_impl_.animation_host()->CurrentFrameHadRAF());
  1404. EXPECT_FALSE(host_impl_.animation_host()->HasSmilAnimation());
  1405. EXPECT_FALSE(animation_host_->needs_push_properties());
  1406. animation_host_->PushPropertiesTo(host_impl_.animation_host(),
  1407. *layer_tree_host_->property_trees());
  1408. EXPECT_TRUE(host_impl_.animation_host()->CurrentFrameHadRAF());
  1409. EXPECT_TRUE(host_impl_.animation_host()->HasSmilAnimation());
  1410. EXPECT_FALSE(layer_tree_host_->GetNeedsCommitAndReset());
  1411. }
  1412. TEST_F(LayerTest, SetElementIdNotUsingLayerLists) {
  1413. scoped_refptr<Layer> test_layer = Layer::Create();
  1414. test_layer->SetLayerTreeHost(layer_tree_host_.get());
  1415. ElementId element_id = ElementId(2);
  1416. EXPECT_EQ(nullptr, layer_tree_host_->LayerByElementId(element_id));
  1417. test_layer->SetElementId(element_id);
  1418. // Layer should now be registered by element id.
  1419. EXPECT_EQ(test_layer, layer_tree_host_->LayerByElementId(element_id));
  1420. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1421. ElementId other_element_id = ElementId(3);
  1422. test_layer->SetElementId(other_element_id);
  1423. // The layer should have been unregistered from the original element
  1424. // id and registered with the new one.
  1425. EXPECT_EQ(nullptr, layer_tree_host_->LayerByElementId(element_id));
  1426. EXPECT_EQ(test_layer, layer_tree_host_->LayerByElementId(other_element_id));
  1427. test_layer->SetLayerTreeHost(nullptr);
  1428. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1429. }
  1430. // Verifies that when mirror count of the layer is incremented or decremented,
  1431. // SetPropertyTreesNeedRebuild() and SetNeedsPushProperties() are called
  1432. // appropriately.
  1433. TEST_F(LayerTest, UpdateMirrorCount) {
  1434. scoped_refptr<Layer> test_layer = Layer::Create();
  1435. test_layer->SetLayerTreeHost(layer_tree_host_.get());
  1436. // This is normally done by TreeSynchronizer::PushLayerProperties().
  1437. layer_tree_host_->GetPendingCommitState()
  1438. ->layers_that_should_push_properties.clear();
  1439. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  1440. EXPECT_EQ(0, test_layer->mirror_count());
  1441. EXPECT_FALSE(layer_tree_host_->property_trees()->needs_rebuild());
  1442. EXPECT_EQ(0u, layer_tree_host_->GetPendingCommitState()
  1443. ->layers_that_should_push_properties.size());
  1444. // Incrementing mirror count from zero should trigger property trees
  1445. // rebuild.
  1446. test_layer->IncrementMirrorCount();
  1447. EXPECT_EQ(1, test_layer->mirror_count());
  1448. EXPECT_TRUE(layer_tree_host_->property_trees()->needs_rebuild());
  1449. EXPECT_TRUE(base::Contains(layer_tree_host_->GetPendingCommitState()
  1450. ->layers_that_should_push_properties,
  1451. test_layer.get()));
  1452. layer_tree_host_->GetPendingCommitState()
  1453. ->layers_that_should_push_properties.clear();
  1454. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  1455. // Incrementing mirror count from non-zero should not trigger property trees
  1456. // rebuild.
  1457. test_layer->IncrementMirrorCount();
  1458. EXPECT_EQ(2, test_layer->mirror_count());
  1459. EXPECT_FALSE(layer_tree_host_->property_trees()->needs_rebuild());
  1460. EXPECT_TRUE(base::Contains(layer_tree_host_->GetPendingCommitState()
  1461. ->layers_that_should_push_properties,
  1462. test_layer.get()));
  1463. layer_tree_host_->GetPendingCommitState()
  1464. ->layers_that_should_push_properties.clear();
  1465. // Decrementing mirror count to non-zero should not trigger property trees
  1466. // rebuild.
  1467. test_layer->DecrementMirrorCount();
  1468. EXPECT_EQ(1, test_layer->mirror_count());
  1469. EXPECT_FALSE(layer_tree_host_->property_trees()->needs_rebuild());
  1470. EXPECT_TRUE(base::Contains(layer_tree_host_->GetPendingCommitState()
  1471. ->layers_that_should_push_properties,
  1472. test_layer.get()));
  1473. // Decrementing mirror count to zero should trigger property trees rebuild.
  1474. test_layer->DecrementMirrorCount();
  1475. EXPECT_EQ(0, test_layer->mirror_count());
  1476. EXPECT_TRUE(layer_tree_host_->property_trees()->needs_rebuild());
  1477. EXPECT_TRUE(base::Contains(layer_tree_host_->GetPendingCommitState()
  1478. ->layers_that_should_push_properties,
  1479. test_layer.get()));
  1480. test_layer->SetLayerTreeHost(nullptr);
  1481. }
  1482. TEST_F(LayerTest, UpdatingCaptureBounds) {
  1483. static const viz::RegionCaptureBounds kEmptyBounds;
  1484. static const viz::RegionCaptureBounds kPopulatedBounds(
  1485. base::flat_map<viz::RegionCaptureCropId, gfx::Rect>{
  1486. {viz::RegionCaptureCropId(123u, 456u), gfx::Rect(0, 0, 640, 480)}});
  1487. static const viz::RegionCaptureBounds kUpdatedBounds(
  1488. base::flat_map<viz::RegionCaptureCropId, gfx::Rect>{
  1489. {viz::RegionCaptureCropId(123u, 456u), gfx::Rect(0, 0, 1280, 720)}});
  1490. // We don't track full tree syncs in this test.
  1491. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
  1492. scoped_refptr<Layer> layer = Layer::Create();
  1493. layer_tree_host_->SetRootLayer(layer);
  1494. // Clear the updates caused by setting a new root layer.
  1495. layer->ClearSubtreePropertyChangedForTesting();
  1496. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  1497. // An empty bounds when none is currently set should not cause an update.
  1498. layer->SetCaptureBounds(kEmptyBounds);
  1499. EXPECT_FALSE(layer_tree_host_->property_trees()->needs_rebuild());
  1500. EXPECT_FALSE(layer->subtree_property_changed());
  1501. EXPECT_FALSE(layer_tree_host_->GetNeedsCommitAndReset());
  1502. // Setting to a new bounds should cause an update.
  1503. layer->SetCaptureBounds(kPopulatedBounds);
  1504. EXPECT_TRUE(layer_tree_host_->property_trees()->needs_rebuild());
  1505. EXPECT_TRUE(layer->subtree_property_changed());
  1506. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1507. // Reset properties.
  1508. layer->ClearSubtreePropertyChangedForTesting();
  1509. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  1510. // Setting to the same bounds should not, however.
  1511. layer->SetCaptureBounds(kPopulatedBounds);
  1512. EXPECT_FALSE(layer_tree_host_->property_trees()->needs_rebuild());
  1513. EXPECT_FALSE(layer->subtree_property_changed());
  1514. EXPECT_FALSE(layer_tree_host_->GetNeedsCommitAndReset());
  1515. // Switching to a differently valued bounds should cause an update.
  1516. layer->SetCaptureBounds(kUpdatedBounds);
  1517. EXPECT_TRUE(layer_tree_host_->property_trees()->needs_rebuild());
  1518. EXPECT_TRUE(layer->subtree_property_changed());
  1519. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1520. // Reset properties.
  1521. layer->ClearSubtreePropertyChangedForTesting();
  1522. layer_tree_host_->property_trees()->set_needs_rebuild(false);
  1523. // Finally, setting to empty should cause an update.
  1524. layer->SetCaptureBounds(kEmptyBounds);
  1525. EXPECT_TRUE(layer_tree_host_->property_trees()->needs_rebuild());
  1526. EXPECT_TRUE(layer->subtree_property_changed());
  1527. EXPECT_TRUE(layer_tree_host_->GetNeedsCommitAndReset());
  1528. }
  1529. TEST_F(LayerTest, UpdatingClipRect) {
  1530. const gfx::Size kRootSize(200, 200);
  1531. const gfx::Vector2dF kParentOffset(10.f, 20.f);
  1532. const gfx::Size kLayerSize(100, 100);
  1533. const gfx::Rect kClipRect(50, 25, 100, 100);
  1534. const gfx::Rect kUpdatedClipRect_1(10, 20, 150, 200);
  1535. const gfx::Rect kUpdatedClipRect_2(20, 20, 50, 100);
  1536. const gfx::Rect kUpdatedClipRect_3(50, 25, 100, 80);
  1537. const gfx::Rect kUpdatedClipRect_4(10, 10, 200, 200);
  1538. scoped_refptr<Layer> root = Layer::Create();
  1539. scoped_refptr<Layer> parent = Layer::Create();
  1540. scoped_refptr<Layer> clipped_1 = Layer::Create();
  1541. scoped_refptr<Layer> clipped_2 = Layer::Create();
  1542. scoped_refptr<Layer> clipped_3 = Layer::Create();
  1543. scoped_refptr<Layer> clipped_4 = Layer::Create();
  1544. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
  1545. layer_tree_host_->SetRootLayer(root);
  1546. root->AddChild(parent);
  1547. parent->AddChild(clipped_1);
  1548. parent->AddChild(clipped_2);
  1549. parent->AddChild(clipped_3);
  1550. parent->AddChild(clipped_4);
  1551. root->SetBounds(kRootSize);
  1552. parent->SetBounds(kRootSize);
  1553. clipped_1->SetBounds(kLayerSize);
  1554. clipped_2->SetBounds(kLayerSize);
  1555. clipped_3->SetBounds(kLayerSize);
  1556. clipped_4->SetBounds(kLayerSize);
  1557. // This should introduce the |offset_from_transform_parent| component.
  1558. parent->SetPosition(gfx::PointF() + kParentOffset);
  1559. clipped_1->SetClipRect(kClipRect);
  1560. clipped_2->SetClipRect(kClipRect);
  1561. clipped_3->SetClipRect(kClipRect);
  1562. clipped_4->SetClipRect(kClipRect);
  1563. EXPECT_EQ(clipped_1->clip_rect(), kClipRect);
  1564. EXPECT_EQ(clipped_2->clip_rect(), kClipRect);
  1565. EXPECT_EQ(clipped_3->clip_rect(), kClipRect);
  1566. EXPECT_EQ(clipped_4->clip_rect(), kClipRect);
  1567. root->layer_tree_host()->BuildPropertyTreesForTesting();
  1568. const ClipNode* node_1 = layer_tree_host_->property_trees()->clip_tree().Node(
  1569. clipped_1->clip_tree_index());
  1570. const ClipNode* node_2 = layer_tree_host_->property_trees()->clip_tree().Node(
  1571. clipped_2->clip_tree_index());
  1572. const ClipNode* node_3 = layer_tree_host_->property_trees()->clip_tree().Node(
  1573. clipped_3->clip_tree_index());
  1574. const ClipNode* node_4 = layer_tree_host_->property_trees()->clip_tree().Node(
  1575. clipped_4->clip_tree_index());
  1576. EXPECT_EQ(gfx::RectF(kClipRect) + kParentOffset, node_1->clip);
  1577. EXPECT_EQ(gfx::RectF(kClipRect) + kParentOffset, node_2->clip);
  1578. EXPECT_EQ(gfx::RectF(kClipRect) + kParentOffset, node_3->clip);
  1579. EXPECT_EQ(gfx::RectF(kClipRect) + kParentOffset, node_4->clip);
  1580. // The following layer properties should result in the layer being clipped
  1581. // to its bounds along with being clipped by the clip rect. Check if the
  1582. // final rect on the clip node is set correctly.
  1583. // Setting clip to layer bounds.
  1584. clipped_1->SetMasksToBounds(true);
  1585. // Setting a mask.
  1586. FakeContentLayerClient client;
  1587. scoped_refptr<PictureLayer> mask = PictureLayer::Create(&client);
  1588. clipped_2->SetMaskLayer(mask);
  1589. // Setting a filter that moves pixels.
  1590. FilterOperations move_pixel_filters;
  1591. move_pixel_filters.Append(
  1592. FilterOperation::CreateBlurFilter(2, SkTileMode::kClamp));
  1593. ASSERT_TRUE(move_pixel_filters.HasFilterThatMovesPixels());
  1594. clipped_3->SetFilters(move_pixel_filters);
  1595. clipped_1->SetClipRect(kUpdatedClipRect_1);
  1596. clipped_2->SetClipRect(kUpdatedClipRect_2);
  1597. clipped_3->SetClipRect(kUpdatedClipRect_3);
  1598. clipped_4->SetClipRect(kUpdatedClipRect_4);
  1599. node_1 = layer_tree_host_->property_trees()->clip_tree().Node(
  1600. clipped_1->clip_tree_index());
  1601. node_2 = layer_tree_host_->property_trees()->clip_tree().Node(
  1602. clipped_2->clip_tree_index());
  1603. node_3 = layer_tree_host_->property_trees()->clip_tree().Node(
  1604. clipped_3->clip_tree_index());
  1605. node_4 = layer_tree_host_->property_trees()->clip_tree().Node(
  1606. clipped_4->clip_tree_index());
  1607. EXPECT_EQ(node_1->clip,
  1608. gfx::IntersectRects(gfx::RectF(kUpdatedClipRect_1),
  1609. gfx::RectF(gfx::SizeF(kLayerSize))) +
  1610. kParentOffset);
  1611. EXPECT_EQ(node_2->clip,
  1612. gfx::IntersectRects(gfx::RectF(kUpdatedClipRect_2),
  1613. gfx::RectF(gfx::SizeF(kLayerSize))) +
  1614. kParentOffset);
  1615. EXPECT_EQ(node_3->clip,
  1616. gfx::IntersectRects(gfx::RectF(kUpdatedClipRect_3),
  1617. gfx::RectF(gfx::SizeF(kLayerSize))) +
  1618. kParentOffset);
  1619. EXPECT_EQ(node_4->clip, gfx::RectF(kUpdatedClipRect_4) + kParentOffset);
  1620. }
  1621. TEST_F(LayerTest, UpdatingRoundedCorners) {
  1622. const gfx::Size kRootSize(200, 200);
  1623. const gfx::Size kLayerSize(100, 100);
  1624. const gfx::Rect kClipRect(50, 25, 100, 100);
  1625. const gfx::Rect kUpdatedClipRect(10, 20, 30, 40);
  1626. const gfx::RoundedCornersF kRoundedCorners(5);
  1627. const gfx::RoundedCornersF kUpdatedRoundedCorners(10);
  1628. scoped_refptr<Layer> root = Layer::Create();
  1629. scoped_refptr<Layer> layer_1 = Layer::Create();
  1630. scoped_refptr<Layer> layer_2 = Layer::Create();
  1631. scoped_refptr<Layer> layer_3 = Layer::Create();
  1632. scoped_refptr<Layer> layer_4 = Layer::Create();
  1633. scoped_refptr<Layer> layer_5 = Layer::Create();
  1634. EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
  1635. layer_tree_host_->SetRootLayer(root);
  1636. root->AddChild(layer_1);
  1637. root->AddChild(layer_2);
  1638. root->AddChild(layer_3);
  1639. root->AddChild(layer_4);
  1640. root->AddChild(layer_5);
  1641. root->SetBounds(kRootSize);
  1642. layer_1->SetBounds(kLayerSize);
  1643. layer_2->SetBounds(kLayerSize);
  1644. layer_3->SetBounds(kLayerSize);
  1645. layer_4->SetBounds(kLayerSize);
  1646. layer_5->SetBounds(kLayerSize);
  1647. layer_1->SetClipRect(kClipRect);
  1648. layer_2->SetClipRect(kClipRect);
  1649. layer_3->SetClipRect(kClipRect);
  1650. layer_4->SetClipRect(kClipRect);
  1651. layer_1->SetRoundedCorner(kRoundedCorners);
  1652. layer_2->SetRoundedCorner(kRoundedCorners);
  1653. layer_3->SetRoundedCorner(kRoundedCorners);
  1654. layer_4->SetRoundedCorner(kRoundedCorners);
  1655. layer_5->SetRoundedCorner(kRoundedCorners);
  1656. EXPECT_EQ(layer_1->corner_radii(), kRoundedCorners);
  1657. EXPECT_EQ(layer_2->corner_radii(), kRoundedCorners);
  1658. EXPECT_EQ(layer_3->corner_radii(), kRoundedCorners);
  1659. EXPECT_EQ(layer_4->corner_radii(), kRoundedCorners);
  1660. EXPECT_EQ(layer_5->corner_radii(), kRoundedCorners);
  1661. root->layer_tree_host()->BuildPropertyTreesForTesting();
  1662. const EffectNode* node_1 =
  1663. layer_tree_host_->property_trees()->effect_tree().Node(
  1664. layer_1->effect_tree_index());
  1665. const EffectNode* node_2 =
  1666. layer_tree_host_->property_trees()->effect_tree().Node(
  1667. layer_2->effect_tree_index());
  1668. const EffectNode* node_3 =
  1669. layer_tree_host_->property_trees()->effect_tree().Node(
  1670. layer_3->effect_tree_index());
  1671. const EffectNode* node_4 =
  1672. layer_tree_host_->property_trees()->effect_tree().Node(
  1673. layer_4->effect_tree_index());
  1674. const EffectNode* node_5 =
  1675. layer_tree_host_->property_trees()->effect_tree().Node(
  1676. layer_5->effect_tree_index());
  1677. EXPECT_EQ(gfx::RRectF(gfx::RectF(kClipRect), kRoundedCorners),
  1678. node_1->mask_filter_info.rounded_corner_bounds());
  1679. EXPECT_EQ(gfx::RRectF(gfx::RectF(kClipRect), kRoundedCorners),
  1680. node_2->mask_filter_info.rounded_corner_bounds());
  1681. EXPECT_EQ(gfx::RRectF(gfx::RectF(kClipRect), kRoundedCorners),
  1682. node_3->mask_filter_info.rounded_corner_bounds());
  1683. EXPECT_EQ(gfx::RRectF(gfx::RectF(kClipRect), kRoundedCorners),
  1684. node_4->mask_filter_info.rounded_corner_bounds());
  1685. EXPECT_EQ(gfx::RRectF(gfx::RectF(gfx::Rect(kLayerSize)), kRoundedCorners),
  1686. node_5->mask_filter_info.rounded_corner_bounds());
  1687. // Setting clip to layer bounds.
  1688. layer_1->SetMasksToBounds(true);
  1689. // Setting a mask.
  1690. FakeContentLayerClient client;
  1691. scoped_refptr<PictureLayer> mask = PictureLayer::Create(&client);
  1692. layer_2->SetMaskLayer(mask);
  1693. layer_1->SetRoundedCorner(kUpdatedRoundedCorners);
  1694. layer_2->SetRoundedCorner(kUpdatedRoundedCorners);
  1695. layer_3->SetRoundedCorner(kUpdatedRoundedCorners);
  1696. // Updates the clip rect instead of rounded corners.
  1697. layer_4->SetClipRect(kUpdatedClipRect);
  1698. layer_5->SetRoundedCorner(kUpdatedRoundedCorners);
  1699. node_1 = layer_tree_host_->property_trees()->effect_tree().Node(
  1700. layer_1->effect_tree_index());
  1701. node_2 = layer_tree_host_->property_trees()->effect_tree().Node(
  1702. layer_2->effect_tree_index());
  1703. node_3 = layer_tree_host_->property_trees()->effect_tree().Node(
  1704. layer_3->effect_tree_index());
  1705. node_4 = layer_tree_host_->property_trees()->effect_tree().Node(
  1706. layer_4->effect_tree_index());
  1707. node_5 = layer_tree_host_->property_trees()->effect_tree().Node(
  1708. layer_5->effect_tree_index());
  1709. EXPECT_EQ(gfx::RRectF(gfx::RectF(gfx::IntersectRects(gfx::Rect(kLayerSize),
  1710. kClipRect)),
  1711. kUpdatedRoundedCorners),
  1712. node_1->mask_filter_info.rounded_corner_bounds());
  1713. EXPECT_EQ(gfx::RRectF(gfx::RectF(gfx::IntersectRects(gfx::Rect(kLayerSize),
  1714. kClipRect)),
  1715. kUpdatedRoundedCorners),
  1716. node_2->mask_filter_info.rounded_corner_bounds());
  1717. EXPECT_EQ(gfx::RRectF(gfx::RectF(kClipRect), kUpdatedRoundedCorners),
  1718. node_3->mask_filter_info.rounded_corner_bounds());
  1719. EXPECT_EQ(gfx::RRectF(gfx::RectF(kUpdatedClipRect), kRoundedCorners),
  1720. node_4->mask_filter_info.rounded_corner_bounds());
  1721. EXPECT_EQ(
  1722. gfx::RRectF(gfx::RectF(gfx::Rect(kLayerSize)), kUpdatedRoundedCorners),
  1723. node_5->mask_filter_info.rounded_corner_bounds());
  1724. }
  1725. } // namespace
  1726. } // namespace cc