Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <unordered_map> | 7 #include <unordered_map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 factory_ptr = base::MakeUnique<SurfaceFactory>(frame_sink_id, | 64 factory_ptr = base::MakeUnique<SurfaceFactory>(frame_sink_id, |
| 65 manager_.get(), &client_); | 65 manager_.get(), &client_); |
| 66 return *factory_ptr; | 66 return *factory_ptr; |
| 67 } | 67 } |
| 68 | 68 |
| 69 void RemoveSurfaceReference(const SurfaceId& parent_id, | 69 void RemoveSurfaceReference(const SurfaceId& parent_id, |
| 70 const SurfaceId& child_id) { | 70 const SurfaceId& child_id) { |
| 71 manager_->RemoveSurfaceReferences({SurfaceReference(parent_id, child_id)}); | 71 manager_->RemoveSurfaceReferences({SurfaceReference(parent_id, child_id)}); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void AddSurfaceReference(const SurfaceId& parent_id, | |
| 75 const SurfaceId& child_id) { | |
| 76 manager_->AddSurfaceReferences({SurfaceReference(parent_id, child_id)}); | |
| 77 } | |
| 78 | |
| 74 // Returns all the references where |surface_id| is the parent. | 79 // Returns all the references where |surface_id| is the parent. |
| 75 const SurfaceManager::SurfaceIdSet& GetReferencesFrom( | 80 const SurfaceManager::SurfaceIdSet& GetReferencesFrom( |
| 76 const SurfaceId& surface_id) { | 81 const SurfaceId& surface_id) { |
| 77 return manager().parent_to_child_refs_[surface_id]; | 82 return manager().parent_to_child_refs_[surface_id]; |
| 78 } | 83 } |
| 79 | 84 |
| 80 // Returns all the references where |surface_id| is the child. | 85 // Returns all the references where |surface_id| is the child. |
| 81 const SurfaceManager::SurfaceIdSet& GetReferencesFor( | 86 const SurfaceManager::SurfaceIdSet& GetReferencesFor( |
| 82 const SurfaceId& surface_id) { | 87 const SurfaceId& surface_id) { |
| 83 return manager().child_to_parent_refs_[surface_id]; | 88 return manager().child_to_parent_refs_[surface_id]; |
| 84 } | 89 } |
| 85 | 90 |
| 86 const SurfaceManager::SurfaceIdSet& GetReferencesFromRoot() { | |
| 87 return GetReferencesFrom(manager().GetRootSurfaceId()); | |
| 88 } | |
| 89 | |
| 90 // Returns all the temporary references for the given frame sink id. | |
| 91 std::vector<LocalSurfaceId> GetTempReferencesFor( | |
| 92 const FrameSinkId& frame_sink_id) { | |
| 93 return manager().temp_references_[frame_sink_id]; | |
| 94 } | |
| 95 | |
| 96 // Temporary references are stored as a map in SurfaceManager. This method | 91 // Temporary references are stored as a map in SurfaceManager. This method |
| 97 // converts the map to a vector. | 92 // converts the map to a vector. |
| 98 std::vector<SurfaceId> GetAllTempReferences() { | 93 std::vector<SurfaceId> GetAllTempReferences() { |
| 99 std::vector<SurfaceId> temp_references; | 94 std::vector<SurfaceId> temp_references; |
| 100 for (auto& map_entry : manager().temp_references_) { | 95 for (auto& map_entry : manager().temporary_references_) |
| 101 for (auto local_surface_id : map_entry.second) | 96 temp_references.push_back(map_entry.first); |
| 102 temp_references.push_back(SurfaceId(map_entry.first, local_surface_id)); | |
| 103 } | |
| 104 return temp_references; | 97 return temp_references; |
| 105 } | 98 } |
| 106 | 99 |
| 107 protected: | 100 protected: |
| 108 // testing::Test: | 101 // testing::Test: |
| 109 void SetUp() override { | 102 void SetUp() override { |
| 110 // Start each test with a fresh SurfaceManager instance. | 103 // Start each test with a fresh SurfaceManager instance. |
| 111 manager_ = base::MakeUnique<SurfaceManager>( | 104 manager_ = base::MakeUnique<SurfaceManager>( |
| 112 SurfaceManager::LifetimeType::REFERENCES); | 105 SurfaceManager::LifetimeType::REFERENCES); |
| 113 } | 106 } |
| 114 void TearDown() override { | 107 void TearDown() override { |
| 115 for (auto& factory : factories_) | 108 for (auto& factory : factories_) |
| 116 factory.second->EvictSurface(); | 109 factory.second->EvictSurface(); |
| 117 factories_.clear(); | 110 factories_.clear(); |
| 118 manager_.reset(); | 111 manager_.reset(); |
| 119 } | 112 } |
| 120 | 113 |
| 121 std::unordered_map<FrameSinkId, | 114 std::unordered_map<FrameSinkId, |
| 122 std::unique_ptr<SurfaceFactory>, | 115 std::unique_ptr<SurfaceFactory>, |
| 123 FrameSinkIdHash> | 116 FrameSinkIdHash> |
| 124 factories_; | 117 factories_; |
| 125 std::unique_ptr<SurfaceManager> manager_; | 118 std::unique_ptr<SurfaceManager> manager_; |
| 126 StubSurfaceFactoryClient client_; | 119 StubSurfaceFactoryClient client_; |
| 127 }; | 120 }; |
| 128 | 121 |
| 129 TEST_F(SurfaceManagerRefTest, AddReference) { | 122 TEST_F(SurfaceManagerRefTest, AddReference) { |
| 130 SurfaceId id1 = CreateSurface(kFrameSink1, 1); | 123 SurfaceId id1 = CreateSurface(kFrameSink1, 1); |
| 131 manager().AddSurfaceReference(manager().GetRootSurfaceId(), id1); | 124 AddSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 132 | 125 |
| 133 EXPECT_THAT(GetReferencesFor(id1), | 126 EXPECT_THAT(GetReferencesFor(id1), |
| 134 UnorderedElementsAre(manager().GetRootSurfaceId())); | 127 UnorderedElementsAre(manager().GetRootSurfaceId())); |
| 135 EXPECT_THAT(GetReferencesFrom(id1), IsEmpty()); | 128 EXPECT_THAT(GetReferencesFrom(id1), IsEmpty()); |
| 136 } | 129 } |
| 137 | 130 |
| 138 TEST_F(SurfaceManagerRefTest, AddRemoveReference) { | 131 TEST_F(SurfaceManagerRefTest, AddRemoveReference) { |
| 139 SurfaceId id1 = CreateSurface(kFrameSink1, 1); | 132 SurfaceId id1 = CreateSurface(kFrameSink1, 1); |
| 140 SurfaceId id2 = CreateSurface(kFrameSink2, 1); | 133 SurfaceId id2 = CreateSurface(kFrameSink2, 1); |
| 141 manager().AddSurfaceReference(manager().GetRootSurfaceId(), id1); | 134 AddSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 142 manager().AddSurfaceReference(id1, id2); | 135 AddSurfaceReference(id1, id2); |
| 143 | 136 |
| 144 EXPECT_THAT(GetReferencesFor(id1), | 137 EXPECT_THAT(GetReferencesFor(id1), |
| 145 UnorderedElementsAre(manager().GetRootSurfaceId())); | 138 UnorderedElementsAre(manager().GetRootSurfaceId())); |
| 146 EXPECT_THAT(GetReferencesFor(id2), UnorderedElementsAre(id1)); | 139 EXPECT_THAT(GetReferencesFor(id2), UnorderedElementsAre(id1)); |
| 147 EXPECT_THAT(GetReferencesFrom(id1), UnorderedElementsAre(id2)); | 140 EXPECT_THAT(GetReferencesFrom(id1), UnorderedElementsAre(id2)); |
| 148 EXPECT_THAT(GetReferencesFrom(id2), IsEmpty()); | 141 EXPECT_THAT(GetReferencesFrom(id2), IsEmpty()); |
| 149 | 142 |
| 150 RemoveSurfaceReference(id1, id2); | 143 RemoveSurfaceReference(id1, id2); |
| 151 EXPECT_THAT(GetReferencesFor(id1), SizeIs(1)); | 144 EXPECT_THAT(GetReferencesFor(id1), SizeIs(1)); |
| 152 EXPECT_THAT(GetReferencesFor(id2), IsEmpty()); | 145 EXPECT_THAT(GetReferencesFor(id2), IsEmpty()); |
| 153 EXPECT_THAT(GetReferencesFrom(id1), IsEmpty()); | 146 EXPECT_THAT(GetReferencesFrom(id1), IsEmpty()); |
| 154 EXPECT_THAT(GetReferencesFrom(id2), IsEmpty()); | 147 EXPECT_THAT(GetReferencesFrom(id2), IsEmpty()); |
| 155 } | 148 } |
| 156 | 149 |
| 157 TEST_F(SurfaceManagerRefTest, NewSurfaceFromFrameSink) { | 150 TEST_F(SurfaceManagerRefTest, NewSurfaceFromFrameSink) { |
| 158 SurfaceId id1 = CreateSurface(kFrameSink1, 1); | 151 SurfaceId id1 = CreateSurface(kFrameSink1, 1); |
| 159 SurfaceId id2 = CreateSurface(kFrameSink2, 1); | 152 SurfaceId id2 = CreateSurface(kFrameSink2, 1); |
| 160 SurfaceId id3 = CreateSurface(kFrameSink3, 1); | 153 SurfaceId id3 = CreateSurface(kFrameSink3, 1); |
| 161 | 154 |
| 162 manager().AddSurfaceReference(manager().GetRootSurfaceId(), id1); | 155 AddSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 163 manager().AddSurfaceReference(id1, id2); | 156 AddSurfaceReference(id1, id2); |
| 164 manager().AddSurfaceReference(id2, id3); | 157 AddSurfaceReference(id2, id3); |
| 165 | 158 |
| 166 // |kFramesink2| received a CompositorFrame with a new size, so it destroys | 159 // |kFramesink2| received a CompositorFrame with a new size, so it destroys |
| 167 // |id2| and creates |id2_next|. No reference have been removed yet. | 160 // |id2| and creates |id2_next|. No reference have been removed yet. |
| 168 SurfaceId id2_next = CreateSurface(kFrameSink2, 2); | 161 SurfaceId id2_next = CreateSurface(kFrameSink2, 2); |
| 169 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); | 162 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); |
| 170 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2_next)); | 163 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2_next)); |
| 171 | 164 |
| 172 // Add references to and from |id2_next|. | 165 // Add references to and from |id2_next|. |
| 173 manager().AddSurfaceReference(id1, id2_next); | 166 AddSurfaceReference(id1, id2_next); |
| 174 manager().AddSurfaceReference(id2_next, id3); | 167 AddSurfaceReference(id2_next, id3); |
| 175 EXPECT_THAT(GetReferencesFor(id2), UnorderedElementsAre(id1)); | 168 EXPECT_THAT(GetReferencesFor(id2), UnorderedElementsAre(id1)); |
| 176 EXPECT_THAT(GetReferencesFor(id2_next), UnorderedElementsAre(id1)); | 169 EXPECT_THAT(GetReferencesFor(id2_next), UnorderedElementsAre(id1)); |
| 177 EXPECT_THAT(GetReferencesFor(id3), UnorderedElementsAre(id2, id2_next)); | 170 EXPECT_THAT(GetReferencesFor(id3), UnorderedElementsAre(id2, id2_next)); |
| 178 | 171 |
| 179 RemoveSurfaceReference(id1, id2); | 172 RemoveSurfaceReference(id1, id2); |
| 180 EXPECT_THAT(GetReferencesFor(id2), IsEmpty()); | 173 EXPECT_THAT(GetReferencesFor(id2), IsEmpty()); |
| 181 EXPECT_THAT(GetReferencesFor(id2_next), UnorderedElementsAre(id1)); | 174 EXPECT_THAT(GetReferencesFor(id2_next), UnorderedElementsAre(id1)); |
| 182 EXPECT_THAT(GetReferencesFor(id3), UnorderedElementsAre(id2_next)); | 175 EXPECT_THAT(GetReferencesFor(id3), UnorderedElementsAre(id2_next)); |
| 183 | 176 |
| 184 // |id2| should be deleted during GC but other surfaces shouldn't. | 177 // |id2| should be deleted during GC but other surfaces shouldn't. |
| 185 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id2)); | 178 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id2)); |
| 186 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2_next)); | 179 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2_next)); |
| 187 EXPECT_NE(nullptr, manager().GetSurfaceForId(id3)); | 180 EXPECT_NE(nullptr, manager().GetSurfaceForId(id3)); |
| 188 } | 181 } |
| 189 | 182 |
| 190 TEST_F(SurfaceManagerRefTest, ReferenceCycleGetsDeleted) { | 183 TEST_F(SurfaceManagerRefTest, ReferenceCycleGetsDeleted) { |
| 191 SurfaceId id1 = CreateSurface(kFrameSink1, 1); | 184 SurfaceId id1 = CreateSurface(kFrameSink1, 1); |
| 192 SurfaceId id2 = CreateSurface(kFrameSink2, 1); | 185 SurfaceId id2 = CreateSurface(kFrameSink2, 1); |
| 193 SurfaceId id3 = CreateSurface(kFrameSink3, 1); | 186 SurfaceId id3 = CreateSurface(kFrameSink3, 1); |
| 194 | 187 |
| 195 manager().AddSurfaceReference(manager().GetRootSurfaceId(), id1); | 188 AddSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 196 manager().AddSurfaceReference(id1, id2); | 189 AddSurfaceReference(id1, id2); |
| 197 manager().AddSurfaceReference(id2, id3); | 190 AddSurfaceReference(id2, id3); |
| 198 | 191 |
| 199 // This reference forms a cycle between id2 and id3. | 192 // This reference forms a cycle between id2 and id3. |
| 200 manager().AddSurfaceReference(id3, id2); | 193 AddSurfaceReference(id3, id2); |
| 201 | 194 |
| 202 DestroySurface(id3); | 195 DestroySurface(id3); |
| 203 DestroySurface(id2); | 196 DestroySurface(id2); |
| 204 DestroySurface(id1); | 197 DestroySurface(id1); |
| 205 | 198 |
| 206 RemoveSurfaceReference(manager().GetRootSurfaceId(), id1); | 199 RemoveSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 207 | 200 |
| 208 // Removing the reference from the root to id1 should allow all three surfaces | 201 // Removing the reference from the root to id1 should allow all three surfaces |
| 209 // to be deleted during GC even with a cycle between 2 and 3. | 202 // to be deleted during GC even with a cycle between 2 and 3. |
| 210 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id1)); | 203 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id1)); |
| 211 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id2)); | 204 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id2)); |
| 212 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id3)); | 205 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id3)); |
| 213 } | 206 } |
| 214 | 207 |
| 215 TEST_F(SurfaceManagerRefTest, CheckGC) { | 208 TEST_F(SurfaceManagerRefTest, CheckGC) { |
| 216 SurfaceId id1 = CreateSurface(kFrameSink1, 1); | 209 SurfaceId id1 = CreateSurface(kFrameSink1, 1); |
| 217 SurfaceId id2 = CreateSurface(kFrameSink2, 1); | 210 SurfaceId id2 = CreateSurface(kFrameSink2, 1); |
| 218 | 211 |
| 219 manager().AddSurfaceReference(manager().GetRootSurfaceId(), id1); | 212 AddSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 220 manager().AddSurfaceReference(id1, id2); | 213 AddSurfaceReference(id1, id2); |
| 221 | 214 |
| 222 EXPECT_NE(nullptr, manager().GetSurfaceForId(id1)); | 215 EXPECT_NE(nullptr, manager().GetSurfaceForId(id1)); |
| 223 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); | 216 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); |
| 224 | 217 |
| 225 // Destroying the surfaces shouldn't delete them yet, since there is still an | 218 // Destroying the surfaces shouldn't delete them yet, since there is still an |
| 226 // active reference on all surfaces. | 219 // active reference on all surfaces. |
| 227 DestroySurface(id1); | 220 DestroySurface(id1); |
| 228 DestroySurface(id2); | 221 DestroySurface(id2); |
| 229 EXPECT_NE(nullptr, manager().GetSurfaceForId(id1)); | 222 EXPECT_NE(nullptr, manager().GetSurfaceForId(id1)); |
| 230 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); | 223 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); |
| 231 | 224 |
| 232 // Should delete |id2| when the only reference to it is removed. | 225 // Should delete |id2| when the only reference to it is removed. |
| 233 RemoveSurfaceReference(id1, id2); | 226 RemoveSurfaceReference(id1, id2); |
| 234 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id2)); | 227 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id2)); |
| 235 | 228 |
| 236 // Should delete |id1| when the only reference to it is removed. | 229 // Should delete |id1| when the only reference to it is removed. |
| 237 RemoveSurfaceReference(manager().GetRootSurfaceId(), id1); | 230 RemoveSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 238 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id1)); | 231 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id1)); |
| 239 } | 232 } |
| 240 | 233 |
| 241 TEST_F(SurfaceManagerRefTest, CheckGCRecusiveFull) { | 234 TEST_F(SurfaceManagerRefTest, CheckGCRecusiveFull) { |
| 242 SurfaceId id1 = CreateSurface(kFrameSink1, 1); | 235 SurfaceId id1 = CreateSurface(kFrameSink1, 1); |
| 243 SurfaceId id2 = CreateSurface(kFrameSink2, 1); | 236 SurfaceId id2 = CreateSurface(kFrameSink2, 1); |
| 244 SurfaceId id3 = CreateSurface(kFrameSink3, 1); | 237 SurfaceId id3 = CreateSurface(kFrameSink3, 1); |
| 245 | 238 |
| 246 manager().AddSurfaceReference(manager().GetRootSurfaceId(), id1); | 239 AddSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 247 manager().AddSurfaceReference(id1, id2); | 240 AddSurfaceReference(id1, id2); |
| 248 manager().AddSurfaceReference(id2, id3); | 241 AddSurfaceReference(id2, id3); |
| 249 | 242 |
| 250 DestroySurface(id3); | 243 DestroySurface(id3); |
| 251 DestroySurface(id2); | 244 DestroySurface(id2); |
| 252 DestroySurface(id1); | 245 DestroySurface(id1); |
| 253 | 246 |
| 254 // Destroying the surfaces shouldn't delete them yet, since there is still an | 247 // Destroying the surfaces shouldn't delete them yet, since there is still an |
| 255 // active reference on all surfaces. | 248 // active reference on all surfaces. |
| 256 EXPECT_NE(nullptr, manager().GetSurfaceForId(id3)); | 249 EXPECT_NE(nullptr, manager().GetSurfaceForId(id3)); |
| 257 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); | 250 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); |
| 258 EXPECT_NE(nullptr, manager().GetSurfaceForId(id1)); | 251 EXPECT_NE(nullptr, manager().GetSurfaceForId(id1)); |
| 259 | 252 |
| 260 RemoveSurfaceReference(manager().GetRootSurfaceId(), id1); | 253 RemoveSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 261 | 254 |
| 262 // Removing the reference from the root to id1 should allow all three surfaces | 255 // Removing the reference from the root to id1 should allow all three surfaces |
| 263 // to be deleted during GC. | 256 // to be deleted during GC. |
| 264 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id1)); | 257 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id1)); |
| 265 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id2)); | 258 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id2)); |
| 266 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id3)); | 259 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id3)); |
| 267 } | 260 } |
| 268 | 261 |
| 269 TEST_F(SurfaceManagerRefTest, TryDoubleAddReference) { | 262 TEST_F(SurfaceManagerRefTest, TryDoubleAddReference) { |
| 270 SurfaceId id1 = CreateSurface(kFrameSink1, 1); | 263 SurfaceId id1 = CreateSurface(kFrameSink1, 1); |
| 271 SurfaceId id2 = CreateSurface(kFrameSink2, 1); | 264 SurfaceId id2 = CreateSurface(kFrameSink2, 1); |
| 272 | 265 |
| 273 manager().AddSurfaceReference(manager().GetRootSurfaceId(), id1); | 266 AddSurfaceReference(manager().GetRootSurfaceId(), id1); |
| 274 manager().AddSurfaceReference(id1, id2); | 267 AddSurfaceReference(id1, id2); |
| 275 EXPECT_THAT(GetReferencesFor(id2), SizeIs(1)); | 268 EXPECT_THAT(GetReferencesFor(id2), SizeIs(1)); |
| 276 EXPECT_THAT(GetReferencesFrom(id1), SizeIs(1)); | 269 EXPECT_THAT(GetReferencesFrom(id1), SizeIs(1)); |
| 277 | 270 |
| 278 // The second request should be ignored without crashing. | 271 // The second request should be ignored without crashing. |
| 279 manager().AddSurfaceReference(id1, id2); | 272 AddSurfaceReference(id1, id2); |
| 280 EXPECT_THAT(GetReferencesFor(id2), SizeIs(1)); | 273 EXPECT_THAT(GetReferencesFor(id2), SizeIs(1)); |
| 281 EXPECT_THAT(GetReferencesFrom(id1), SizeIs(1)); | 274 EXPECT_THAT(GetReferencesFrom(id1), SizeIs(1)); |
| 282 } | 275 } |
| 283 | 276 |
| 284 TEST_F(SurfaceManagerRefTest, TryAddSelfReference) { | 277 TEST_F(SurfaceManagerRefTest, TryAddSelfReference) { |
| 285 SurfaceId id1 = CreateSurface(kFrameSink1, 1); | 278 SurfaceId id1 = CreateSurface(kFrameSink2, 1); |
| 286 | 279 |
| 287 // A temporary reference must exist to |id1|. | 280 // A temporary reference must exist to |id1|. |
| 288 EXPECT_THAT(GetReferencesFor(id1), SizeIs(1)); | 281 EXPECT_THAT(GetAllTempReferences(), ElementsAre(id1)); |
| 282 EXPECT_THAT(GetReferencesFrom(id1), IsEmpty()); | |
| 283 EXPECT_THAT(GetReferencesFor(id1), IsEmpty()); | |
| 289 | 284 |
| 290 // Try to add a self reference. This should fail. | 285 // Try to add a self reference. This should fail. |
| 291 manager().AddSurfaceReference(id1, id1); | 286 AddSurfaceReference(id1, id1); |
| 292 | 287 |
| 293 // Adding a self reference should be ignored without crashing. | 288 // Adding a self reference should be ignored without crashing. The temporary |
| 289 // reference to |id1| must still exist. | |
| 290 EXPECT_THAT(GetAllTempReferences(), ElementsAre(id1)); | |
| 294 EXPECT_THAT(GetReferencesFrom(id1), IsEmpty()); | 291 EXPECT_THAT(GetReferencesFrom(id1), IsEmpty()); |
| 295 | 292 EXPECT_THAT(GetReferencesFor(id1), IsEmpty()); |
| 296 // The temporary reference to |id1| must still exist. | |
| 297 EXPECT_THAT(GetReferencesFor(id1), SizeIs(1)); | |
| 298 } | 293 } |
| 299 | 294 |
| 300 TEST_F(SurfaceManagerRefTest, TryRemoveBadReference) { | 295 TEST_F(SurfaceManagerRefTest, TryRemoveBadReference) { |
| 301 SurfaceId id1 = CreateSurface(kFrameSink1, 1); | 296 SurfaceId id1 = CreateSurface(kFrameSink1, 1); |
| 302 SurfaceId id2 = CreateSurface(kFrameSink2, 1); | 297 SurfaceId id2 = CreateSurface(kFrameSink2, 1); |
| 303 | 298 |
| 304 // Removing non-existent reference should be ignored. | 299 // Removing non-existent reference should be ignored. |
| 305 manager().AddSurfaceReference(id1, id2); | 300 AddSurfaceReference(id1, id2); |
| 306 RemoveSurfaceReference(id2, id1); | 301 RemoveSurfaceReference(id2, id1); |
| 307 EXPECT_THAT(GetReferencesFrom(id1), SizeIs(1)); | 302 EXPECT_THAT(GetReferencesFrom(id1), SizeIs(1)); |
| 308 EXPECT_THAT(GetReferencesFor(id2), SizeIs(1)); | 303 EXPECT_THAT(GetReferencesFor(id2), SizeIs(1)); |
| 309 } | 304 } |
| 310 | 305 |
| 311 TEST_F(SurfaceManagerRefTest, AddSurfaceThenReference) { | 306 TEST_F(SurfaceManagerRefTest, AddSurfaceThenReference) { |
| 312 // Create a new surface. | 307 // Create a new surface. |
| 313 const SurfaceId surface_id = CreateSurface(kFrameSink2, 1); | 308 const SurfaceId surface_id = CreateSurface(kFrameSink2, 1); |
| 314 | 309 |
| 315 // A temporary reference must be added to |surface_id|. | 310 // A temporary reference must be added to |surface_id|. |
| 316 EXPECT_THAT(GetAllTempReferences(), ElementsAre(surface_id)); | 311 EXPECT_THAT(GetAllTempReferences(), ElementsAre(surface_id)); |
| 317 EXPECT_THAT(GetReferencesFromRoot(), ElementsAre(surface_id)); | |
| 318 | 312 |
| 319 // Create |parent_id| and add a real reference from it to |surface_id|. | 313 // Create |parent_id| and add a real reference from it to |surface_id|. |
| 320 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); | 314 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); |
| 321 manager().AddSurfaceReference(parent_id, surface_id); | 315 AddSurfaceReference(parent_id, surface_id); |
| 322 | 316 |
| 323 // The temporary reference to |surface_id| should be gone. | 317 // The temporary reference to |surface_id| should be gone. |
| 324 // The only temporary reference should be to |parent_id|. | 318 // The only temporary reference should be to |parent_id|. |
| 325 // There must be a real reference from |parent_id| to |child_id|. | 319 // There must be a real reference from |parent_id| to |child_id|. |
| 326 EXPECT_THAT(GetAllTempReferences(), ElementsAre(parent_id)); | 320 EXPECT_THAT(GetAllTempReferences(), ElementsAre(parent_id)); |
| 327 EXPECT_THAT(GetReferencesFromRoot(), ElementsAre(parent_id)); | |
| 328 EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id)); | 321 EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id)); |
| 329 } | 322 } |
| 330 | 323 |
| 331 TEST_F(SurfaceManagerRefTest, AddSurfaceThenRootReference) { | 324 TEST_F(SurfaceManagerRefTest, AddSurfaceThenRootReference) { |
| 332 // Create a new surface. | 325 // Create a new surface. |
| 333 const SurfaceId surface_id = CreateSurface(kFrameSink1, 1); | 326 const SurfaceId surface_id = CreateSurface(kFrameSink1, 1); |
| 334 | 327 |
| 335 // Temporary reference should be added to |surface_id|. | 328 // Temporary reference should be added to |surface_id|. |
| 336 EXPECT_THAT(GetAllTempReferences(), ElementsAre(surface_id)); | 329 EXPECT_THAT(GetAllTempReferences(), ElementsAre(surface_id)); |
| 337 EXPECT_THAT(GetReferencesFromRoot(), ElementsAre(surface_id)); | |
| 338 | 330 |
| 339 // Add a real reference from root to |surface_id|. | 331 // Add a real reference from root to |surface_id|. |
| 340 manager().AddSurfaceReference(manager().GetRootSurfaceId(), surface_id); | 332 AddSurfaceReference(manager().GetRootSurfaceId(), surface_id); |
| 341 | 333 |
| 342 // The temporary reference should be gone. | 334 // The temporary reference should be gone. |
| 343 // There should now be a real reference from root to |surface_id|. | 335 // There should now be a real reference from root to |surface_id|. |
| 344 EXPECT_TRUE(GetAllTempReferences().empty()); | 336 EXPECT_TRUE(GetAllTempReferences().empty()); |
| 345 EXPECT_THAT(GetReferencesFromRoot(), ElementsAre(surface_id)); | 337 EXPECT_THAT(GetReferencesFrom(manager().GetRootSurfaceId()), |
| 338 ElementsAre(surface_id)); | |
| 346 } | 339 } |
| 347 | 340 |
| 348 TEST_F(SurfaceManagerRefTest, AddTwoSurfacesThenOneReference) { | 341 TEST_F(SurfaceManagerRefTest, AddTwoSurfacesThenOneReference) { |
| 349 // Create two surfaces with different FrameSinkIds. | 342 // Create two surfaces with different FrameSinkIds. |
| 350 const SurfaceId surface_id1 = CreateSurface(kFrameSink2, 1); | 343 const SurfaceId surface_id1 = CreateSurface(kFrameSink2, 1); |
| 351 const SurfaceId surface_id2 = CreateSurface(kFrameSink3, 1); | 344 const SurfaceId surface_id2 = CreateSurface(kFrameSink3, 1); |
| 352 | 345 |
| 353 // Temporary reference should be added for both surfaces. | 346 // Temporary reference should be added for both surfaces. |
| 354 EXPECT_THAT(GetAllTempReferences(), | 347 EXPECT_THAT(GetAllTempReferences(), |
| 355 UnorderedElementsAre(surface_id1, surface_id2)); | 348 UnorderedElementsAre(surface_id1, surface_id2)); |
| 356 EXPECT_THAT(GetReferencesFromRoot(), | |
| 357 UnorderedElementsAre(surface_id1, surface_id2)); | |
| 358 | 349 |
| 359 // Create |parent_id| and add a real reference from it to |surface_id1|. | 350 // Create |parent_id| and add a real reference from it to |surface_id1|. |
| 360 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); | 351 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); |
| 361 manager().AddSurfaceReference(parent_id, surface_id1); | 352 AddSurfaceReference(parent_id, surface_id1); |
| 362 | 353 |
| 363 // Real reference must be added to |surface_id1| and the temporary reference | 354 // Real reference must be added to |surface_id1| and the temporary reference |
| 364 // to it must be gone. | 355 // to it must be gone. |
| 365 // There should still be a temporary reference left to |surface_id2|. | 356 // There should still be a temporary reference left to |surface_id2|. |
| 366 // A temporary reference to |parent_id| must be created. | 357 // A temporary reference to |parent_id| must be created. |
| 367 EXPECT_THAT(GetAllTempReferences(), | 358 EXPECT_THAT(GetAllTempReferences(), |
| 368 UnorderedElementsAre(parent_id, surface_id2)); | 359 UnorderedElementsAre(parent_id, surface_id2)); |
| 369 EXPECT_THAT(GetReferencesFromRoot(), | |
| 370 UnorderedElementsAre(parent_id, surface_id2)); | |
| 371 EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id1)); | 360 EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id1)); |
| 372 } | 361 } |
| 373 | 362 |
| 374 TEST_F(SurfaceManagerRefTest, AddSurfacesSkipReference) { | 363 TEST_F(SurfaceManagerRefTest, AddSurfacesSkipReference) { |
| 375 // Add two surfaces that have the same FrameSinkId. This would happen when a | 364 // Add two surfaces that have the same FrameSinkId. This would happen when a |
| 376 // client submits two CompositorFrames before parent submits a new | 365 // client submits two CompositorFrames before parent submits a new |
| 377 // CompositorFrame. | 366 // CompositorFrame. |
| 378 const SurfaceId surface_id1 = CreateSurface(kFrameSink2, 2); | 367 const SurfaceId surface_id1 = CreateSurface(kFrameSink2, 2); |
| 379 const SurfaceId surface_id2 = CreateSurface(kFrameSink2, 1); | 368 const SurfaceId surface_id2 = CreateSurface(kFrameSink2, 1); |
| 380 | 369 |
| 381 // Temporary references should be added for both surfaces and they should be | 370 // Temporary references should be added for both surfaces and they should be |
| 382 // stored in the order of creation. | 371 // stored in the order of creation. |
| 383 EXPECT_THAT(GetTempReferencesFor(surface_id1.frame_sink_id()), | 372 EXPECT_THAT(GetAllTempReferences(), |
| 384 ElementsAre(surface_id1.local_surface_id(), | |
| 385 surface_id2.local_surface_id())); | |
| 386 EXPECT_THAT(GetReferencesFromRoot(), | |
| 387 UnorderedElementsAre(surface_id1, surface_id2)); | 373 UnorderedElementsAre(surface_id1, surface_id2)); |
| 388 | 374 |
| 389 // Create |parent_id| and add a reference from it to |surface_id2| which was | 375 // Create |parent_id| and add a reference from it to |surface_id2| which was |
| 390 // created later. | 376 // created later. |
| 391 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); | 377 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); |
| 392 manager().AddSurfaceReference(parent_id, surface_id2); | 378 AddSurfaceReference(parent_id, surface_id2); |
| 393 | 379 |
| 394 // The real reference should be added for |surface_id2| and the temporary | 380 // The real reference should be added for |surface_id2| and the temporary |
| 395 // references to both |surface_id1| and |surface_id2| should be gone. | 381 // references to both |surface_id1| and |surface_id2| should be gone. |
| 396 // There should be a temporary reference to |parent_id|. | 382 // There should be a temporary reference to |parent_id|. |
| 397 EXPECT_THAT(GetAllTempReferences(), ElementsAre(parent_id)); | 383 EXPECT_THAT(GetAllTempReferences(), ElementsAre(parent_id)); |
| 398 EXPECT_THAT(GetReferencesFromRoot(), ElementsAre(parent_id)); | |
| 399 EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id2)); | 384 EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id2)); |
| 400 } | 385 } |
| 401 | 386 |
| 402 TEST_F(SurfaceManagerRefTest, RemoveFirstTempRefOnly) { | 387 TEST_F(SurfaceManagerRefTest, RemoveFirstTempRefOnly) { |
| 403 // Add two surfaces that have the same FrameSinkId. This would happen when a | 388 // Add two surfaces that have the same FrameSinkId. This would happen when a |
| 404 // client submits two CFs before parent submits a new CF. | 389 // client submits two CFs before parent submits a new CF. |
| 405 const SurfaceId surface_id1 = CreateSurface(kFrameSink2, 1); | 390 const SurfaceId surface_id1 = CreateSurface(kFrameSink2, 1); |
| 406 const SurfaceId surface_id2 = CreateSurface(kFrameSink2, 2); | 391 const SurfaceId surface_id2 = CreateSurface(kFrameSink2, 2); |
| 407 | 392 |
| 408 // Temporary references should be added for both surfaces and they should be | 393 // Temporary references should be added for both surfaces and they should be |
| 409 // stored in the order of creation. | 394 // stored in the order of creation. |
| 410 EXPECT_THAT(GetTempReferencesFor(surface_id1.frame_sink_id()), | 395 EXPECT_THAT(GetAllTempReferences(), |
| 411 ElementsAre(surface_id1.local_surface_id(), | |
| 412 surface_id2.local_surface_id())); | |
| 413 EXPECT_THAT(GetReferencesFromRoot(), | |
| 414 UnorderedElementsAre(surface_id1, surface_id2)); | 396 UnorderedElementsAre(surface_id1, surface_id2)); |
| 415 | 397 |
| 416 // Create |parent_id| and add a reference from it to |surface_id1| which was | 398 // Create |parent_id| and add a reference from it to |surface_id1| which was |
| 417 // created earlier. | 399 // created earlier. |
| 418 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); | 400 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); |
| 419 manager().AddSurfaceReference(parent_id, surface_id1); | 401 AddSurfaceReference(parent_id, surface_id1); |
| 420 | 402 |
| 421 // The real reference should be added for |surface_id1| and its temporary | 403 // The real reference should be added for |surface_id1| and its temporary |
| 422 // reference should be removed. The temporary reference for |surface_id2| | 404 // reference should be removed. The temporary reference for |surface_id2| |
| 423 // should remain. A temporary reference must be added for |parent_id|. | 405 // should remain. A temporary reference must be added for |parent_id|. |
| 424 EXPECT_THAT(GetAllTempReferences(), | 406 EXPECT_THAT(GetAllTempReferences(), |
| 425 UnorderedElementsAre(parent_id, surface_id2)); | 407 UnorderedElementsAre(parent_id, surface_id2)); |
| 426 EXPECT_THAT(GetReferencesFromRoot(), | |
| 427 UnorderedElementsAre(parent_id, surface_id2)); | |
| 428 EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id1)); | 408 EXPECT_THAT(GetReferencesFrom(parent_id), ElementsAre(surface_id1)); |
| 429 } | 409 } |
| 430 | 410 |
| 411 TEST_F(SurfaceManagerRefTest, TemporaryReferenceNotGarbageCollected) { | |
|
Fady Samuel
2017/02/24 19:08:11
Top level comment about what the unit test is tryi
kylechar
2017/02/24 19:28:01
I changed the name to be more descriptive. PTAL in
| |
| 412 const SurfaceId id1 = CreateSurface(kFrameSink1, 1); | |
| 413 AddSurfaceReference(manager().GetRootSurfaceId(), id1); | |
| 414 | |
| 415 const SurfaceId id2 = CreateSurface(kFrameSink2, 1); | |
| 416 ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id2)); | |
| 417 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); | |
| 418 | |
| 419 // Destroy both surfaces and remove the reference to |id1|. | |
|
Fady Samuel
2017/02/24 19:08:11
Add comment about when garbage collection happens.
kylechar
2017/02/24 19:28:01
Done in other CL.
| |
| 420 DestroySurface(id1); | |
| 421 DestroySurface(id2); | |
| 422 RemoveSurfaceReference(manager().GetRootSurfaceId(), id1); | |
| 423 | |
| 424 // Without any references |id1| is deleted. There is still a temporary | |
|
Fady Samuel
2017/02/24 19:08:11
Move the "There is still a temporary reference to
kylechar
2017/02/24 19:28:01
Done in other CL.
| |
| 425 // reference to |id2| so it is not deleted. | |
| 426 EXPECT_EQ(nullptr, manager().GetSurfaceForId(id1)); | |
| 427 EXPECT_NE(nullptr, manager().GetSurfaceForId(id2)); | |
| 428 } | |
| 429 | |
| 430 TEST_F(SurfaceManagerRefTest, AssignTempReference) { | |
|
Fady Samuel
2017/02/24 19:08:11
Top level comment about what the unit test is tryi
kylechar
2017/02/24 19:57:29
Done.
| |
| 431 // The newly created surface should have a temporary reference. | |
| 432 const SurfaceId id1 = CreateSurface(kFrameSink2, 1); | |
| 433 ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id1)); | |
| 434 | |
| 435 // It should still have a temporary reference after an owner is assigned. | |
| 436 manager().AssignTemporaryReference(id1, kFrameSink1); | |
| 437 ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id1)); | |
| 438 | |
| 439 // When |kFrameSink1| is invalidated the temporary reference will be removed. | |
| 440 manager().InvalidateFrameSinkId(kFrameSink1); | |
| 441 ASSERT_THAT(GetAllTempReferences(), IsEmpty()); | |
| 442 } | |
| 443 | |
| 444 TEST_F(SurfaceManagerRefTest, InvalidateHasNoEffectOnSurfaceReferences) { | |
|
Fady Samuel
2017/02/24 19:08:11
Top level comment about what the unit test is tryi
kylechar
2017/02/24 19:57:29
Done.
| |
| 445 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); | |
| 446 AddSurfaceReference(manager().GetRootSurfaceId(), parent_id); | |
| 447 | |
| 448 const SurfaceId id1 = CreateSurface(kFrameSink2, 1); | |
| 449 manager().AssignTemporaryReference(id1, kFrameSink1); | |
| 450 ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id1)); | |
| 451 | |
| 452 // Adding a real surface reference will remove the temporary reference. | |
| 453 AddSurfaceReference(parent_id, id1); | |
| 454 ASSERT_THAT(GetAllTempReferences(), IsEmpty()); | |
| 455 ASSERT_THAT(GetReferencesFor(id1), UnorderedElementsAre(parent_id)); | |
| 456 | |
| 457 // When |kFrameSink1| is invalidated it shouldn't change the surface | |
| 458 // references. | |
| 459 manager().InvalidateFrameSinkId(kFrameSink1); | |
| 460 ASSERT_THAT(GetReferencesFor(id1), UnorderedElementsAre(parent_id)); | |
| 461 } | |
| 462 | |
| 463 TEST_F(SurfaceManagerRefTest, DropTempReference) { | |
|
Fady Samuel
2017/02/24 19:08:11
Top level comment about what the unit test is tryi
kylechar
2017/02/24 19:57:29
Done-ish.
| |
| 464 const SurfaceId id1 = CreateSurface(kFrameSink1, 1); | |
| 465 ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id1)); | |
| 466 | |
| 467 // An example of why this could happen is the window server doesn't know the | |
| 468 // owner, maybe it has crashed and been cleanup already, and asks to drop the | |
| 469 // temporary reference. | |
| 470 manager().DropTemporaryReference(id1); | |
| 471 ASSERT_THAT(GetAllTempReferences(), IsEmpty()); | |
| 472 } | |
| 473 | |
| 474 TEST_F(SurfaceManagerRefTest, DropOnlyOneTempReference) { | |
|
Fady Samuel
2017/02/24 19:08:11
Top level comment about what the unit test is tryi
kylechar
2017/02/24 19:57:29
Done.
| |
| 475 const SurfaceId parent_id = CreateSurface(kFrameSink1, 1); | |
| 476 AddSurfaceReference(manager().GetRootSurfaceId(), parent_id); | |
| 477 | |
| 478 const SurfaceId id1a = CreateSurface(kFrameSink2, 1); | |
| 479 const SurfaceId id1b = CreateSurface(kFrameSink2, 2); | |
| 480 | |
| 481 ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id1a, id1b)); | |
| 482 | |
| 483 // Assign |id1a| to |kFrameSink1|. This doesn't change the temporary | |
| 484 // reference, it just assigns as owner to it. | |
| 485 manager().AssignTemporaryReference(id1a, kFrameSink1); | |
| 486 ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id1a, id1b)); | |
| 487 | |
| 488 // When the owner is invalidated the temporary reference to |id2a| will | |
| 489 // removed. | |
| 490 manager().InvalidateFrameSinkId(kFrameSink1); | |
| 491 ASSERT_THAT(GetAllTempReferences(), UnorderedElementsAre(id1b)); | |
| 492 | |
| 493 // If the owner is invalidated then the Window Server might not | |
|
Fady Samuel
2017/02/24 19:08:11
Incomplete sentence?
kylechar
2017/02/24 19:57:29
Done.
| |
| 494 manager().DropTemporaryReference(id1b); | |
| 495 ASSERT_THAT(GetAllTempReferences(), IsEmpty()); | |
| 496 } | |
| 497 | |
| 431 } // namespace cc | 498 } // namespace cc |
| OLD | NEW |