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

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

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

Powered by Google App Engine
This is Rietveld 408576698