OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "cc/surfaces/framesink_manager.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "cc/surfaces/surface_factory_client.h" |
| 12 |
| 13 #if DCHECK_IS_ON() |
| 14 #include <sstream> |
| 15 #endif |
| 16 |
| 17 namespace cc { |
| 18 |
| 19 FrameSinkManager::FrameSinkSourceMapping::FrameSinkSourceMapping() |
| 20 : source(nullptr) {} |
| 21 |
| 22 FrameSinkManager::FrameSinkSourceMapping::FrameSinkSourceMapping( |
| 23 const FrameSinkSourceMapping& other) = default; |
| 24 |
| 25 FrameSinkManager::FrameSinkSourceMapping::~FrameSinkSourceMapping() { |
| 26 } |
| 27 |
| 28 FrameSinkManager::FrameSinkManager() {} |
| 29 |
| 30 FrameSinkManager::~FrameSinkManager() { |
| 31 // All surface factory clients should be unregistered prior to SurfaceManager |
| 32 // destruction. |
| 33 DCHECK_EQ(clients_.size(), 0u); |
| 34 DCHECK_EQ(registered_sources_.size(), 0u); |
| 35 } |
| 36 |
| 37 void FrameSinkManager::RegisterFrameSinkId(const FrameSinkId& frame_sink_id) { |
| 38 bool inserted = valid_frame_sink_ids_.insert(frame_sink_id).second; |
| 39 DCHECK(inserted); |
| 40 } |
| 41 |
| 42 void FrameSinkManager::InvalidateFrameSinkId(const FrameSinkId& frame_sink_id) { |
| 43 valid_frame_sink_ids_.erase(frame_sink_id); |
| 44 } |
| 45 |
| 46 void FrameSinkManager::RegisterSurfaceFactoryClient( |
| 47 const FrameSinkId& frame_sink_id, |
| 48 SurfaceFactoryClient* client) { |
| 49 DCHECK(client); |
| 50 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u); |
| 51 |
| 52 clients_[frame_sink_id] = client; |
| 53 |
| 54 auto it = frame_sink_source_map_.find(frame_sink_id); |
| 55 if (it != frame_sink_source_map_.end()) { |
| 56 if (it->second.source) |
| 57 client->SetBeginFrameSource(it->second.source); |
| 58 } |
| 59 } |
| 60 |
| 61 void FrameSinkManager::UnregisterSurfaceFactoryClient( |
| 62 const FrameSinkId& frame_sink_id) { |
| 63 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u); |
| 64 auto client_iter = clients_.find(frame_sink_id); |
| 65 DCHECK(client_iter != clients_.end()); |
| 66 |
| 67 auto source_iter = frame_sink_source_map_.find(frame_sink_id); |
| 68 if (source_iter != frame_sink_source_map_.end()) { |
| 69 if (source_iter->second.source) |
| 70 client_iter->second->SetBeginFrameSource(nullptr); |
| 71 if (!source_iter->second.has_children()) |
| 72 frame_sink_source_map_.erase(source_iter); |
| 73 } |
| 74 clients_.erase(client_iter); |
| 75 } |
| 76 |
| 77 void FrameSinkManager::RegisterBeginFrameSource( |
| 78 BeginFrameSource* source, |
| 79 const FrameSinkId& frame_sink_id) { |
| 80 DCHECK(source); |
| 81 DCHECK_EQ(registered_sources_.count(source), 0u); |
| 82 DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u); |
| 83 |
| 84 registered_sources_[source] = frame_sink_id; |
| 85 RecursivelyAttachBeginFrameSource(frame_sink_id, source); |
| 86 } |
| 87 |
| 88 void FrameSinkManager::UnregisterBeginFrameSource(BeginFrameSource* source) { |
| 89 DCHECK(source); |
| 90 DCHECK_EQ(registered_sources_.count(source), 1u); |
| 91 |
| 92 FrameSinkId frame_sink_id = registered_sources_[source]; |
| 93 registered_sources_.erase(source); |
| 94 |
| 95 if (frame_sink_source_map_.count(frame_sink_id) == 0u) |
| 96 return; |
| 97 |
| 98 // TODO(enne): these walks could be done in one step. |
| 99 // Remove this begin frame source from its subtree. |
| 100 RecursivelyDetachBeginFrameSource(frame_sink_id, source); |
| 101 // Then flush every remaining registered source to fix any sources that |
| 102 // became null because of the previous step but that have an alternative. |
| 103 for (auto source_iter : registered_sources_) |
| 104 RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first); |
| 105 } |
| 106 |
| 107 void FrameSinkManager::RecursivelyAttachBeginFrameSource( |
| 108 const FrameSinkId& frame_sink_id, |
| 109 BeginFrameSource* source) { |
| 110 FrameSinkSourceMapping& mapping = frame_sink_source_map_[frame_sink_id]; |
| 111 if (!mapping.source) { |
| 112 mapping.source = source; |
| 113 auto client_iter = clients_.find(frame_sink_id); |
| 114 if (client_iter != clients_.end()) |
| 115 client_iter->second->SetBeginFrameSource(source); |
| 116 } |
| 117 for (size_t i = 0; i < mapping.children.size(); ++i) |
| 118 RecursivelyAttachBeginFrameSource(mapping.children[i], source); |
| 119 } |
| 120 |
| 121 void FrameSinkManager::RecursivelyDetachBeginFrameSource( |
| 122 const FrameSinkId& frame_sink_id, |
| 123 BeginFrameSource* source) { |
| 124 auto iter = frame_sink_source_map_.find(frame_sink_id); |
| 125 if (iter == frame_sink_source_map_.end()) |
| 126 return; |
| 127 if (iter->second.source == source) { |
| 128 iter->second.source = nullptr; |
| 129 auto client_iter = clients_.find(frame_sink_id); |
| 130 if (client_iter != clients_.end()) |
| 131 client_iter->second->SetBeginFrameSource(nullptr); |
| 132 } |
| 133 |
| 134 if (!iter->second.has_children() && !clients_.count(frame_sink_id)) { |
| 135 frame_sink_source_map_.erase(iter); |
| 136 return; |
| 137 } |
| 138 |
| 139 std::vector<FrameSinkId>& children = iter->second.children; |
| 140 for (size_t i = 0; i < children.size(); ++i) { |
| 141 RecursivelyDetachBeginFrameSource(children[i], source); |
| 142 } |
| 143 } |
| 144 |
| 145 bool FrameSinkManager::ChildContains( |
| 146 const FrameSinkId& child_frame_sink_id, |
| 147 const FrameSinkId& search_frame_sink_id) const { |
| 148 auto iter = frame_sink_source_map_.find(child_frame_sink_id); |
| 149 if (iter == frame_sink_source_map_.end()) |
| 150 return false; |
| 151 |
| 152 const std::vector<FrameSinkId>& children = iter->second.children; |
| 153 for (size_t i = 0; i < children.size(); ++i) { |
| 154 if (children[i] == search_frame_sink_id) |
| 155 return true; |
| 156 if (ChildContains(children[i], search_frame_sink_id)) |
| 157 return true; |
| 158 } |
| 159 return false; |
| 160 } |
| 161 |
| 162 void FrameSinkManager::RegisterFrameSinkHierarchy( |
| 163 const FrameSinkId& parent_frame_sink_id, |
| 164 const FrameSinkId& child_frame_sink_id) { |
| 165 // If it's possible to reach the parent through the child's descendant chain, |
| 166 // then this will create an infinite loop. Might as well just crash here. |
| 167 CHECK(!ChildContains(child_frame_sink_id, parent_frame_sink_id)); |
| 168 |
| 169 std::vector<FrameSinkId>& children = |
| 170 frame_sink_source_map_[parent_frame_sink_id].children; |
| 171 for (size_t i = 0; i < children.size(); ++i) |
| 172 DCHECK(children[i] != child_frame_sink_id); |
| 173 children.push_back(child_frame_sink_id); |
| 174 |
| 175 // If the parent has no source, then attaching it to this child will |
| 176 // not change any downstream sources. |
| 177 BeginFrameSource* parent_source = |
| 178 frame_sink_source_map_[parent_frame_sink_id].source; |
| 179 if (!parent_source) |
| 180 return; |
| 181 |
| 182 DCHECK_EQ(registered_sources_.count(parent_source), 1u); |
| 183 RecursivelyAttachBeginFrameSource(child_frame_sink_id, parent_source); |
| 184 } |
| 185 |
| 186 void FrameSinkManager::UnregisterFrameSinkHierarchy( |
| 187 const FrameSinkId& parent_frame_sink_id, |
| 188 const FrameSinkId& child_frame_sink_id) { |
| 189 // Deliberately do not check validity of either parent or child FrameSinkId |
| 190 // here. They were valid during the registration, so were valid at some |
| 191 // point in time. This makes it possible to invalidate parent and child |
| 192 // FrameSinkIds independently of each other and not have an ordering |
| 193 // dependency of unregistering the hierarchy first before either of them. |
| 194 DCHECK_EQ(frame_sink_source_map_.count(parent_frame_sink_id), 1u); |
| 195 |
| 196 auto iter = frame_sink_source_map_.find(parent_frame_sink_id); |
| 197 |
| 198 std::vector<FrameSinkId>& children = iter->second.children; |
| 199 bool found_child = false; |
| 200 for (size_t i = 0; i < children.size(); ++i) { |
| 201 if (children[i] == child_frame_sink_id) { |
| 202 found_child = true; |
| 203 children[i] = children.back(); |
| 204 children.resize(children.size() - 1); |
| 205 break; |
| 206 } |
| 207 } |
| 208 DCHECK(found_child); |
| 209 |
| 210 // The SurfaceFactoryClient and hierarchy can be registered/unregistered |
| 211 // in either order, so empty frame_sink_source_map entries need to be |
| 212 // checked when removing either clients or relationships. |
| 213 if (!iter->second.has_children() && !clients_.count(parent_frame_sink_id) && |
| 214 !iter->second.source) { |
| 215 frame_sink_source_map_.erase(iter); |
| 216 return; |
| 217 } |
| 218 |
| 219 // If the parent does not have a begin frame source, then disconnecting it |
| 220 // will not change any of its children. |
| 221 BeginFrameSource* parent_source = iter->second.source; |
| 222 if (!parent_source) |
| 223 return; |
| 224 |
| 225 // TODO(enne): these walks could be done in one step. |
| 226 RecursivelyDetachBeginFrameSource(child_frame_sink_id, parent_source); |
| 227 for (auto source_iter : registered_sources_) |
| 228 RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first); |
| 229 } |
| 230 |
| 231 } // namespace cc |
OLD | NEW |