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

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

Issue 2455663003: Add cc::Surface ref counting. (Closed)
Patch Set: Address 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
« no previous file with comments | « cc/surfaces/surface_manager.h ('k') | cc/surfaces/surface_manager_ref_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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) {
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 DCHECK(thread_checker_.CalledOnValidThread());
120
121 // Check if we have the reference that is requested to be removed. We don't
122 // want to crash on bad input from a compromised client so just return early.
123 if (parent_to_child_refs_.count(parent_id) == 0 ||
124 parent_to_child_refs_[parent_id].count(child_id) == 0) {
125 LOG(ERROR) << "No reference from " << parent_id.ToString() << " to "
126 << child_id.ToString();
127 return;
128 }
129
130 RemoveSurfaceReferenceImpl(parent_id, child_id);
131 GarbageCollectSurfaces();
132 }
133
134 size_t SurfaceManager::GetSurfaceReferenceCount(
135 const SurfaceId& surface_id) const {
136 auto iter = child_to_parent_refs_.find(surface_id);
137 if (iter == child_to_parent_refs_.end())
138 return 0;
139 return iter->second.size();
140 }
141
142 size_t SurfaceManager::GetReferencedSurfaceCount(
143 const SurfaceId& surface_id) const {
144 auto iter = parent_to_child_refs_.find(surface_id);
145 if (iter == parent_to_child_refs_.end())
146 return 0;
147 return iter->second.size();
148 }
149
90 void SurfaceManager::GarbageCollectSurfaces() { 150 void SurfaceManager::GarbageCollectSurfaces() {
91 // Simple mark and sweep GC. 151 // Simple mark and sweep GC.
92 // TODO(jbauman): Reduce the amount of work when nothing needs to be 152 // TODO(jbauman): Reduce the amount of work when nothing needs to be
93 // destroyed. 153 // destroyed.
94 std::vector<SurfaceId> live_surfaces; 154 std::vector<SurfaceId> live_surfaces;
95 std::set<SurfaceId> live_surfaces_set; 155 std::unordered_set<SurfaceId, SurfaceIdHash> live_surfaces_set;
96 156
97 // GC roots are surfaces that have not been destroyed, or have not had all 157 // GC roots are surfaces that have not been destroyed, or have not had all
98 // their destruction dependencies satisfied. 158 // their destruction dependencies satisfied.
99 for (auto& map_entry : surface_map_) { 159 for (auto& map_entry : surface_map_) {
100 map_entry.second->SatisfyDestructionDependencies(&satisfied_sequences_, 160 const SurfaceId& surface_id = map_entry.first;
101 &valid_frame_sink_ids_); 161 Surface* surface = map_entry.second;
102 if (!map_entry.second->destroyed() || 162 surface->SatisfyDestructionDependencies(&satisfied_sequences_,
103 map_entry.second->GetDestructionDependencyCount()) { 163 &valid_frame_sink_ids_);
104 live_surfaces_set.insert(map_entry.first); 164
105 live_surfaces.push_back(map_entry.first); 165 // Never use both sequences and references for the same Surface.
166 DCHECK(surface->GetDestructionDependencyCount() == 0 ||
167 GetSurfaceReferenceCount(surface_id) == 0);
168
169 if (!surface->destroyed() || surface->GetDestructionDependencyCount() > 0 ||
170 GetSurfaceReferenceCount(surface_id) > 0) {
171 live_surfaces_set.insert(surface_id);
172 live_surfaces.push_back(surface_id);
106 } 173 }
107 } 174 }
108 175
109 // Mark all surfaces reachable from live surfaces by adding them to 176 // Mark all surfaces reachable from live surfaces by adding them to
110 // live_surfaces and live_surfaces_set. 177 // live_surfaces and live_surfaces_set.
111 for (size_t i = 0; i < live_surfaces.size(); i++) { 178 for (size_t i = 0; i < live_surfaces.size(); i++) {
112 Surface* surf = surface_map_[live_surfaces[i]]; 179 Surface* surf = surface_map_[live_surfaces[i]];
113 DCHECK(surf); 180 DCHECK(surf);
114 181
115 for (const SurfaceId& id : surf->referenced_surfaces()) { 182 for (const SurfaceId& id : surf->referenced_surfaces()) {
116 if (live_surfaces_set.count(id)) 183 if (live_surfaces_set.count(id))
117 continue; 184 continue;
118 185
119 Surface* surf2 = GetSurfaceForId(id); 186 Surface* surf2 = GetSurfaceForId(id);
120 if (surf2) { 187 if (surf2) {
121 live_surfaces.push_back(id); 188 live_surfaces.push_back(id);
122 live_surfaces_set.insert(id); 189 live_surfaces_set.insert(id);
123 } 190 }
124 } 191 }
125 } 192 }
126 193
127 std::vector<std::unique_ptr<Surface>> to_destroy; 194 std::vector<std::unique_ptr<Surface>> to_destroy;
128 195
129 // Destroy all remaining unreachable surfaces. 196 // Destroy all remaining unreachable surfaces.
130 for (SurfaceDestroyList::iterator dest_it = surfaces_to_destroy_.begin(); 197 for (auto iter = surfaces_to_destroy_.begin();
131 dest_it != surfaces_to_destroy_.end();) { 198 iter != surfaces_to_destroy_.end();) {
132 if (!live_surfaces_set.count((*dest_it)->surface_id())) { 199 SurfaceId surface_id = (*iter)->surface_id();
133 std::unique_ptr<Surface> surf(std::move(*dest_it)); 200 if (!live_surfaces_set.count(surface_id)) {
134 DeregisterSurface(surf->surface_id()); 201 DeregisterSurface(surface_id);
135 dest_it = surfaces_to_destroy_.erase(dest_it); 202 to_destroy.push_back(std::move(*iter));
136 to_destroy.push_back(std::move(surf)); 203 iter = surfaces_to_destroy_.erase(iter);
137 } else { 204 } else {
138 ++dest_it; 205 ++iter;
139 } 206 }
140 } 207 }
141 208
142 to_destroy.clear(); 209 to_destroy.clear();
143 } 210 }
144 211
212 void SurfaceManager::RemoveSurfaceReferenceImpl(const SurfaceId& parent_id,
213 const SurfaceId& child_id) {
214 // Remove the reference from parent to child. This doesn't change anything
215 // about the validity of the parent.
216 parent_to_child_refs_[parent_id].erase(child_id);
217
218 // Remove the reference from child to parent. This might drop the number of
219 // references to the child to zero.
220 DCHECK_EQ(child_to_parent_refs_.count(child_id), 1u);
221 SurfaceIdSet& child_refs = child_to_parent_refs_[child_id];
222 DCHECK_EQ(child_refs.count(parent_id), 1u);
223 child_refs.erase(parent_id);
224
225 if (!child_refs.empty())
226 return;
227
228 // Remove any references the child holds before it gets garbage collected.
229 // Copy SurfaceIdSet to avoid iterator invalidation problems.
230 SurfaceIdSet child_child_refs = parent_to_child_refs_[child_id];
231 for (auto& child_child_id : child_child_refs)
232 RemoveSurfaceReferenceImpl(child_id, child_child_id);
233 DCHECK_EQ(GetReferencedSurfaceCount(child_id), 0u);
234 }
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
« no previous file with comments | « cc/surfaces/surface_manager.h ('k') | cc/surfaces/surface_manager_ref_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698