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
3 // found in the LICENSE file.
5 #include "cc/surfaces/surface_manager.h"
7 #include "base/logging.h"
8 #include "cc/surfaces/surface.h"
9 #include "cc/surfaces/surface_id_allocator.h"
13 SurfaceManager::SurfaceManager() {
14 thread_checker_
.DetachFromThread();
17 SurfaceManager::~SurfaceManager() {
18 DCHECK(thread_checker_
.CalledOnValidThread());
19 for (SurfaceDestroyList::iterator it
= surfaces_to_destroy_
.begin();
20 it
!= surfaces_to_destroy_
.end();
22 DeregisterSurface((*it
)->surface_id());
27 void SurfaceManager::RegisterSurface(Surface
* surface
) {
28 DCHECK(thread_checker_
.CalledOnValidThread());
30 DCHECK(!surface_map_
.count(surface
->surface_id()));
31 surface_map_
[surface
->surface_id()] = surface
;
34 void SurfaceManager::DeregisterSurface(SurfaceId surface_id
) {
35 DCHECK(thread_checker_
.CalledOnValidThread());
36 SurfaceMap::iterator it
= surface_map_
.find(surface_id
);
37 DCHECK(it
!= surface_map_
.end());
38 surface_map_
.erase(it
);
41 void SurfaceManager::Destroy(scoped_ptr
<Surface
> surface
) {
42 DCHECK(thread_checker_
.CalledOnValidThread());
43 surfaces_to_destroy_
.push_back(surface
.release());
44 SearchForSatisfaction();
47 void SurfaceManager::DidSatisfySequences(uint32_t id_namespace
,
48 std::vector
<uint32_t>* sequence
) {
49 DCHECK(thread_checker_
.CalledOnValidThread());
50 for (std::vector
<uint32_t>::iterator it
= sequence
->begin();
51 it
!= sequence
->end();
53 satisfied_sequences_
.insert(SurfaceSequence(id_namespace
, *it
));
56 SearchForSatisfaction();
59 void SurfaceManager::RegisterSurfaceIdNamespace(uint32_t id_namespace
) {
60 bool inserted
= valid_surface_id_namespaces_
.insert(id_namespace
).second
;
64 void SurfaceManager::InvalidateSurfaceIdNamespace(uint32_t id_namespace
) {
65 valid_surface_id_namespaces_
.erase(id_namespace
);
66 SearchForSatisfaction();
69 void SurfaceManager::SearchForSatisfaction() {
70 for (SurfaceDestroyList::iterator dest_it
= surfaces_to_destroy_
.begin();
71 dest_it
!= surfaces_to_destroy_
.end();) {
72 (*dest_it
)->SatisfyDestructionDependencies(&satisfied_sequences_
,
73 &valid_surface_id_namespaces_
);
74 if (!(*dest_it
)->GetDestructionDependencyCount()) {
75 scoped_ptr
<Surface
> surf(*dest_it
);
76 DeregisterSurface(surf
->surface_id());
77 dest_it
= surfaces_to_destroy_
.erase(dest_it
);
84 Surface
* SurfaceManager::GetSurfaceForId(SurfaceId surface_id
) {
85 DCHECK(thread_checker_
.CalledOnValidThread());
86 SurfaceMap::iterator it
= surface_map_
.find(surface_id
);
87 if (it
== surface_map_
.end())
92 bool SurfaceManager::SurfaceModified(SurfaceId surface_id
) {
93 DCHECK(thread_checker_
.CalledOnValidThread());
95 FOR_EACH_OBSERVER(SurfaceDamageObserver
, observer_list_
,
96 OnSurfaceDamaged(surface_id
, &changed
));