Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: cc/surfaces/surface_manager.cc

Issue 2638833002: Moving temporary reference logic to SurfaceManager (Closed)
Patch Set: c Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 valid_frame_sink_ids_.erase(frame_sink_id); 117 valid_frame_sink_ids_.erase(frame_sink_id);
104 GarbageCollectSurfaces(); 118 GarbageCollectSurfaces();
105 } 119 }
106 120
107 const SurfaceId& SurfaceManager::GetRootSurfaceId() const { 121 const SurfaceId& SurfaceManager::GetRootSurfaceId() const {
108 return root_surface_id_; 122 return root_surface_id_;
109 } 123 }
110 124
111 void SurfaceManager::AddSurfaceReference(const SurfaceId& parent_id, 125 void SurfaceManager::AddSurfaceReference(const SurfaceId& parent_id,
112 const SurfaceId& child_id) { 126 const SurfaceId& child_id) {
127 auto vector_iter = temp_references_.find(child_id.frame_sink_id());
128
129 // If there are no temporary references for the FrameSinkId then we can just
130 // add reference and return.
131 if (vector_iter == temp_references_.end()) {
132 AddSurfaceReferenceImpl(parent_id, child_id);
133 return;
134 }
135
136 // Get the vector<LocalFrameId> for the appropriate FrameSinkId and look for
137 // |child_id.local_frame_id| in that vector. If found, there is a temporary
138 // reference to |child_id|.
139 std::vector<LocalFrameId>& refs = vector_iter->second;
140 auto temp_ref_iter =
141 std::find(refs.begin(), refs.end(), child_id.local_frame_id());
142
143 if (temp_ref_iter == refs.end()) {
144 AddSurfaceReferenceImpl(parent_id, child_id);
145 return;
146 }
147
148 // All surfaces get a temporary reference to the top level root. If the parent
149 // wants to add a reference to the top level root then we do nothing.
150 // Otherwise remove the temporary reference and add the reference.
151 if (parent_id != GetRootSurfaceId()) {
152 AddSurfaceReferenceImpl(parent_id, child_id);
153 RemoveSurfaceReference(GetRootSurfaceId(), child_id);
154 }
155
156 // Remove temporary references for surfaces with the same FrameSinkId that
157 // were created before |child_id|. The earlier surfaces were never embedded in
158 // the parent and the parent is embedding a later surface, so we know the
159 // parent doesn't need them anymore.
160 for (auto iter = refs.begin(); iter != temp_ref_iter; ++iter) {
161 SurfaceId id = SurfaceId(child_id.frame_sink_id(), *iter);
162 RemoveSurfaceReference(GetRootSurfaceId(), id);
163 }
164
165 // Remove markers for temporary references up to |child_id|, as the temporary
166 // references they correspond to were removed above. If |temp_ref_iter| points
167 // at the last element in |refs| then we are removing all temporary references
168 // for the FrameSinkId and can remove the map entry entirely.
169 if (++temp_ref_iter == refs.end())
170 temp_references_.erase(child_id.frame_sink_id());
171 else
172 refs.erase(refs.begin(), temp_ref_iter);
173 }
174
175 void SurfaceManager::AddSurfaceReferenceImpl(const SurfaceId& parent_id,
176 const SurfaceId& child_id) {
113 DCHECK(thread_checker_.CalledOnValidThread()); 177 DCHECK(thread_checker_.CalledOnValidThread());
kylechar 2017/01/17 15:04:19 The DCHECK + if statements should go on the public
Saman Sami 2017/01/17 15:32:24 Done.
114 178
115 // Check some conditions that should never happen. We don't want to crash on 179 // Check some conditions that should never happen. We don't want to crash on
116 // bad input from a compromised client so just return early. 180 // bad input from a compromised client so just return early.
117 if (parent_id == child_id) { 181 if (parent_id == child_id) {
118 LOG(ERROR) << "Cannot add self reference for " << parent_id.ToString(); 182 LOG(ERROR) << "Cannot add self reference for " << parent_id.ToString();
119 return; 183 return;
120 } 184 }
121 if (parent_id != root_surface_id_ && surface_map_.count(parent_id) == 0) { 185 if (parent_id != root_surface_id_ && surface_map_.count(parent_id) == 0) {
122 LOG(ERROR) << "No surface in map for " << parent_id.ToString(); 186 LOG(ERROR) << "No surface in map for " << parent_id.ToString();
123 return; 187 return;
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id) { 561 bool SurfaceManager::SurfaceModified(const SurfaceId& surface_id) {
498 CHECK(thread_checker_.CalledOnValidThread()); 562 CHECK(thread_checker_.CalledOnValidThread());
499 bool changed = false; 563 bool changed = false;
500 for (auto& observer : observer_list_) 564 for (auto& observer : observer_list_)
501 observer.OnSurfaceDamaged(surface_id, &changed); 565 observer.OnSurfaceDamaged(surface_id, &changed);
502 return changed; 566 return changed;
503 } 567 }
504 568
505 void SurfaceManager::SurfaceCreated(const SurfaceInfo& surface_info) { 569 void SurfaceManager::SurfaceCreated(const SurfaceInfo& surface_info) {
506 CHECK(thread_checker_.CalledOnValidThread()); 570 CHECK(thread_checker_.CalledOnValidThread());
571
572 if (lifetime_type_ == LifetimeType::REFERENCES) {
573 AddSurfaceReferenceImpl(GetRootSurfaceId(), surface_info.id());
574 temp_references_[surface_info.id().frame_sink_id()].push_back(
575 surface_info.id().local_frame_id());
576 }
577
507 for (auto& observer : observer_list_) 578 for (auto& observer : observer_list_)
508 observer.OnSurfaceCreated(surface_info); 579 observer.OnSurfaceCreated(surface_info);
509 } 580 }
510 581
511 } // namespace cc 582 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698