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

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

Issue 2455663003: Add cc::Surface ref counting. (Closed)
Patch Set: More cleanup. 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
19 const SurfaceId SurfaceManager::kRootSurfaceId(FrameSinkId(0u, 0u),
20 LocalFrameId(0u, 0u));
21
17 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping() 22 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping()
18 : client(nullptr), source(nullptr) {} 23 : client(nullptr), source(nullptr) {}
19 24
20 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping( 25 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping(
21 const FrameSinkSourceMapping& other) = default; 26 const FrameSinkSourceMapping& other) = default;
22 27
23 SurfaceManager::FrameSinkSourceMapping::~FrameSinkSourceMapping() { 28 SurfaceManager::FrameSinkSourceMapping::~FrameSinkSourceMapping() {
24 DCHECK(is_empty()) << "client: " << client 29 DCHECK(is_empty()) << "client: " << client
25 << ", children: " << children.size(); 30 << ", children: " << children.size();
26 } 31 }
(...skipping 22 matching lines...) Expand all
49 DCHECK(surface); 54 DCHECK(surface);
50 DCHECK(!surface_map_.count(surface->surface_id())); 55 DCHECK(!surface_map_.count(surface->surface_id()));
51 surface_map_[surface->surface_id()] = surface; 56 surface_map_[surface->surface_id()] = surface;
52 } 57 }
53 58
54 void SurfaceManager::DeregisterSurface(const SurfaceId& surface_id) { 59 void SurfaceManager::DeregisterSurface(const SurfaceId& surface_id) {
55 DCHECK(thread_checker_.CalledOnValidThread()); 60 DCHECK(thread_checker_.CalledOnValidThread());
56 SurfaceMap::iterator it = surface_map_.find(surface_id); 61 SurfaceMap::iterator it = surface_map_.find(surface_id);
57 DCHECK(it != surface_map_.end()); 62 DCHECK(it != surface_map_.end());
58 surface_map_.erase(it); 63 surface_map_.erase(it);
64 child_to_parent_refs_.erase(surface_id);
65 parent_to_child_refs_.erase(surface_id);
59 } 66 }
60 67
61 void SurfaceManager::Destroy(std::unique_ptr<Surface> surface) { 68 void SurfaceManager::Destroy(std::unique_ptr<Surface> surface) {
62 DCHECK(thread_checker_.CalledOnValidThread()); 69 DCHECK(thread_checker_.CalledOnValidThread());
63 surface->set_destroyed(true); 70 surface->set_destroyed(true);
64 surfaces_to_destroy_.push_back(std::move(surface)); 71 surfaces_to_destroy_.push_back(std::move(surface));
65 GarbageCollectSurfaces(); 72 GarbageCollectSurfaces();
66 } 73 }
67 74
68 void SurfaceManager::DidSatisfySequences(const FrameSinkId& frame_sink_id, 75 void SurfaceManager::DidSatisfySequences(const FrameSinkId& frame_sink_id,
69 std::vector<uint32_t>* sequence) { 76 std::vector<uint32_t>* sequence) {
70 DCHECK(thread_checker_.CalledOnValidThread()); 77 DCHECK(thread_checker_.CalledOnValidThread());
71 for (std::vector<uint32_t>::iterator it = sequence->begin(); 78 for (uint32_t value : *sequence) {
Fady Samuel 2016/11/02 03:10:36 nit: drop braces.
kylechar 2016/11/02 13:58:58 Done.
72 it != sequence->end(); 79 satisfied_sequences_.insert(SurfaceSequence(frame_sink_id, value));
73 ++it) {
74 satisfied_sequences_.insert(SurfaceSequence(frame_sink_id, *it));
75 } 80 }
76 sequence->clear(); 81 sequence->clear();
77 GarbageCollectSurfaces(); 82 GarbageCollectSurfaces();
78 } 83 }
79 84
80 void SurfaceManager::RegisterFrameSinkId(const FrameSinkId& frame_sink_id) { 85 void SurfaceManager::RegisterFrameSinkId(const FrameSinkId& frame_sink_id) {
81 bool inserted = valid_frame_sink_ids_.insert(frame_sink_id).second; 86 bool inserted = valid_frame_sink_ids_.insert(frame_sink_id).second;
82 DCHECK(inserted); 87 DCHECK(inserted);
83 } 88 }
84 89
85 void SurfaceManager::InvalidateFrameSinkId(const FrameSinkId& frame_sink_id) { 90 void SurfaceManager::InvalidateFrameSinkId(const FrameSinkId& frame_sink_id) {
86 valid_frame_sink_ids_.erase(frame_sink_id); 91 valid_frame_sink_ids_.erase(frame_sink_id);
87 GarbageCollectSurfaces(); 92 GarbageCollectSurfaces();
88 } 93 }
89 94
95 void SurfaceManager::AddSurfaceReference(const SurfaceId& parent_id,
96 const SurfaceId& child_id) {
97 DCHECK(thread_checker_.CalledOnValidThread());
98
99 // Check some conditions that should never happen. We don't want to crash on
100 // bad input from a compromised client so just return early.
101 if (parent_id == child_id) {
102 LOG(ERROR) << "Cannot add self reference for " << parent_id.ToString();
103 return;
104 }
105 if (parent_id != kRootSurfaceId && surface_map_.count(parent_id) == 0) {
106 LOG(ERROR) << "No surface in map for " << parent_id.ToString();
107 return;
108 }
109 if (surface_map_.count(child_id) == 0) {
110 LOG(ERROR) << "No surface in map for " << child_id.ToString();
111 return;
112 }
113
114 parent_to_child_refs_[parent_id].insert(child_id);
115 child_to_parent_refs_[child_id].insert(parent_id);
116 }
117
118 void SurfaceManager::RemoveSurfaceReference(const SurfaceId& parent_id,
119 const SurfaceId& child_id,
120 bool should_run_gc) {
121 DCHECK(thread_checker_.CalledOnValidThread());
122
123 // Check if we have the reference that is requested to be removed. We don't
124 // want to crash on bad input from a compromised client so just return early.
125 if (parent_to_child_refs_.count(parent_id) == 0 ||
126 parent_to_child_refs_[parent_id].count(child_id) == 0) {
127 LOG(ERROR) << "No reference from " << parent_id.ToString() << " to "
128 << child_id.ToString();
129 return;
130 }
131
132 // Remove the reference from parent to child. This doesn't change anything
133 // about the validity of the parent.
134 parent_to_child_refs_[parent_id].erase(child_id);
135
136 // Remove the reference from child to parent. This might drop the number of
137 // references to the child to zero.
138 DCHECK_EQ(child_to_parent_refs_.count(child_id), 1u);
139 SurfaceIdSet& child_refs = child_to_parent_refs_[child_id];
140 DCHECK_EQ(child_refs.count(parent_id), 1u);
141 child_refs.erase(parent_id);
142
143 if (!child_refs.empty())
144 return;
145
146 // Remove any references the child holds before it gets garbage collected.
147 // Copy SurfaceIdSet to avoid iterator invalidation problems.
148 SurfaceIdSet child_child_refs = parent_to_child_refs_[child_id];
149 for (auto& child_child_id : child_child_refs)
150 RemoveSurfaceReference(child_id, child_child_id, false);
151 DCHECK_EQ(GetSurfaceRefereeCount(child_id), 0u);
152
153 // For recursive calls to RemoveSurfaceIdReference() only run GC at very end.
154 if (should_run_gc)
155 GarbageCollectSurfaces();
156 }
157
158 size_t SurfaceManager::GetSurfaceReferenceCount(
159 const SurfaceId& surface_id) const {
160 auto iter = child_to_parent_refs_.find(surface_id);
161 if (iter == child_to_parent_refs_.end())
162 return 0;
163 return iter->second.size();
164 }
165
166 size_t SurfaceManager::GetSurfaceRefereeCount(
167 const SurfaceId& surface_id) const {
168 auto iter = parent_to_child_refs_.find(surface_id);
169 if (iter == parent_to_child_refs_.end())
170 return 0;
171 return iter->second.size();
172 }
173
90 void SurfaceManager::GarbageCollectSurfaces() { 174 void SurfaceManager::GarbageCollectSurfaces() {
91 // Simple mark and sweep GC. 175 // Simple mark and sweep GC.
92 // TODO(jbauman): Reduce the amount of work when nothing needs to be 176 // TODO(jbauman): Reduce the amount of work when nothing needs to be
93 // destroyed. 177 // destroyed.
94 std::vector<SurfaceId> live_surfaces; 178 std::vector<SurfaceId> live_surfaces;
95 std::set<SurfaceId> live_surfaces_set; 179 std::unordered_set<SurfaceId, SurfaceIdHash> live_surfaces_set;
96 180
97 // GC roots are surfaces that have not been destroyed, or have not had all 181 // GC roots are surfaces that have not been destroyed, or have not had all
98 // their destruction dependencies satisfied. 182 // their destruction dependencies satisfied.
99 for (auto& map_entry : surface_map_) { 183 for (auto& map_entry : surface_map_) {
100 map_entry.second->SatisfyDestructionDependencies(&satisfied_sequences_, 184 const SurfaceId& surface_id = map_entry.first;
101 &valid_frame_sink_ids_); 185 Surface* surface = map_entry.second;
102 if (!map_entry.second->destroyed() || 186 surface->SatisfyDestructionDependencies(&satisfied_sequences_,
103 map_entry.second->GetDestructionDependencyCount()) { 187 &valid_frame_sink_ids_);
104 live_surfaces_set.insert(map_entry.first); 188
105 live_surfaces.push_back(map_entry.first); 189 // Never use both sequences and references for the same Surface.
190 DCHECK(surface->GetDestructionDependencyCount() == 0 ||
191 GetSurfaceReferenceCount(surface_id) == 0);
192
193 if (!surface->destroyed() || surface->GetDestructionDependencyCount() > 0 ||
194 GetSurfaceReferenceCount(surface_id) > 0) {
195 live_surfaces_set.insert(surface_id);
196 live_surfaces.push_back(surface_id);
106 } 197 }
107 } 198 }
108 199
109 // Mark all surfaces reachable from live surfaces by adding them to 200 // Mark all surfaces reachable from live surfaces by adding them to
110 // live_surfaces and live_surfaces_set. 201 // live_surfaces and live_surfaces_set.
111 for (size_t i = 0; i < live_surfaces.size(); i++) { 202 for (size_t i = 0; i < live_surfaces.size(); i++) {
112 Surface* surf = surface_map_[live_surfaces[i]]; 203 Surface* surf = surface_map_[live_surfaces[i]];
113 DCHECK(surf); 204 DCHECK(surf);
114 205
115 for (const SurfaceId& id : surf->referenced_surfaces()) { 206 for (const SurfaceId& id : surf->referenced_surfaces()) {
116 if (live_surfaces_set.count(id)) 207 if (live_surfaces_set.count(id))
117 continue; 208 continue;
118 209
119 Surface* surf2 = GetSurfaceForId(id); 210 Surface* surf2 = GetSurfaceForId(id);
120 if (surf2) { 211 if (surf2) {
121 live_surfaces.push_back(id); 212 live_surfaces.push_back(id);
122 live_surfaces_set.insert(id); 213 live_surfaces_set.insert(id);
123 } 214 }
124 } 215 }
125 } 216 }
126 217
127 std::vector<std::unique_ptr<Surface>> to_destroy; 218 std::vector<std::unique_ptr<Surface>> to_destroy;
128 219
129 // Destroy all remaining unreachable surfaces. 220 // Destroy all remaining unreachable surfaces.
130 for (SurfaceDestroyList::iterator dest_it = surfaces_to_destroy_.begin(); 221 for (auto iter = surfaces_to_destroy_.begin();
131 dest_it != surfaces_to_destroy_.end();) { 222 iter != surfaces_to_destroy_.end();) {
132 if (!live_surfaces_set.count((*dest_it)->surface_id())) { 223 SurfaceId surface_id = (*iter)->surface_id();
133 std::unique_ptr<Surface> surf(std::move(*dest_it)); 224 if (!live_surfaces_set.count(surface_id)) {
134 DeregisterSurface(surf->surface_id()); 225 DeregisterSurface(surface_id);
135 dest_it = surfaces_to_destroy_.erase(dest_it); 226 to_destroy.push_back(std::move(*iter));
136 to_destroy.push_back(std::move(surf)); 227 iter = surfaces_to_destroy_.erase(iter);
137 } else { 228 } else {
138 ++dest_it; 229 ++iter;
139 } 230 }
140 } 231 }
141 232
142 to_destroy.clear(); 233 to_destroy.clear();
143 } 234 }
144 235
145 void SurfaceManager::RegisterSurfaceFactoryClient( 236 void SurfaceManager::RegisterSurfaceFactoryClient(
146 const FrameSinkId& frame_sink_id, 237 const FrameSinkId& frame_sink_id,
147 SurfaceFactoryClient* client) { 238 SurfaceFactoryClient* client) {
148 DCHECK(client); 239 DCHECK(client);
149 DCHECK(!frame_sink_source_map_[frame_sink_id].client);
150 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u); 240 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u);
151 241
152 auto iter = frame_sink_source_map_.find(frame_sink_id); 242 // Will create a new FrameSinkSourceMapping for |frame_sink_id| if necessary.
153 if (iter == frame_sink_source_map_.end()) { 243 FrameSinkSourceMapping& frame_sink_source =
154 auto insert_result = frame_sink_source_map_.insert( 244 frame_sink_source_map_[frame_sink_id];
155 std::make_pair(frame_sink_id, FrameSinkSourceMapping())); 245 DCHECK(!frame_sink_source.client);
156 DCHECK(insert_result.second); 246 frame_sink_source.client = client;
157 iter = insert_result.first;
158 }
159 iter->second.client = client;
160 247
161 // Propagate any previously set sources to the new client. 248 // Propagate any previously set sources to the new client.
162 if (iter->second.source) 249 if (frame_sink_source.source)
163 client->SetBeginFrameSource(iter->second.source); 250 client->SetBeginFrameSource(frame_sink_source.source);
164 } 251 }
165 252
166 void SurfaceManager::UnregisterSurfaceFactoryClient( 253 void SurfaceManager::UnregisterSurfaceFactoryClient(
167 const FrameSinkId& frame_sink_id) { 254 const FrameSinkId& frame_sink_id) {
168 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u); 255 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u);
169 DCHECK_EQ(frame_sink_source_map_.count(frame_sink_id), 1u); 256 DCHECK_EQ(frame_sink_source_map_.count(frame_sink_id), 1u);
170 257
171 auto iter = frame_sink_source_map_.find(frame_sink_id); 258 auto iter = frame_sink_source_map_.find(frame_sink_id);
172 if (iter->second.source) 259 if (iter->second.source)
173 iter->second.client->SetBeginFrameSource(nullptr); 260 iter->second.client->SetBeginFrameSource(nullptr);
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 // TODO(enne): these walks could be done in one step. 418 // TODO(enne): these walks could be done in one step.
332 RecursivelyDetachBeginFrameSource(child_frame_sink_id, parent_source); 419 RecursivelyDetachBeginFrameSource(child_frame_sink_id, parent_source);
333 for (auto source_iter : registered_sources_) 420 for (auto source_iter : registered_sources_)
334 RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first); 421 RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first);
335 } 422 }
336 423
337 Surface* SurfaceManager::GetSurfaceForId(const SurfaceId& surface_id) { 424 Surface* SurfaceManager::GetSurfaceForId(const SurfaceId& surface_id) {
338 DCHECK(thread_checker_.CalledOnValidThread()); 425 DCHECK(thread_checker_.CalledOnValidThread());
339 SurfaceMap::iterator it = surface_map_.find(surface_id); 426 SurfaceMap::iterator it = surface_map_.find(surface_id);
340 if (it == surface_map_.end()) 427 if (it == surface_map_.end())
341 return NULL; 428 return nullptr;
342 return it->second; 429 return it->second;
343 } 430 }
344 431
345 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id) { 432 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id) {
346 CHECK(thread_checker_.CalledOnValidThread()); 433 CHECK(thread_checker_.CalledOnValidThread());
347 bool changed = false; 434 bool changed = false;
348 for (auto& observer : observer_list_) 435 for (auto& observer : observer_list_)
349 observer.OnSurfaceDamaged(surface_id, &changed); 436 observer.OnSurfaceDamaged(surface_id, &changed);
350 return changed; 437 return changed;
351 } 438 }
352 439
353 void SurfaceManager::SurfaceCreated(const SurfaceId& surface_id, 440 void SurfaceManager::SurfaceCreated(const SurfaceId& surface_id,
354 const gfx::Size& frame_size, 441 const gfx::Size& frame_size,
355 float device_scale_factor) { 442 float device_scale_factor) {
356 CHECK(thread_checker_.CalledOnValidThread()); 443 CHECK(thread_checker_.CalledOnValidThread());
357 for (auto& observer : observer_list_) 444 for (auto& observer : observer_list_)
358 observer.OnSurfaceCreated(surface_id, frame_size, device_scale_factor); 445 observer.OnSurfaceCreated(surface_id, frame_size, device_scale_factor);
359 } 446 }
360 447
361 } // namespace cc 448 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698