This sets up API to release OutputSurface from LTHClient.
[chromium-blink-merge.git] / cc / surfaces / surface_manager.cc
blob9ced46e41bad430f2cbb8607f397926a45eddad8
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"
11 namespace cc {
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();
21 ++it) {
22 DeregisterSurface((*it)->surface_id());
23 delete *it;
27 void SurfaceManager::RegisterSurface(Surface* surface) {
28 DCHECK(thread_checker_.CalledOnValidThread());
29 DCHECK(surface);
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 surface->set_destroyed(true);
44 surfaces_to_destroy_.push_back(surface.release());
45 GarbageCollectSurfaces();
48 void SurfaceManager::DidSatisfySequences(uint32_t id_namespace,
49 std::vector<uint32_t>* sequence) {
50 DCHECK(thread_checker_.CalledOnValidThread());
51 for (std::vector<uint32_t>::iterator it = sequence->begin();
52 it != sequence->end();
53 ++it) {
54 satisfied_sequences_.insert(SurfaceSequence(id_namespace, *it));
56 sequence->clear();
57 GarbageCollectSurfaces();
60 void SurfaceManager::RegisterSurfaceIdNamespace(uint32_t id_namespace) {
61 bool inserted = valid_surface_id_namespaces_.insert(id_namespace).second;
62 DCHECK(inserted);
65 void SurfaceManager::InvalidateSurfaceIdNamespace(uint32_t id_namespace) {
66 valid_surface_id_namespaces_.erase(id_namespace);
67 GarbageCollectSurfaces();
70 void SurfaceManager::GarbageCollectSurfaces() {
71 // Simple mark and sweep GC.
72 // TODO(jbauman): Reduce the amount of work when nothing needs to be
73 // destroyed.
74 std::vector<SurfaceId> live_surfaces;
75 std::set<SurfaceId> live_surfaces_set;
77 // GC roots are surfaces that have not been destroyed, or have not had all
78 // their destruction dependencies satisfied.
79 for (auto& map_entry : surface_map_) {
80 map_entry.second->SatisfyDestructionDependencies(
81 &satisfied_sequences_, &valid_surface_id_namespaces_);
82 if (!map_entry.second->destroyed() ||
83 map_entry.second->GetDestructionDependencyCount()) {
84 live_surfaces_set.insert(map_entry.first);
85 live_surfaces.push_back(map_entry.first);
89 // Mark all surfaces reachable from live surfaces by adding them to
90 // live_surfaces and live_surfaces_set.
91 for (size_t i = 0; i < live_surfaces.size(); i++) {
92 Surface* surf = surface_map_[live_surfaces[i]];
93 DCHECK(surf);
95 for (SurfaceId id : surf->referenced_surfaces()) {
96 if (live_surfaces_set.count(id))
97 continue;
99 Surface* surf2 = GetSurfaceForId(id);
100 if (surf2) {
101 live_surfaces.push_back(id);
102 live_surfaces_set.insert(id);
107 // Destroy all remaining unreachable surfaces.
108 for (SurfaceDestroyList::iterator dest_it = surfaces_to_destroy_.begin();
109 dest_it != surfaces_to_destroy_.end();) {
110 if (!live_surfaces_set.count((*dest_it)->surface_id())) {
111 scoped_ptr<Surface> surf(*dest_it);
112 DeregisterSurface(surf->surface_id());
113 dest_it = surfaces_to_destroy_.erase(dest_it);
114 } else {
115 ++dest_it;
120 Surface* SurfaceManager::GetSurfaceForId(SurfaceId surface_id) {
121 DCHECK(thread_checker_.CalledOnValidThread());
122 SurfaceMap::iterator it = surface_map_.find(surface_id);
123 if (it == surface_map_.end())
124 return NULL;
125 return it->second;
128 bool SurfaceManager::SurfaceModified(SurfaceId surface_id) {
129 DCHECK(thread_checker_.CalledOnValidThread());
130 bool changed = false;
131 FOR_EACH_OBSERVER(SurfaceDamageObserver, observer_list_,
132 OnSurfaceDamaged(surface_id, &changed));
133 return changed;
136 } // namespace cc