Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: cc/surfaces/surface_manager.cc

Issue 2455663003: Add cc::Surface ref counting. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "cc/surfaces/surface_manager.h" 5 #include "cc/surfaces/surface_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <utility>
11
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "cc/surfaces/surface.h" 13 #include "cc/surfaces/surface.h"
12 #include "cc/surfaces/surface_factory_client.h" 14 #include "cc/surfaces/surface_factory_client.h"
13 #include "cc/surfaces/surface_id_allocator.h" 15 #include "cc/surfaces/surface_id_allocator.h"
14 16
15 namespace cc { 17 namespace cc {
16 18
17 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping() 19 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping()
18 : client(nullptr), source(nullptr) {} 20 : client(nullptr), source(nullptr) {}
19 21
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 void SurfaceManager::Destroy(std::unique_ptr<Surface> surface) { 63 void SurfaceManager::Destroy(std::unique_ptr<Surface> surface) {
62 DCHECK(thread_checker_.CalledOnValidThread()); 64 DCHECK(thread_checker_.CalledOnValidThread());
63 surface->set_destroyed(true); 65 surface->set_destroyed(true);
64 surfaces_to_destroy_.push_back(std::move(surface)); 66 surfaces_to_destroy_.push_back(std::move(surface));
65 GarbageCollectSurfaces(); 67 GarbageCollectSurfaces();
66 } 68 }
67 69
68 void SurfaceManager::DidSatisfySequences(const FrameSinkId& frame_sink_id, 70 void SurfaceManager::DidSatisfySequences(const FrameSinkId& frame_sink_id,
69 std::vector<uint32_t>* sequence) { 71 std::vector<uint32_t>* sequence) {
70 DCHECK(thread_checker_.CalledOnValidThread()); 72 DCHECK(thread_checker_.CalledOnValidThread());
71 for (std::vector<uint32_t>::iterator it = sequence->begin(); 73 for (uint32_t value : *sequence) {
72 it != sequence->end(); 74 satisfied_sequences_.insert(SurfaceSequence(frame_sink_id, value));
73 ++it) {
74 satisfied_sequences_.insert(SurfaceSequence(frame_sink_id, *it));
75 } 75 }
76 sequence->clear(); 76 sequence->clear();
77 GarbageCollectSurfaces(); 77 GarbageCollectSurfaces();
78 } 78 }
79 79
80 void SurfaceManager::RegisterFrameSinkId(const FrameSinkId& frame_sink_id) { 80 void SurfaceManager::RegisterFrameSinkId(const FrameSinkId& frame_sink_id) {
81 bool inserted = valid_frame_sink_ids_.insert(frame_sink_id).second; 81 bool inserted = valid_frame_sink_ids_.insert(frame_sink_id).second;
82 DCHECK(inserted); 82 DCHECK(inserted);
83 } 83 }
84 84
85 void SurfaceManager::InvalidateFrameSinkId(const FrameSinkId& frame_sink_id) { 85 void SurfaceManager::InvalidateFrameSinkId(const FrameSinkId& frame_sink_id) {
86 valid_frame_sink_ids_.erase(frame_sink_id); 86 valid_frame_sink_ids_.erase(frame_sink_id);
87 GarbageCollectSurfaces(); 87 GarbageCollectSurfaces();
88 } 88 }
89 89
90 void SurfaceManager::AddSurfaceIdReference(const SurfaceId& parent_id,
91 const SurfaceId& child_id) {
92 DCHECK(thread_checker_.CalledOnValidThread());
93 LOG(ERROR) << "SurfaceManager::AddSurfaceIdReference\n parent_id="
94 << parent_id.ToString() << "\n child_id=" << child_id.ToString();
95
96 // Should we allow multiple references? Maybe? IDK.
Fady Samuel 2016/10/27 04:14:48 To reduce complexity here and IPCs I think we shou
kylechar 2016/10/27 21:14:09 Changed per our offline discussion.
97 DCHECK_EQ(parent_to_child_refs_[parent_id].count(child_id), 0u);
98 DCHECK_EQ(child_to_parent_refs_[child_id].count(parent_id), 0u);
99
100 parent_to_child_refs_[parent_id].insert(child_id);
101 child_to_parent_refs_[child_id].insert(parent_id);
102
103 DCHECK_EQ(parent_to_child_refs_[parent_id].count(child_id), 1u);
104 DCHECK_EQ(child_to_parent_refs_[child_id].count(parent_id), 1u);
105 }
106
107 void SurfaceManager::RemoveSurfaceIdReference(const SurfaceId& parent_id,
Fady Samuel 2016/10/27 04:14:48 We should garbage collect if a surface ID no longe
108 const SurfaceId& child_id) {
109 DCHECK(thread_checker_.CalledOnValidThread());
110 LOG(ERROR) << "SurfaceManager::RemoveSurfaceIdReference\n parent_id="
111 << parent_id.ToString() << "\n child_id=" << child_id.ToString();
112
113 // Remove the reference from parent to child. This doesn't change anything
114 // about the validity of the parent.
115 DCHECK_EQ(parent_to_child_refs_.count(parent_id), 1u);
Fady Samuel 2016/10/27 04:14:48 This actually doesn't work very well for restartab
kylechar 2016/10/27 21:14:09 I'm checking the reference exists instead of DCHEC
116 DCHECK_EQ(parent_to_child_refs_[parent_id].count(child_id), 1u);
117 parent_to_child_refs_[parent_id].erase(child_id);
118
119 // Remove the reference from child to parent. This might drop the number of
120 // references to the child down to zero.
121 DCHECK_EQ(child_to_parent_refs_.count(child_id), 1u);
122 SurfaceIdSet& child_refs = child_to_parent_refs_[child_id];
123 DCHECK_EQ(child_refs.count(parent_id), 1u);
124 child_refs.erase(parent_id);
125
126 // Check if child has zero reference, if so we unref all children it
127 // references.
128 if (child_refs.empty()) {
129 LOG(ERROR) << child_id.ToString() << " dropped to ZERO references.";
130 SurfaceIdSet child_child_refs = parent_to_child_refs_[child_id]; // Copy
Fady Samuel 2016/10/27 04:14:48 Add a comment here that you need to do this becaus
kylechar 2016/10/27 21:14:09 Changed, PTAL.
131 for (const SurfaceId& surface_id : child_child_refs) {
132 RemoveSurfaceIdReference(child_id, surface_id);
133 }
134 DCHECK_EQ(CountSurfaceReferees(child_id), 0);
Fady Samuel 2016/10/27 04:14:48 We should start a garbage collect if a surface ID
kylechar 2016/10/27 21:14:09 Done. I've added an optional parameter to avoid GC
135 }
136 }
137
138 void SurfaceManager::RemoveAllReferencesForFrameSink(
139 const FrameSinkId& frame_sink_id) {
140 LOG(ERROR) << "SurfaceManager::DropReferencesForFrameSink frame_sink_id="
141 << frame_sink_id.ToString();
142
143 // Find set of surfaces that belong to the FrameSink.
144 SurfaceIdSet frame_sink_surfaces;
145 for (auto& map_entry : parent_to_child_refs_) {
Fady Samuel 2016/10/27 04:14:48 Could you add a description of what we discussed h
Fady Samuel 2016/10/27 04:14:48 nit: const auto&
kylechar 2016/10/27 21:14:09 Done.
146 const SurfaceId& surface_id = map_entry.first;
147 if (surface_id.frame_sink_id() == frame_sink_id)
148 frame_sink_surfaces.insert(surface_id);
149 }
150 for (auto& map_entry : child_to_parent_refs_) {
151 const SurfaceId& surface_id = map_entry.first;
152 if (surface_id.frame_sink_id() == frame_sink_id)
153 frame_sink_surfaces.insert(surface_id);
Fady Samuel 2016/10/27 04:14:48 I'm confused about when we'd hit this case?
kylechar 2016/10/27 21:14:09 This is for leaf surfaces. So if we have A -> B ->
154 }
155
156 // Clear all references involving FrameSink surfaces.
Fady Samuel 2016/10/27 04:14:48 nit: This comment is confusing. // Clear all surfa
kylechar 2016/10/27 21:14:09 Done.
157 for (const SurfaceId& surface_id : frame_sink_surfaces) {
158 // Remove any references from parents to this surface, since this surface
Fady Samuel 2016/10/27 04:14:48 It seems like all the code in this block could mov
kylechar 2016/10/27 21:14:09 Done.
159 // no longer exists. This surface will have zero references when finished.
160 if (child_to_parent_refs_.count(surface_id) > 0) {
161 SurfaceIdSet parent_refs = child_to_parent_refs_[surface_id]; // Copy
162 for (auto& parent_id : parent_refs)
163 RemoveSurfaceIdReference(parent_id, surface_id);
164 }
165 DCHECK_EQ(CountSurfaceReferences(surface_id), 0);
166
167 // Remove any references from this surface to children, since this surface
168 // no longer exists. Any child surfaces may drop to zero references here,
169 // recursively clearing any references it holds.
170 if (parent_to_child_refs_.count(surface_id) > 0) {
171 SurfaceIdSet child_refs = parent_to_child_refs_[surface_id]; // Copy
172 for (auto& child_id : child_refs)
173 RemoveSurfaceIdReference(surface_id, child_id);
174 }
175 DCHECK_EQ(CountSurfaceReferees(surface_id), 0);
176 }
177 }
178
179 int SurfaceManager::CountSurfaceReferences(const SurfaceId& surface_id) const {
Fady Samuel 2016/10/27 04:14:48 nit: GetSurfaceReferenceCount? Make it uint32_t?
kylechar 2016/10/27 21:14:09 Changed name. If I'm going to use an unsigned type
180 auto iter = child_to_parent_refs_.find(surface_id);
181 if (iter == child_to_parent_refs_.end())
182 return 0;
183 return static_cast<int>(iter->second.size());
184 }
185
186 int SurfaceManager::CountSurfaceReferees(const SurfaceId& surface_id) const {
Fady Samuel 2016/10/27 04:14:48 GetReferencedSurfaceCountBySurface?
kylechar 2016/10/27 21:14:09 Changed name.
187 auto iter = parent_to_child_refs_.find(surface_id);
188 if (iter == parent_to_child_refs_.end())
189 return 0;
190 return static_cast<int>(iter->second.size());
191 }
192
90 void SurfaceManager::GarbageCollectSurfaces() { 193 void SurfaceManager::GarbageCollectSurfaces() {
91 // Simple mark and sweep GC. 194 // Simple mark and sweep GC.
92 // TODO(jbauman): Reduce the amount of work when nothing needs to be 195 // TODO(jbauman): Reduce the amount of work when nothing needs to be
93 // destroyed. 196 // destroyed.
94 std::vector<SurfaceId> live_surfaces; 197 std::vector<SurfaceId> live_surfaces;
95 std::set<SurfaceId> live_surfaces_set; 198 std::unordered_set<SurfaceId, SurfaceIdHash> live_surfaces_set;
96 199
97 // GC roots are surfaces that have not been destroyed, or have not had all 200 // GC roots are surfaces that have not been destroyed, or have not had all
98 // their destruction dependencies satisfied. 201 // their destruction dependencies satisfied.
99 for (auto& map_entry : surface_map_) { 202 for (auto& map_entry : surface_map_) {
100 map_entry.second->SatisfyDestructionDependencies(&satisfied_sequences_, 203 const SurfaceId& surface_id = map_entry.first;
101 &valid_frame_sink_ids_); 204 Surface* surface = map_entry.second;
102 if (!map_entry.second->destroyed() || 205 surface->SatisfyDestructionDependencies(&satisfied_sequences_,
103 map_entry.second->GetDestructionDependencyCount()) { 206 &valid_frame_sink_ids_);
104 live_surfaces_set.insert(map_entry.first); 207
105 live_surfaces.push_back(map_entry.first); 208 // Never use both SurfaceSequence and Surface references.
209 DCHECK(surface->GetDestructionDependencyCount() == 0 ||
210 CountSurfaceReferences(surface_id) == 0);
211
212 if (!surface->destroyed() || surface->GetDestructionDependencyCount() > 0 ||
213 CountSurfaceReferences(surface_id) > 0) {
214 live_surfaces_set.insert(surface_id);
215 live_surfaces.push_back(surface_id);
106 } 216 }
107 } 217 }
108 218
109 // Mark all surfaces reachable from live surfaces by adding them to 219 // Mark all surfaces reachable from live surfaces by adding them to
110 // live_surfaces and live_surfaces_set. 220 // live_surfaces and live_surfaces_set.
111 for (size_t i = 0; i < live_surfaces.size(); i++) { 221 for (size_t i = 0; i < live_surfaces.size(); i++) {
112 Surface* surf = surface_map_[live_surfaces[i]]; 222 Surface* surf = surface_map_[live_surfaces[i]];
113 DCHECK(surf); 223 DCHECK(surf);
114 224
115 for (const SurfaceId& id : surf->referenced_surfaces()) { 225 for (const SurfaceId& id : surf->referenced_surfaces()) {
116 if (live_surfaces_set.count(id)) 226 if (live_surfaces_set.count(id))
117 continue; 227 continue;
118 228
119 Surface* surf2 = GetSurfaceForId(id); 229 Surface* surf2 = GetSurfaceForId(id);
120 if (surf2) { 230 if (surf2) {
121 live_surfaces.push_back(id); 231 live_surfaces.push_back(id);
122 live_surfaces_set.insert(id); 232 live_surfaces_set.insert(id);
123 } 233 }
124 } 234 }
125 } 235 }
126 236
127 std::vector<std::unique_ptr<Surface>> to_destroy; 237 std::vector<std::unique_ptr<Surface>> to_destroy;
128 238
129 // Destroy all remaining unreachable surfaces. 239 // Destroy all remaining unreachable surfaces.
130 for (SurfaceDestroyList::iterator dest_it = surfaces_to_destroy_.begin(); 240 for (auto iter = surfaces_to_destroy_.begin();
131 dest_it != surfaces_to_destroy_.end();) { 241 iter != surfaces_to_destroy_.end();) {
132 if (!live_surfaces_set.count((*dest_it)->surface_id())) { 242 SurfaceId surface_id = (*iter)->surface_id();
133 std::unique_ptr<Surface> surf(std::move(*dest_it)); 243 if (!live_surfaces_set.count(surface_id)) {
134 DeregisterSurface(surf->surface_id()); 244 DeregisterSurface(surface_id);
135 dest_it = surfaces_to_destroy_.erase(dest_it); 245 to_destroy.push_back(std::move(*iter));
136 to_destroy.push_back(std::move(surf)); 246 iter = surfaces_to_destroy_.erase(iter);
137 } else { 247 } else {
138 ++dest_it; 248 ++iter;
139 } 249 }
140 } 250 }
141 251
142 to_destroy.clear(); 252 to_destroy.clear();
143 } 253 }
144 254
145 void SurfaceManager::RegisterSurfaceFactoryClient( 255 void SurfaceManager::RegisterSurfaceFactoryClient(
146 const FrameSinkId& frame_sink_id, 256 const FrameSinkId& frame_sink_id,
147 SurfaceFactoryClient* client) { 257 SurfaceFactoryClient* client) {
148 DCHECK(client); 258 DCHECK(client);
149 DCHECK(!frame_sink_source_map_[frame_sink_id].client);
150 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u); 259 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u);
151 260
152 auto iter = frame_sink_source_map_.find(frame_sink_id); 261 // Will create a new FrameSinkSourceMapping for |frame_sink_id| if necessary.
153 if (iter == frame_sink_source_map_.end()) { 262 FrameSinkSourceMapping& frame_sink_source =
154 auto insert_result = frame_sink_source_map_.insert( 263 frame_sink_source_map_[frame_sink_id];
155 std::make_pair(frame_sink_id, FrameSinkSourceMapping())); 264 DCHECK(!frame_sink_source.client);
156 DCHECK(insert_result.second); 265 frame_sink_source.client = client;
157 iter = insert_result.first;
158 }
159 iter->second.client = client;
160 266
161 // Propagate any previously set sources to the new client. 267 // Propagate any previously set sources to the new client.
162 if (iter->second.source) 268 if (frame_sink_source.source)
163 client->SetBeginFrameSource(iter->second.source); 269 client->SetBeginFrameSource(frame_sink_source.source);
164 } 270 }
165 271
166 void SurfaceManager::UnregisterSurfaceFactoryClient( 272 void SurfaceManager::UnregisterSurfaceFactoryClient(
167 const FrameSinkId& frame_sink_id) { 273 const FrameSinkId& frame_sink_id) {
168 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u); 274 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u);
169 DCHECK_EQ(frame_sink_source_map_.count(frame_sink_id), 1u); 275 DCHECK_EQ(frame_sink_source_map_.count(frame_sink_id), 1u);
170 276
277 RemoveAllReferencesForFrameSink(frame_sink_id);
278
171 auto iter = frame_sink_source_map_.find(frame_sink_id); 279 auto iter = frame_sink_source_map_.find(frame_sink_id);
172 if (iter->second.source) 280 if (iter->second.source)
173 iter->second.client->SetBeginFrameSource(nullptr); 281 iter->second.client->SetBeginFrameSource(nullptr);
174 iter->second.client = nullptr; 282 iter->second.client = nullptr;
175 283
176 // The SurfaceFactoryClient and hierarchy can be registered/unregistered 284 // The SurfaceFactoryClient and hierarchy can be registered/unregistered
177 // in either order, so empty namespace_client_map entries need to be 285 // in either order, so empty namespace_client_map entries need to be
178 // checked when removing either clients or relationships. 286 // checked when removing either clients or relationships.
179 if (iter->second.is_empty()) 287 if (iter->second.is_empty())
180 frame_sink_source_map_.erase(iter); 288 frame_sink_source_map_.erase(iter);
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 // TODO(enne): these walks could be done in one step. 439 // TODO(enne): these walks could be done in one step.
332 RecursivelyDetachBeginFrameSource(child_frame_sink_id, parent_source); 440 RecursivelyDetachBeginFrameSource(child_frame_sink_id, parent_source);
333 for (auto source_iter : registered_sources_) 441 for (auto source_iter : registered_sources_)
334 RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first); 442 RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first);
335 } 443 }
336 444
337 Surface* SurfaceManager::GetSurfaceForId(const SurfaceId& surface_id) { 445 Surface* SurfaceManager::GetSurfaceForId(const SurfaceId& surface_id) {
338 DCHECK(thread_checker_.CalledOnValidThread()); 446 DCHECK(thread_checker_.CalledOnValidThread());
339 SurfaceMap::iterator it = surface_map_.find(surface_id); 447 SurfaceMap::iterator it = surface_map_.find(surface_id);
340 if (it == surface_map_.end()) 448 if (it == surface_map_.end())
341 return NULL; 449 return nullptr;
342 return it->second; 450 return it->second;
343 } 451 }
344 452
345 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id) { 453 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id) {
346 CHECK(thread_checker_.CalledOnValidThread()); 454 CHECK(thread_checker_.CalledOnValidThread());
347 bool changed = false; 455 bool changed = false;
348 for (auto& observer : observer_list_) 456 for (auto& observer : observer_list_)
349 observer.OnSurfaceDamaged(surface_id, &changed); 457 observer.OnSurfaceDamaged(surface_id, &changed);
350 return changed; 458 return changed;
351 } 459 }
352 460
353 void SurfaceManager::SurfaceCreated(const SurfaceId& surface_id, 461 void SurfaceManager::SurfaceCreated(const SurfaceId& surface_id,
354 const gfx::Size& frame_size, 462 const gfx::Size& frame_size,
355 float device_scale_factor) { 463 float device_scale_factor) {
356 CHECK(thread_checker_.CalledOnValidThread()); 464 CHECK(thread_checker_.CalledOnValidThread());
357 for (auto& observer : observer_list_) 465 for (auto& observer : observer_list_)
358 observer.OnSurfaceCreated(surface_id, frame_size, device_scale_factor); 466 observer.OnSurfaceCreated(surface_id, frame_size, device_scale_factor);
359 } 467 }
360 468
361 } // namespace cc 469 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698