Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <queue> | 10 #include <queue> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "cc/surfaces/direct_surface_reference_factory.h" | 14 #include "cc/surfaces/direct_surface_reference_factory.h" |
| 15 #include "cc/surfaces/surface.h" | 15 #include "cc/surfaces/surface.h" |
| 16 #include "cc/surfaces/surface_factory_client.h" | 16 #include "cc/surfaces/surface_factory_client.h" |
| 17 #include "cc/surfaces/surface_id_allocator.h" | 17 #include "cc/surfaces/surface_id_allocator.h" |
| 18 #include "cc/surfaces/surface_info.h" | |
| 18 | 19 |
| 19 namespace cc { | 20 namespace cc { |
| 20 | 21 |
| 21 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping() | 22 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping() |
| 22 : client(nullptr), source(nullptr) {} | 23 : client(nullptr), source(nullptr) {} |
| 23 | 24 |
| 24 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping( | 25 SurfaceManager::FrameSinkSourceMapping::FrameSinkSourceMapping( |
| 25 const FrameSinkSourceMapping& other) = default; | 26 const FrameSinkSourceMapping& other) = default; |
| 26 | 27 |
| 27 SurfaceManager::FrameSinkSourceMapping::~FrameSinkSourceMapping() { | 28 SurfaceManager::FrameSinkSourceMapping::~FrameSinkSourceMapping() { |
| 28 DCHECK(is_empty()) << "client: " << client | 29 DCHECK(is_empty()) << "client: " << client |
| 29 << ", children: " << children.size(); | 30 << ", children: " << children.size(); |
| 30 } | 31 } |
| 31 | 32 |
| 32 SurfaceManager::SurfaceManager(LifetimeType lifetime_type) | 33 SurfaceManager::SurfaceManager(LifetimeType lifetime_type) |
| 33 : lifetime_type_(lifetime_type), | 34 : lifetime_type_(lifetime_type), |
| 34 root_surface_id_(FrameSinkId(0u, 0u), | 35 root_surface_id_(FrameSinkId(0u, 0u), |
| 35 LocalFrameId(1u, base::UnguessableToken::Create())), | 36 LocalFrameId(1u, base::UnguessableToken::Create())), |
| 36 weak_factory_(this) { | 37 weak_factory_(this) { |
| 37 thread_checker_.DetachFromThread(); | 38 thread_checker_.DetachFromThread(); |
| 38 reference_factory_ = | 39 reference_factory_ = |
| 39 new DirectSurfaceReferenceFactory(weak_factory_.GetWeakPtr()); | 40 new DirectSurfaceReferenceFactory(weak_factory_.GetWeakPtr()); |
| 40 } | 41 } |
| 41 | 42 |
| 42 SurfaceManager::~SurfaceManager() { | 43 SurfaceManager::~SurfaceManager() { |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); | 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 | |
| 46 if (lifetime_type_ == LifetimeType::REFERENCES) { | |
| 47 // Remove all temporary references on shutdown. | |
| 48 for (auto& map_entry : temp_references_) { | |
| 49 const FrameSinkId& frame_sink_id = map_entry.first; | |
| 50 for (auto& local_frame_id : map_entry.second) { | |
| 51 RemoveSurfaceReferenceImpl(GetRootSurfaceId(), | |
| 52 SurfaceId(frame_sink_id, local_frame_id)); | |
| 53 } | |
| 54 } | |
| 55 GarbageCollectSurfaces(); | |
| 56 } | |
| 57 | |
| 44 for (SurfaceDestroyList::iterator it = surfaces_to_destroy_.begin(); | 58 for (SurfaceDestroyList::iterator it = surfaces_to_destroy_.begin(); |
| 45 it != surfaces_to_destroy_.end(); | 59 it != surfaces_to_destroy_.end(); |
| 46 ++it) { | 60 ++it) { |
| 47 DeregisterSurface((*it)->surface_id()); | 61 DeregisterSurface((*it)->surface_id()); |
| 48 } | 62 } |
| 49 surfaces_to_destroy_.clear(); | 63 surfaces_to_destroy_.clear(); |
| 50 | 64 |
| 51 // All hierarchies, sources, and surface factory clients should be | 65 // All hierarchies, sources, and surface factory clients should be |
| 52 // unregistered prior to SurfaceManager destruction. | 66 // unregistered prior to SurfaceManager destruction. |
| 53 DCHECK_EQ(frame_sink_source_map_.size(), 0u); | 67 DCHECK_EQ(frame_sink_source_map_.size(), 0u); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 } | 134 } |
| 121 if (parent_id != root_surface_id_ && surface_map_.count(parent_id) == 0) { | 135 if (parent_id != root_surface_id_ && surface_map_.count(parent_id) == 0) { |
| 122 LOG(ERROR) << "No surface in map for " << parent_id.ToString(); | 136 LOG(ERROR) << "No surface in map for " << parent_id.ToString(); |
| 123 return; | 137 return; |
| 124 } | 138 } |
| 125 if (surface_map_.count(child_id) == 0) { | 139 if (surface_map_.count(child_id) == 0) { |
| 126 LOG(ERROR) << "No surface in map for " << child_id.ToString(); | 140 LOG(ERROR) << "No surface in map for " << child_id.ToString(); |
| 127 return; | 141 return; |
| 128 } | 142 } |
| 129 | 143 |
| 144 auto vector_iter = temp_references_.find(child_id.frame_sink_id()); | |
| 145 | |
| 146 // If there are no temporary references for the FrameSinkId then we can just | |
| 147 // add reference and return. | |
| 148 if (vector_iter == temp_references_.end()) { | |
| 149 AddSurfaceReferenceImpl(parent_id, child_id); | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 // Get the vector<LocalFrameId> for the appropriate FrameSinkId and look for | |
| 154 // |child_id.local_frame_id| in that vector. If found, there is a temporary | |
| 155 // reference to |child_id|. | |
| 156 std::vector<LocalFrameId>& refs = vector_iter->second; | |
| 157 auto temp_ref_iter = | |
| 158 std::find(refs.begin(), refs.end(), child_id.local_frame_id()); | |
| 159 | |
| 160 if (temp_ref_iter == refs.end()) { | |
| 161 AddSurfaceReferenceImpl(parent_id, child_id); | |
| 162 return; | |
| 163 } | |
| 164 | |
| 165 // All surfaces get a temporary reference to the top level root. If the parent | |
| 166 // wants to add a reference to the top level root then we do nothing. | |
| 167 // Otherwise remove the temporary reference and add the reference. | |
| 168 if (parent_id != GetRootSurfaceId()) { | |
| 169 AddSurfaceReferenceImpl(parent_id, child_id); | |
| 170 RemoveSurfaceReference(GetRootSurfaceId(), child_id); | |
| 171 } | |
| 172 | |
| 173 // Remove temporary references for surfaces with the same FrameSinkId that | |
| 174 // were created before |child_id|. The earlier surfaces were never embedded in | |
| 175 // the parent and the parent is embedding a later surface, so we know the | |
| 176 // parent doesn't need them anymore. | |
| 177 for (auto iter = refs.begin(); iter != temp_ref_iter; ++iter) { | |
| 178 SurfaceId id = SurfaceId(child_id.frame_sink_id(), *iter); | |
| 179 RemoveSurfaceReference(GetRootSurfaceId(), id); | |
| 180 } | |
| 181 | |
| 182 // Remove markers for temporary references up to |child_id|, as the temporary | |
| 183 // references they correspond to were removed above. If |temp_ref_iter| points | |
| 184 // at the last element in |refs| then we are removing all temporary references | |
| 185 // for the FrameSinkId and can remove the map entry entirely. | |
| 186 if (++temp_ref_iter == refs.end()) | |
| 187 temp_references_.erase(child_id.frame_sink_id()); | |
| 188 else | |
| 189 refs.erase(refs.begin(), temp_ref_iter); | |
| 190 } | |
| 191 | |
| 192 void SurfaceManager::AddSurfaceReferenceImpl(const SurfaceId& parent_id, | |
| 193 const SurfaceId& child_id) { | |
| 194 DCHECK(thread_checker_.CalledOnValidThread()); | |
|
kylechar
2017/01/17 16:15:35
The private method doesn't also need a DCHECK(thr
Saman Sami
2017/01/17 18:29:05
Done.
| |
| 195 | |
| 130 parent_to_child_refs_[parent_id].insert(child_id); | 196 parent_to_child_refs_[parent_id].insert(child_id); |
| 131 child_to_parent_refs_[child_id].insert(parent_id); | 197 child_to_parent_refs_[child_id].insert(parent_id); |
| 132 } | 198 } |
| 133 | 199 |
| 134 void SurfaceManager::RemoveSurfaceReference(const SurfaceId& parent_id, | 200 void SurfaceManager::RemoveSurfaceReference(const SurfaceId& parent_id, |
| 135 const SurfaceId& child_id) { | 201 const SurfaceId& child_id) { |
| 136 DCHECK(thread_checker_.CalledOnValidThread()); | 202 DCHECK(thread_checker_.CalledOnValidThread()); |
| 137 | 203 |
| 138 // Check if we have the reference that is requested to be removed. We don't | 204 // Check if we have the reference that is requested to be removed. We don't |
| 139 // want to crash on bad input from a compromised client so just return early. | 205 // want to crash on bad input from a compromised client so just return early. |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 497 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id) { | 563 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id) { |
| 498 CHECK(thread_checker_.CalledOnValidThread()); | 564 CHECK(thread_checker_.CalledOnValidThread()); |
| 499 bool changed = false; | 565 bool changed = false; |
| 500 for (auto& observer : observer_list_) | 566 for (auto& observer : observer_list_) |
| 501 observer.OnSurfaceDamaged(surface_id, &changed); | 567 observer.OnSurfaceDamaged(surface_id, &changed); |
| 502 return changed; | 568 return changed; |
| 503 } | 569 } |
| 504 | 570 |
| 505 void SurfaceManager::SurfaceCreated(const SurfaceInfo& surface_info) { | 571 void SurfaceManager::SurfaceCreated(const SurfaceInfo& surface_info) { |
| 506 CHECK(thread_checker_.CalledOnValidThread()); | 572 CHECK(thread_checker_.CalledOnValidThread()); |
| 573 | |
| 574 if (lifetime_type_ == LifetimeType::REFERENCES) { | |
|
kylechar
2017/01/17 16:15:35
There was a comment that went with this code in Di
Saman Sami
2017/01/17 18:29:05
Done.
| |
| 575 AddSurfaceReferenceImpl(GetRootSurfaceId(), surface_info.id()); | |
| 576 temp_references_[surface_info.id().frame_sink_id()].push_back( | |
| 577 surface_info.id().local_frame_id()); | |
| 578 } | |
| 579 | |
| 507 for (auto& observer : observer_list_) | 580 for (auto& observer : observer_list_) |
| 508 observer.OnSurfaceCreated(surface_info); | 581 observer.OnSurfaceCreated(surface_info); |
| 509 } | 582 } |
| 510 | 583 |
| 511 } // namespace cc | 584 } // namespace cc |
| OLD | NEW |