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

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

Issue 2940183002: cc: Move ownership of surfaces to SurfaceManager (Closed)
Patch Set: c Created 3 years, 6 months 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 <algorithm> 10 #include <algorithm>
(...skipping 27 matching lines...) Expand all
38 new DirectSurfaceReferenceFactory(weak_factory_.GetWeakPtr()); 38 new DirectSurfaceReferenceFactory(weak_factory_.GetWeakPtr());
39 } 39 }
40 40
41 SurfaceManager::~SurfaceManager() { 41 SurfaceManager::~SurfaceManager() {
42 DCHECK(thread_checker_.CalledOnValidThread()); 42 DCHECK(thread_checker_.CalledOnValidThread());
43 43
44 // Remove all temporary references on shutdown. 44 // Remove all temporary references on shutdown.
45 temporary_references_.clear(); 45 temporary_references_.clear();
46 temporary_reference_ranges_.clear(); 46 temporary_reference_ranges_.clear();
47 47
48 GarbageCollectSurfaces();
49
50 for (SurfaceDestroyList::iterator it = surfaces_to_destroy_.begin();
51 it != surfaces_to_destroy_.end();
52 ++it) {
53 UnregisterSurface((*it)->surface_id());
54 }
55 surfaces_to_destroy_.clear(); 48 surfaces_to_destroy_.clear();
49 surface_map_.clear();
danakj 2017/06/21 21:24:09 Why do we clear these in the destructor here? Thei
Saman Sami 2017/06/26 20:44:05 We actually don't need them so I'll remove them.
56 } 50 }
57 51
58 #if DCHECK_IS_ON() 52 #if DCHECK_IS_ON()
59 std::string SurfaceManager::SurfaceReferencesToString() { 53 std::string SurfaceManager::SurfaceReferencesToString() {
60 std::stringstream str; 54 std::stringstream str;
61 SurfaceReferencesToStringImpl(root_surface_id_, "", &str); 55 SurfaceReferencesToStringImpl(root_surface_id_, "", &str);
62 // Temporary references will have an asterisk in front of them. 56 // Temporary references will have an asterisk in front of them.
63 for (auto& map_entry : temporary_references_) 57 for (auto& map_entry : temporary_references_)
64 SurfaceReferencesToStringImpl(map_entry.first, "* ", &str); 58 SurfaceReferencesToStringImpl(map_entry.first, "* ", &str);
65 59
66 return str.str(); 60 return str.str();
67 } 61 }
68 #endif 62 #endif
69 63
70 void SurfaceManager::SetDependencyTracker( 64 void SurfaceManager::SetDependencyTracker(
71 SurfaceDependencyTracker* dependency_tracker) { 65 SurfaceDependencyTracker* dependency_tracker) {
72 dependency_tracker_ = dependency_tracker; 66 dependency_tracker_ = dependency_tracker;
73 } 67 }
74 68
75 void SurfaceManager::RequestSurfaceResolution(Surface* pending_surface) { 69 void SurfaceManager::RequestSurfaceResolution(Surface* pending_surface) {
76 if (dependency_tracker_) 70 if (dependency_tracker_)
77 dependency_tracker_->RequestSurfaceResolution(pending_surface); 71 dependency_tracker_->RequestSurfaceResolution(pending_surface);
78 } 72 }
79 73
80 std::unique_ptr<Surface> SurfaceManager::CreateSurface( 74 Surface* SurfaceManager::CreateSurface(
81 base::WeakPtr<CompositorFrameSinkSupport> compositor_frame_sink_support, 75 base::WeakPtr<CompositorFrameSinkSupport> compositor_frame_sink_support,
82 const SurfaceInfo& surface_info) { 76 const SurfaceInfo& surface_info) {
83 DCHECK(thread_checker_.CalledOnValidThread()); 77 DCHECK(thread_checker_.CalledOnValidThread());
84 DCHECK(surface_info.is_valid()); 78 DCHECK(surface_info.is_valid());
85 DCHECK(compositor_frame_sink_support); 79 DCHECK(compositor_frame_sink_support);
86 DCHECK_EQ(surface_info.id().frame_sink_id(), 80 DCHECK_EQ(surface_info.id().frame_sink_id(),
87 compositor_frame_sink_support->frame_sink_id()); 81 compositor_frame_sink_support->frame_sink_id());
88 82
89 // If no surface with this SurfaceId exists, simply create the surface and 83 // If no surface with this SurfaceId exists, simply create the surface and
90 // return. 84 // return.
91 auto surface_iter = surface_map_.find(surface_info.id()); 85 auto it = surface_map_.find(surface_info.id());
92 if (surface_iter == surface_map_.end()) { 86 if (it == surface_map_.end()) {
93 auto surface = 87 surface_map_[surface_info.id()] =
94 base::MakeUnique<Surface>(surface_info, compositor_frame_sink_support); 88 base::MakeUnique<Surface>(surface_info, compositor_frame_sink_support);
95 surface_map_[surface->surface_id()] = surface.get(); 89 return surface_map_[surface_info.id()].get();
96 return surface;
97 } 90 }
98 91
99 // If a surface with this SurfaceId exists and it's not marked as destroyed, 92 // If a surface with this SurfaceId exists, it must be marked as destroyed.
100 // we should not receive a request to create a new surface with the same 93 // Otherwise, we wouldn't receive a request to reuse the same SurfaceId.
101 // SurfaceId. 94 // Remove the surface out of the garbage collector's queue and reuse it.
102 DCHECK(surface_iter->second->destroyed()); 95 Surface* surface = it->second.get();
103 96 DCHECK(IsMarkedForDestruction(surface_info.id()));
104 // If a surface with this SurfaceId exists and it's marked as destroyed, 97 surfaces_to_destroy_.erase(surface_info.id());
105 // it means it's in the garbage collector's queue. We simply take it out of
106 // the queue and reuse it.
107 auto it =
108 std::find_if(surfaces_to_destroy_.begin(), surfaces_to_destroy_.end(),
109 [&surface_info](const std::unique_ptr<Surface>& surface) {
110 return surface->surface_id() == surface_info.id();
111 });
112 DCHECK(it != surfaces_to_destroy_.end());
113 std::unique_ptr<Surface> surface = std::move(*it);
114 surfaces_to_destroy_.erase(it);
115 surface->set_destroyed(false);
116 DCHECK_EQ(compositor_frame_sink_support.get(), 98 DCHECK_EQ(compositor_frame_sink_support.get(),
117 surface->compositor_frame_sink_support().get()); 99 surface->compositor_frame_sink_support().get());
118 return surface; 100 return surface;
119 } 101 }
120 102
121 void SurfaceManager::DestroySurface(std::unique_ptr<Surface> surface) { 103 void SurfaceManager::DestroySurface(const SurfaceId& surface_id) {
122 DCHECK(thread_checker_.CalledOnValidThread()); 104 DCHECK(thread_checker_.CalledOnValidThread());
123 surface->set_destroyed(true); 105 DCHECK(surface_map_.count(surface_id));
106 if (IsMarkedForDestruction(surface_id))
107 return;
124 for (auto& observer : observer_list_) 108 for (auto& observer : observer_list_)
125 observer.OnSurfaceDestroyed(surface->surface_id()); 109 observer.OnSurfaceDestroyed(surface_id);
126 surfaces_to_destroy_.push_back(std::move(surface)); 110 surfaces_to_destroy_.insert(surface_id);
127 GarbageCollectSurfaces(); 111 GarbageCollectSurfaces();
128 } 112 }
129 113
130 void SurfaceManager::SurfaceWillDraw(const SurfaceId& surface_id) { 114 void SurfaceManager::SurfaceWillDraw(const SurfaceId& surface_id) {
131 DCHECK(thread_checker_.CalledOnValidThread()); 115 DCHECK(thread_checker_.CalledOnValidThread());
132 for (auto& observer : observer_list_) 116 for (auto& observer : observer_list_)
133 observer.OnSurfaceWillDraw(surface_id); 117 observer.OnSurfaceWillDraw(surface_id);
134 } 118 }
135 119
136 void SurfaceManager::RequireSequence(const SurfaceId& surface_id, 120 void SurfaceManager::RequireSequence(const SurfaceId& surface_id,
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 } 215 }
232 216
233 void SurfaceManager::GarbageCollectSurfaces() { 217 void SurfaceManager::GarbageCollectSurfaces() {
234 if (surfaces_to_destroy_.empty()) 218 if (surfaces_to_destroy_.empty())
235 return; 219 return;
236 220
237 SurfaceIdSet reachable_surfaces = using_surface_references() 221 SurfaceIdSet reachable_surfaces = using_surface_references()
238 ? GetLiveSurfacesForReferences() 222 ? GetLiveSurfacesForReferences()
239 : GetLiveSurfacesForSequences(); 223 : GetLiveSurfacesForSequences();
240 224
241 std::vector<std::unique_ptr<Surface>> surfaces_to_delete; 225 std::vector<SurfaceId> surfaces_to_delete;
242 226
243 // Delete all destroyed and unreachable surfaces. 227 // Delete all destroyed and unreachable surfaces.
244 for (auto iter = surfaces_to_destroy_.begin(); 228 for (auto iter = surfaces_to_destroy_.begin();
245 iter != surfaces_to_destroy_.end();) { 229 iter != surfaces_to_destroy_.end();) {
246 SurfaceId surface_id = (*iter)->surface_id(); 230 if (reachable_surfaces.count(*iter) == 0) {
247 if (reachable_surfaces.count(surface_id) == 0) { 231 surfaces_to_delete.push_back(*iter);
248 UnregisterSurface(surface_id);
249 surfaces_to_delete.push_back(std::move(*iter));
250 iter = surfaces_to_destroy_.erase(iter); 232 iter = surfaces_to_destroy_.erase(iter);
251 } else { 233 } else {
252 ++iter; 234 ++iter;
253 } 235 }
254 } 236 }
255 237
256 // ~Surface() draw callback could modify |surfaces_to_destroy_|. 238 // ~Surface() draw callback could modify |surfaces_to_destroy_|.
257 surfaces_to_delete.clear(); 239 for (const SurfaceId& surface_id : surfaces_to_delete)
240 DestroySurfaceInternal(surface_id);
258 } 241 }
259 242
260 SurfaceManager::SurfaceIdSet SurfaceManager::GetLiveSurfacesForReferences() { 243 SurfaceManager::SurfaceIdSet SurfaceManager::GetLiveSurfacesForReferences() {
261 DCHECK(using_surface_references()); 244 DCHECK(using_surface_references());
262 245
263 SurfaceIdSet reachable_surfaces; 246 SurfaceIdSet reachable_surfaces;
264 247
265 // Walk down from the root and mark each SurfaceId we encounter as reachable. 248 // Walk down from the root and mark each SurfaceId we encounter as reachable.
266 std::queue<SurfaceId> surface_queue; 249 std::queue<SurfaceId> surface_queue;
267 surface_queue.push(root_surface_id_); 250 surface_queue.push(root_surface_id_);
(...skipping 23 matching lines...) Expand all
291 // Simple mark and sweep GC. 274 // Simple mark and sweep GC.
292 // TODO(jbauman): Reduce the amount of work when nothing needs to be 275 // TODO(jbauman): Reduce the amount of work when nothing needs to be
293 // destroyed. 276 // destroyed.
294 std::vector<SurfaceId> live_surfaces; 277 std::vector<SurfaceId> live_surfaces;
295 std::unordered_set<SurfaceId, SurfaceIdHash> live_surfaces_set; 278 std::unordered_set<SurfaceId, SurfaceIdHash> live_surfaces_set;
296 279
297 // GC roots are surfaces that have not been destroyed, or have not had all 280 // GC roots are surfaces that have not been destroyed, or have not had all
298 // their destruction dependencies satisfied. 281 // their destruction dependencies satisfied.
299 for (auto& map_entry : surface_map_) { 282 for (auto& map_entry : surface_map_) {
300 const SurfaceId& surface_id = map_entry.first; 283 const SurfaceId& surface_id = map_entry.first;
301 Surface* surface = map_entry.second; 284 Surface* surface = map_entry.second.get();
302 surface->SatisfyDestructionDependencies(&satisfied_sequences_, 285 surface->SatisfyDestructionDependencies(&satisfied_sequences_,
303 framesink_manager_.GetValidFrameSinkIds()); 286 framesink_manager_.GetValidFrameSinkIds());
304 287
305 if (!surface->destroyed() || surface->GetDestructionDependencyCount() > 0) { 288 if (!IsMarkedForDestruction(surface_id) ||
289 surface->GetDestructionDependencyCount() > 0) {
306 live_surfaces_set.insert(surface_id); 290 live_surfaces_set.insert(surface_id);
307 live_surfaces.push_back(surface_id); 291 live_surfaces.push_back(surface_id);
308 } 292 }
309 } 293 }
310 294
311 // Mark all surfaces reachable from live surfaces by adding them to 295 // Mark all surfaces reachable from live surfaces by adding them to
312 // live_surfaces and live_surfaces_set. 296 // live_surfaces and live_surfaces_set.
313 for (size_t i = 0; i < live_surfaces.size(); i++) { 297 for (size_t i = 0; i < live_surfaces.size(); i++) {
314 Surface* surf = surface_map_[live_surfaces[i]]; 298 Surface* surf = surface_map_[live_surfaces[i]].get();
315 DCHECK(surf); 299 DCHECK(surf);
316 300
317 const auto& children = GetSurfacesReferencedByParent(surf->surface_id()); 301 const auto& children = GetSurfacesReferencedByParent(surf->surface_id());
318 for (const SurfaceId& id : children) { 302 for (const SurfaceId& id : children) {
319 if (live_surfaces_set.count(id)) 303 if (live_surfaces_set.count(id))
320 continue; 304 continue;
321 305
322 Surface* surf2 = GetSurfaceForId(id); 306 Surface* surf2 = GetSurfaceForId(id);
323 if (surf2) { 307 if (surf2) {
324 live_surfaces.push_back(id); 308 live_surfaces.push_back(id);
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 const FrameSinkId& child_frame_sink_id) { 445 const FrameSinkId& child_frame_sink_id) {
462 framesink_manager_.UnregisterFrameSinkHierarchy(parent_frame_sink_id, 446 framesink_manager_.UnregisterFrameSinkHierarchy(parent_frame_sink_id,
463 child_frame_sink_id); 447 child_frame_sink_id);
464 } 448 }
465 449
466 Surface* SurfaceManager::GetSurfaceForId(const SurfaceId& surface_id) { 450 Surface* SurfaceManager::GetSurfaceForId(const SurfaceId& surface_id) {
467 DCHECK(thread_checker_.CalledOnValidThread()); 451 DCHECK(thread_checker_.CalledOnValidThread());
468 SurfaceMap::iterator it = surface_map_.find(surface_id); 452 SurfaceMap::iterator it = surface_map_.find(surface_id);
469 if (it == surface_map_.end()) 453 if (it == surface_map_.end())
470 return nullptr; 454 return nullptr;
471 return it->second; 455 return it->second.get();
472 } 456 }
473 457
474 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id, 458 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id,
475 const BeginFrameAck& ack) { 459 const BeginFrameAck& ack) {
476 CHECK(thread_checker_.CalledOnValidThread()); 460 CHECK(thread_checker_.CalledOnValidThread());
477 bool changed = false; 461 bool changed = false;
478 for (auto& observer : observer_list_) 462 for (auto& observer : observer_list_)
479 changed |= observer.OnSurfaceDamaged(surface_id, ack); 463 changed |= observer.OnSurfaceDamaged(surface_id, ack);
480 return changed; 464 return changed;
481 } 465 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 dependency_tracker_->OnSurfaceDiscarded(surface); 503 dependency_tracker_->OnSurfaceDiscarded(surface);
520 } 504 }
521 505
522 void SurfaceManager::SurfaceDamageExpected(const SurfaceId& surface_id, 506 void SurfaceManager::SurfaceDamageExpected(const SurfaceId& surface_id,
523 const BeginFrameArgs& args) { 507 const BeginFrameArgs& args) {
524 DCHECK(thread_checker_.CalledOnValidThread()); 508 DCHECK(thread_checker_.CalledOnValidThread());
525 for (auto& observer : observer_list_) 509 for (auto& observer : observer_list_)
526 observer.OnSurfaceDamageExpected(surface_id, args); 510 observer.OnSurfaceDamageExpected(surface_id, args);
527 } 511 }
528 512
529 void SurfaceManager::UnregisterSurface(const SurfaceId& surface_id) { 513 void SurfaceManager::DestroySurfaceInternal(const SurfaceId& surface_id) {
530 DCHECK(thread_checker_.CalledOnValidThread()); 514 DCHECK(thread_checker_.CalledOnValidThread());
531 SurfaceMap::iterator it = surface_map_.find(surface_id); 515 SurfaceMap::iterator it = surface_map_.find(surface_id);
532 DCHECK(it != surface_map_.end()); 516 DCHECK(it != surface_map_.end());
517 std::unique_ptr<Surface> doomed = std::move(it->second);
533 surface_map_.erase(it); 518 surface_map_.erase(it);
534 RemoveAllSurfaceReferences(surface_id); 519 RemoveAllSurfaceReferences(surface_id);
535 } 520 }
536 521
537 #if DCHECK_IS_ON() 522 #if DCHECK_IS_ON()
538 void SurfaceManager::SurfaceReferencesToStringImpl(const SurfaceId& surface_id, 523 void SurfaceManager::SurfaceReferencesToStringImpl(const SurfaceId& surface_id,
539 std::string indent, 524 std::string indent,
540 std::stringstream* str) { 525 std::stringstream* str) {
541 *str << indent; 526 *str << indent;
542 527
543 // Print the current line for |surface_id|. 528 // Print the current line for |surface_id|.
544 Surface* surface = GetSurfaceForId(surface_id); 529 Surface* surface = GetSurfaceForId(surface_id);
545 if (surface) { 530 if (surface) {
546 *str << surface->surface_id().ToString(); 531 *str << surface->surface_id().ToString();
547 *str << (surface->destroyed() ? " destroyed" : " live"); 532 *str << (IsMarkedForDestruction(surface_id) ? " destroyed" : " live");
548 533
549 if (surface->HasPendingFrame()) { 534 if (surface->HasPendingFrame()) {
550 // This provides the surface size from the root render pass. 535 // This provides the surface size from the root render pass.
551 const CompositorFrame& frame = surface->GetPendingFrame(); 536 const CompositorFrame& frame = surface->GetPendingFrame();
552 *str << " pending " 537 *str << " pending "
553 << frame.render_pass_list.back()->output_rect.size().ToString(); 538 << frame.render_pass_list.back()->output_rect.size().ToString();
554 } 539 }
555 540
556 if (surface->HasActiveFrame()) { 541 if (surface->HasActiveFrame()) {
557 // This provides the surface size from the root render pass. 542 // This provides the surface size from the root render pass.
558 const CompositorFrame& frame = surface->GetActiveFrame(); 543 const CompositorFrame& frame = surface->GetActiveFrame();
559 *str << " active " 544 *str << " active "
560 << frame.render_pass_list.back()->output_rect.size().ToString(); 545 << frame.render_pass_list.back()->output_rect.size().ToString();
561 } 546 }
562 } else { 547 } else {
563 *str << surface_id; 548 *str << surface_id;
564 } 549 }
565 *str << "\n"; 550 *str << "\n";
566 551
567 // If the current surface has references to children, sort children and print 552 // If the current surface has references to children, sort children and print
568 // references for each child. 553 // references for each child.
569 for (const SurfaceId& child_id : GetSurfacesReferencedByParent(surface_id)) 554 for (const SurfaceId& child_id : GetSurfacesReferencedByParent(surface_id))
570 SurfaceReferencesToStringImpl(child_id, indent + " ", str); 555 SurfaceReferencesToStringImpl(child_id, indent + " ", str);
571 } 556 }
572 #endif // DCHECK_IS_ON() 557 #endif // DCHECK_IS_ON()
573 558
559 bool SurfaceManager::IsMarkedForDestruction(const SurfaceId& surface_id) {
560 return surfaces_to_destroy_.count(surface_id) != 0;
561 }
562
574 } // namespace cc 563 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698