1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/surfaces/surface_manager.h"
#include <stddef.h>
#include <stdint.h>
#include "base/logging.h"
#include "cc/surfaces/surface.h"
#include "cc/surfaces/surface_factory_client.h"
#include "cc/surfaces/surface_id_allocator.h"
namespace cc {
SurfaceManager::ClientSourceMapping::ClientSourceMapping()
: client(nullptr), source(nullptr) {}
SurfaceManager::ClientSourceMapping::ClientSourceMapping(
const ClientSourceMapping& other) = default;
SurfaceManager::ClientSourceMapping::~ClientSourceMapping() {
DCHECK(is_empty()) << "client: " << client
<< ", children: " << children.size();
}
SurfaceManager::SurfaceManager() {
thread_checker_.DetachFromThread();
}
SurfaceManager::~SurfaceManager() {
DCHECK(thread_checker_.CalledOnValidThread());
for (SurfaceDestroyList::iterator it = surfaces_to_destroy_.begin();
it != surfaces_to_destroy_.end();
++it) {
DeregisterSurface((*it)->surface_id());
delete *it;
}
// All hierarchies, sources, and surface factory clients should be
// unregistered prior to SurfaceManager destruction.
DCHECK_EQ(namespace_client_map_.size(), 0u);
DCHECK_EQ(registered_sources_.size(), 0u);
}
void SurfaceManager::RegisterSurface(Surface* surface) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(surface);
DCHECK(!surface_map_.count(surface->surface_id()));
surface_map_[surface->surface_id()] = surface;
}
void SurfaceManager::DeregisterSurface(SurfaceId surface_id) {
DCHECK(thread_checker_.CalledOnValidThread());
SurfaceMap::iterator it = surface_map_.find(surface_id);
DCHECK(it != surface_map_.end());
surface_map_.erase(it);
}
void SurfaceManager::Destroy(scoped_ptr<Surface> surface) {
DCHECK(thread_checker_.CalledOnValidThread());
surface->set_destroyed(true);
surfaces_to_destroy_.push_back(surface.release());
GarbageCollectSurfaces();
}
void SurfaceManager::DidSatisfySequences(uint32_t id_namespace,
std::vector<uint32_t>* sequence) {
DCHECK(thread_checker_.CalledOnValidThread());
for (std::vector<uint32_t>::iterator it = sequence->begin();
it != sequence->end();
++it) {
satisfied_sequences_.insert(SurfaceSequence(id_namespace, *it));
}
sequence->clear();
GarbageCollectSurfaces();
}
void SurfaceManager::RegisterSurfaceIdNamespace(uint32_t id_namespace) {
bool inserted = valid_surface_id_namespaces_.insert(id_namespace).second;
DCHECK(inserted);
}
void SurfaceManager::InvalidateSurfaceIdNamespace(uint32_t id_namespace) {
valid_surface_id_namespaces_.erase(id_namespace);
GarbageCollectSurfaces();
}
void SurfaceManager::GarbageCollectSurfaces() {
// Simple mark and sweep GC.
// TODO(jbauman): Reduce the amount of work when nothing needs to be
// destroyed.
std::vector<SurfaceId> live_surfaces;
std::set<SurfaceId> live_surfaces_set;
// GC roots are surfaces that have not been destroyed, or have not had all
// their destruction dependencies satisfied.
for (auto& map_entry : surface_map_) {
map_entry.second->SatisfyDestructionDependencies(
&satisfied_sequences_, &valid_surface_id_namespaces_);
if (!map_entry.second->destroyed() ||
map_entry.second->GetDestructionDependencyCount()) {
live_surfaces_set.insert(map_entry.first);
live_surfaces.push_back(map_entry.first);
}
}
// Mark all surfaces reachable from live surfaces by adding them to
// live_surfaces and live_surfaces_set.
for (size_t i = 0; i < live_surfaces.size(); i++) {
Surface* surf = surface_map_[live_surfaces[i]];
DCHECK(surf);
for (SurfaceId id : surf->referenced_surfaces()) {
if (live_surfaces_set.count(id))
continue;
Surface* surf2 = GetSurfaceForId(id);
if (surf2) {
live_surfaces.push_back(id);
live_surfaces_set.insert(id);
}
}
}
// Destroy all remaining unreachable surfaces.
for (SurfaceDestroyList::iterator dest_it = surfaces_to_destroy_.begin();
dest_it != surfaces_to_destroy_.end();) {
if (!live_surfaces_set.count((*dest_it)->surface_id())) {
scoped_ptr<Surface> surf(*dest_it);
DeregisterSurface(surf->surface_id());
dest_it = surfaces_to_destroy_.erase(dest_it);
} else {
++dest_it;
}
}
}
void SurfaceManager::RegisterSurfaceFactoryClient(
uint32_t id_namespace,
SurfaceFactoryClient* client) {
DCHECK(client);
DCHECK(!namespace_client_map_[id_namespace].client);
DCHECK_EQ(valid_surface_id_namespaces_.count(id_namespace), 1u);
auto iter = namespace_client_map_.find(id_namespace);
if (iter == namespace_client_map_.end()) {
auto insert_result = namespace_client_map_.insert(
std::make_pair(id_namespace, ClientSourceMapping()));
DCHECK(insert_result.second);
iter = insert_result.first;
}
iter->second.client = client;
// Propagate any previously set sources to the new client.
if (iter->second.source)
client->SetBeginFrameSource(iter->second.source);
}
void SurfaceManager::UnregisterSurfaceFactoryClient(uint32_t id_namespace) {
DCHECK_EQ(valid_surface_id_namespaces_.count(id_namespace), 1u);
DCHECK_EQ(namespace_client_map_.count(id_namespace), 1u);
auto iter = namespace_client_map_.find(id_namespace);
if (iter->second.source)
iter->second.client->SetBeginFrameSource(nullptr);
iter->second.client = nullptr;
// The SurfaceFactoryClient and hierarchy can be registered/unregistered
// in either order, so empty namespace_client_map entries need to be
// checked when removing either clients or relationships.
if (iter->second.is_empty())
namespace_client_map_.erase(iter);
}
void SurfaceManager::RegisterBeginFrameSource(BeginFrameSource* source,
uint32_t id_namespace) {
DCHECK(source);
DCHECK_EQ(registered_sources_.count(source), 0u);
DCHECK_EQ(valid_surface_id_namespaces_.count(id_namespace), 1u);
registered_sources_[source] = id_namespace;
RecursivelyAttachBeginFrameSource(id_namespace, source);
}
void SurfaceManager::UnregisterBeginFrameSource(BeginFrameSource* source) {
DCHECK(source);
DCHECK_EQ(registered_sources_.count(source), 1u);
uint32_t id_namespace = registered_sources_[source];
registered_sources_.erase(source);
if (namespace_client_map_.count(id_namespace) == 0u)
return;
// TODO(enne): these walks could be done in one step.
// Remove this begin frame source from its subtree.
RecursivelyDetachBeginFrameSource(id_namespace, source);
// Then flush every remaining registered source to fix any sources that
// became null because of the previous step but that have an alternative.
for (auto source_iter : registered_sources_)
RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first);
}
void SurfaceManager::RecursivelyAttachBeginFrameSource(
uint32_t id_namespace,
BeginFrameSource* source) {
ClientSourceMapping& mapping = namespace_client_map_[id_namespace];
if (!mapping.source) {
mapping.source = source;
if (mapping.client)
mapping.client->SetBeginFrameSource(source);
}
for (size_t i = 0; i < mapping.children.size(); ++i)
RecursivelyAttachBeginFrameSource(mapping.children[i], source);
}
void SurfaceManager::RecursivelyDetachBeginFrameSource(
uint32_t id_namespace,
BeginFrameSource* source) {
auto iter = namespace_client_map_.find(id_namespace);
if (iter == namespace_client_map_.end())
return;
if (iter->second.source == source) {
iter->second.source = nullptr;
if (iter->second.client)
iter->second.client->SetBeginFrameSource(nullptr);
}
if (iter->second.is_empty()) {
namespace_client_map_.erase(iter);
return;
}
std::vector<uint32_t>& children = iter->second.children;
for (size_t i = 0; i < children.size(); ++i) {
RecursivelyDetachBeginFrameSource(children[i], source);
}
}
bool SurfaceManager::ChildContains(uint32_t child_namespace,
uint32_t search_namespace) const {
auto iter = namespace_client_map_.find(child_namespace);
if (iter == namespace_client_map_.end())
return false;
const std::vector<uint32_t>& children = iter->second.children;
for (size_t i = 0; i < children.size(); ++i) {
if (children[i] == search_namespace)
return true;
if (ChildContains(children[i], search_namespace))
return true;
}
return false;
}
void SurfaceManager::RegisterSurfaceNamespaceHierarchy(
uint32_t parent_namespace,
uint32_t child_namespace) {
DCHECK_EQ(valid_surface_id_namespaces_.count(parent_namespace), 1u);
DCHECK_EQ(valid_surface_id_namespaces_.count(child_namespace), 1u);
// If it's possible to reach the parent through the child's descendant chain,
// then this will create an infinite loop. Might as well just crash here.
CHECK(!ChildContains(child_namespace, parent_namespace));
std::vector<uint32_t>& children =
namespace_client_map_[parent_namespace].children;
for (size_t i = 0; i < children.size(); ++i)
DCHECK_NE(children[i], child_namespace);
children.push_back(child_namespace);
// If the parent has no source, then attaching it to this child will
// not change any downstream sources.
BeginFrameSource* parent_source =
namespace_client_map_[parent_namespace].source;
if (!parent_source)
return;
DCHECK_EQ(registered_sources_.count(parent_source), 1u);
RecursivelyAttachBeginFrameSource(child_namespace, parent_source);
}
void SurfaceManager::UnregisterSurfaceNamespaceHierarchy(
uint32_t parent_namespace,
uint32_t child_namespace) {
DCHECK_EQ(valid_surface_id_namespaces_.count(parent_namespace), 1u);
DCHECK_EQ(valid_surface_id_namespaces_.count(child_namespace), 1u);
DCHECK_EQ(namespace_client_map_.count(parent_namespace), 1u);
auto iter = namespace_client_map_.find(parent_namespace);
std::vector<uint32_t>& children = iter->second.children;
bool found_child = false;
for (size_t i = 0; i < children.size(); ++i) {
if (children[i] == child_namespace) {
found_child = true;
children[i] = children[children.size() - 1];
children.resize(children.size() - 1);
break;
}
}
DCHECK(found_child);
// The SurfaceFactoryClient and hierarchy can be registered/unregistered
// in either order, so empty namespace_client_map entries need to be
// checked when removing either clients or relationships.
if (iter->second.is_empty()) {
namespace_client_map_.erase(iter);
return;
}
// If the parent does not have a begin frame source, then disconnecting it
// will not change any of its children.
BeginFrameSource* parent_source = iter->second.source;
if (!parent_source)
return;
// TODO(enne): these walks could be done in one step.
RecursivelyDetachBeginFrameSource(child_namespace, parent_source);
for (auto source_iter : registered_sources_)
RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first);
}
Surface* SurfaceManager::GetSurfaceForId(SurfaceId surface_id) {
DCHECK(thread_checker_.CalledOnValidThread());
SurfaceMap::iterator it = surface_map_.find(surface_id);
if (it == surface_map_.end())
return NULL;
return it->second;
}
bool SurfaceManager::SurfaceModified(SurfaceId surface_id) {
CHECK(thread_checker_.CalledOnValidThread());
bool changed = false;
FOR_EACH_OBSERVER(SurfaceDamageObserver, observer_list_,
OnSurfaceDamaged(surface_id, &changed));
return changed;
}
} // namespace cc
|