OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include <stddef.h> |
| 6 |
| 7 #include "cc/scheduler/begin_frame_source.h" |
| 8 #include "cc/surfaces/surface_factory_client.h" |
| 9 #include "cc/surfaces/surface_manager.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace cc { |
| 13 |
| 14 class FakeSurfaceFactoryClient : public SurfaceFactoryClient { |
| 15 public: |
| 16 explicit FakeSurfaceFactoryClient(int id_namespace) |
| 17 : source_(nullptr), manager_(nullptr), id_namespace_(id_namespace) {} |
| 18 FakeSurfaceFactoryClient(int id_namespace, SurfaceManager* manager) |
| 19 : source_(nullptr), manager_(nullptr), id_namespace_(id_namespace) { |
| 20 DCHECK(manager); |
| 21 Register(manager); |
| 22 } |
| 23 |
| 24 ~FakeSurfaceFactoryClient() override { |
| 25 if (manager_) { |
| 26 Unregister(); |
| 27 } |
| 28 EXPECT_EQ(source_, nullptr); |
| 29 } |
| 30 |
| 31 BeginFrameSource* source() { return source_; } |
| 32 uint32_t id_namespace() { return id_namespace_; } |
| 33 |
| 34 void Register(SurfaceManager* manager) { |
| 35 EXPECT_EQ(manager_, nullptr); |
| 36 manager_ = manager; |
| 37 manager_->RegisterSurfaceFactoryClient(id_namespace_, this); |
| 38 } |
| 39 |
| 40 void Unregister() { |
| 41 EXPECT_NE(manager_, nullptr); |
| 42 manager_->RegisterSurfaceFactoryClient(id_namespace_, nullptr); |
| 43 manager_ = nullptr; |
| 44 } |
| 45 |
| 46 // SurfaceFactoryClient implementation. |
| 47 void ReturnResources(const ReturnedResourceArray& resources) override{}; |
| 48 void SetBeginFrameSource(BeginFrameSource* begin_frame_source) override { |
| 49 DCHECK(!source_ || !begin_frame_source); |
| 50 source_ = begin_frame_source; |
| 51 }; |
| 52 |
| 53 private: |
| 54 BeginFrameSource* source_; |
| 55 SurfaceManager* manager_; |
| 56 uint32_t id_namespace_; |
| 57 }; |
| 58 |
| 59 class EmptyBeginFrameSource : public BeginFrameSource { |
| 60 public: |
| 61 void DidFinishFrame(size_t remaining_frames) override{}; |
| 62 void AddObserver(BeginFrameObserver* obs) override{}; |
| 63 void RemoveObserver(BeginFrameObserver* obs) override{}; |
| 64 void SetClientReady() override {} |
| 65 void AsValueInto(base::trace_event::TracedValue* dict) const override{}; |
| 66 }; |
| 67 |
| 68 TEST(SurfaceManagerTest, SingleClients) { |
| 69 SurfaceManager manager; |
| 70 FakeSurfaceFactoryClient client(1); |
| 71 FakeSurfaceFactoryClient other_client(2); |
| 72 EmptyBeginFrameSource source; |
| 73 |
| 74 EXPECT_EQ(client.source(), nullptr); |
| 75 EXPECT_EQ(other_client.source(), nullptr); |
| 76 client.Register(&manager); |
| 77 other_client.Register(&manager); |
| 78 EXPECT_EQ(client.source(), nullptr); |
| 79 EXPECT_EQ(other_client.source(), nullptr); |
| 80 |
| 81 // Test setting unsetting BFS |
| 82 manager.RegisterBeginFrameSource(&source, client.id_namespace()); |
| 83 EXPECT_EQ(client.source(), &source); |
| 84 EXPECT_EQ(other_client.source(), nullptr); |
| 85 manager.UnregisterBeginFrameSource(&source); |
| 86 EXPECT_EQ(client.source(), nullptr); |
| 87 EXPECT_EQ(other_client.source(), nullptr); |
| 88 |
| 89 // Set BFS for other namespace |
| 90 manager.RegisterBeginFrameSource(&source, other_client.id_namespace()); |
| 91 EXPECT_EQ(other_client.source(), &source); |
| 92 EXPECT_EQ(client.source(), nullptr); |
| 93 manager.UnregisterBeginFrameSource(&source); |
| 94 EXPECT_EQ(client.source(), nullptr); |
| 95 EXPECT_EQ(other_client.source(), nullptr); |
| 96 |
| 97 // Re-set BFS for original |
| 98 manager.RegisterBeginFrameSource(&source, client.id_namespace()); |
| 99 EXPECT_EQ(client.source(), &source); |
| 100 manager.UnregisterBeginFrameSource(&source); |
| 101 EXPECT_EQ(client.source(), nullptr); |
| 102 } |
| 103 |
| 104 TEST(SurfaceManagerTest, MultipleDisplays) { |
| 105 SurfaceManager manager; |
| 106 EmptyBeginFrameSource root1_source; |
| 107 EmptyBeginFrameSource root2_source; |
| 108 |
| 109 // root1 -> A -> B |
| 110 // root2 -> C |
| 111 FakeSurfaceFactoryClient root1(1, &manager); |
| 112 FakeSurfaceFactoryClient root2(2, &manager); |
| 113 FakeSurfaceFactoryClient client_a(3, &manager); |
| 114 FakeSurfaceFactoryClient client_b(4, &manager); |
| 115 FakeSurfaceFactoryClient client_c(5, &manager); |
| 116 |
| 117 manager.RegisterBeginFrameSource(&root1_source, root1.id_namespace()); |
| 118 manager.RegisterBeginFrameSource(&root2_source, root2.id_namespace()); |
| 119 EXPECT_EQ(root1.source(), &root1_source); |
| 120 EXPECT_EQ(root2.source(), &root2_source); |
| 121 |
| 122 // Set up initial hierarchy. |
| 123 manager.RegisterSurfaceNamespaceHierarchy(root1.id_namespace(), |
| 124 client_a.id_namespace()); |
| 125 EXPECT_EQ(client_a.source(), root1.source()); |
| 126 manager.RegisterSurfaceNamespaceHierarchy(client_a.id_namespace(), |
| 127 client_b.id_namespace()); |
| 128 EXPECT_EQ(client_b.source(), root1.source()); |
| 129 manager.RegisterSurfaceNamespaceHierarchy(root2.id_namespace(), |
| 130 client_c.id_namespace()); |
| 131 EXPECT_EQ(client_c.source(), root2.source()); |
| 132 |
| 133 // Attach A into root2's subtree, like a window moving across displays. |
| 134 // root1 -> A -> B |
| 135 // root2 -> C -> A -> B |
| 136 manager.RegisterSurfaceNamespaceHierarchy(client_c.id_namespace(), |
| 137 client_a.id_namespace()); |
| 138 // With the heuristic of just keeping existing BFS in the face of multiple, |
| 139 // no client sources should change. |
| 140 EXPECT_EQ(client_a.source(), root1.source()); |
| 141 EXPECT_EQ(client_b.source(), root1.source()); |
| 142 EXPECT_EQ(client_c.source(), root2.source()); |
| 143 |
| 144 // Detach A from root1. A and B should now be updated to root2. |
| 145 manager.UnregisterSurfaceNamespaceHierarchy(root1.id_namespace(), |
| 146 client_a.id_namespace()); |
| 147 EXPECT_EQ(client_a.source(), root2.source()); |
| 148 EXPECT_EQ(client_b.source(), root2.source()); |
| 149 EXPECT_EQ(client_c.source(), root2.source()); |
| 150 |
| 151 // Detach root1 from BFS. root1 should now have no source. |
| 152 manager.UnregisterBeginFrameSource(&root1_source); |
| 153 EXPECT_EQ(root1.source(), nullptr); |
| 154 EXPECT_NE(root2.source(), nullptr); |
| 155 |
| 156 // Detatch root2 from BFS. |
| 157 manager.UnregisterBeginFrameSource(&root2_source); |
| 158 EXPECT_EQ(client_a.source(), nullptr); |
| 159 EXPECT_EQ(client_b.source(), nullptr); |
| 160 EXPECT_EQ(client_c.source(), nullptr); |
| 161 EXPECT_EQ(root2.source(), nullptr); |
| 162 |
| 163 // Cleanup hierarchy. |
| 164 manager.UnregisterSurfaceNamespaceHierarchy(root2.id_namespace(), |
| 165 client_c.id_namespace()); |
| 166 manager.UnregisterSurfaceNamespaceHierarchy(client_c.id_namespace(), |
| 167 client_a.id_namespace()); |
| 168 manager.UnregisterSurfaceNamespaceHierarchy(client_a.id_namespace(), |
| 169 client_b.id_namespace()); |
| 170 } |
| 171 |
| 172 } // namespace cc |
OLD | NEW |