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 "components/view_manager/server_view.h" | |
6 | |
7 #include <inttypes.h> | |
8 | |
9 #include "base/strings/stringprintf.h" | |
10 #include "components/view_manager/server_view_delegate.h" | |
11 #include "components/view_manager/server_view_observer.h" | |
12 #include "components/view_manager/surfaces/surfaces_state.h" | |
13 #include "mojo/converters/geometry/geometry_type_converters.h" | |
14 #include "mojo/converters/surfaces/surfaces_type_converters.h" | |
15 | |
16 namespace view_manager { | |
17 | |
18 namespace { | |
19 | |
20 void CallCallback(const mojo::Closure& callback, cc::SurfaceDrawStatus status) { | |
21 callback.Run(); | |
22 } | |
23 | |
24 } // namespace | |
25 | |
26 ServerView::ServerView(ServerViewDelegate* delegate, const ViewId& id) | |
27 : delegate_(delegate), | |
28 id_(id), | |
29 parent_(nullptr), | |
30 visible_(false), | |
31 opacity_(1), | |
32 pending_access_policy_(mojo::ViewTree::ACCESS_POLICY_DEFAULT), | |
33 // Don't notify newly added observers during notification. This causes | |
34 // problems for code that adds an observer as part of an observer | |
35 // notification (such as ServerViewDrawTracker). | |
36 observers_(base::ObserverList<ServerViewObserver>::NOTIFY_EXISTING_ONLY), | |
37 binding_(this) { | |
38 DCHECK(delegate); // Must provide a delegate. | |
39 } | |
40 | |
41 ServerView::~ServerView() { | |
42 FOR_EACH_OBSERVER(ServerViewObserver, observers_, OnWillDestroyView(this)); | |
43 | |
44 while (!children_.empty()) | |
45 children_.front()->parent()->Remove(children_.front()); | |
46 | |
47 if (parent_) | |
48 parent_->Remove(this); | |
49 | |
50 FOR_EACH_OBSERVER(ServerViewObserver, observers_, OnViewDestroyed(this)); | |
51 | |
52 // SurfaceFactory's destructor will attempt to return resources which will | |
53 // call back into here and access |client_| so we should destroy | |
54 // |surface_factory_| early on. | |
55 surface_factory_.reset(); | |
56 } | |
57 | |
58 void ServerView::AddObserver(ServerViewObserver* observer) { | |
59 observers_.AddObserver(observer); | |
60 } | |
61 | |
62 void ServerView::RemoveObserver(ServerViewObserver* observer) { | |
63 observers_.RemoveObserver(observer); | |
64 } | |
65 | |
66 void ServerView::Bind(mojo::InterfaceRequest<Surface> request, | |
67 mojo::SurfaceClientPtr client) { | |
68 if (binding_.is_bound()) { | |
69 if (surface_factory_) { | |
70 // Destroy frame surfaces submitted by the old client before replacing | |
71 // client_, so those surfaces will be returned to the old client. | |
72 surface_factory_->DestroyAll(); | |
73 SetSurfaceId(cc::SurfaceId()); | |
74 } | |
75 binding_.Close(); | |
76 client_ = nullptr; | |
77 } | |
78 binding_.Bind(request.Pass()); | |
79 client_ = client.Pass(); | |
80 } | |
81 | |
82 void ServerView::Add(ServerView* child) { | |
83 // We assume validation checks happened already. | |
84 DCHECK(child); | |
85 DCHECK(child != this); | |
86 DCHECK(!child->Contains(this)); | |
87 if (child->parent() == this) { | |
88 if (children_.size() == 1) | |
89 return; // Already in the right position. | |
90 Reorder(child, children_.back(), mojo::ORDER_DIRECTION_ABOVE); | |
91 return; | |
92 } | |
93 | |
94 ServerView* old_parent = child->parent(); | |
95 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_, | |
96 OnWillChangeViewHierarchy(child, this, old_parent)); | |
97 | |
98 if (child->parent()) | |
99 child->parent()->RemoveImpl(child); | |
100 | |
101 child->parent_ = this; | |
102 children_.push_back(child); | |
103 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_, | |
104 OnViewHierarchyChanged(child, this, old_parent)); | |
105 } | |
106 | |
107 void ServerView::Remove(ServerView* child) { | |
108 // We assume validation checks happened else where. | |
109 DCHECK(child); | |
110 DCHECK(child != this); | |
111 DCHECK(child->parent() == this); | |
112 | |
113 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_, | |
114 OnWillChangeViewHierarchy(child, nullptr, this)); | |
115 RemoveImpl(child); | |
116 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_, | |
117 OnViewHierarchyChanged(child, nullptr, this)); | |
118 } | |
119 | |
120 void ServerView::Reorder(ServerView* child, | |
121 ServerView* relative, | |
122 mojo::OrderDirection direction) { | |
123 // We assume validation checks happened else where. | |
124 DCHECK(child); | |
125 DCHECK(child->parent() == this); | |
126 DCHECK_GT(children_.size(), 1u); | |
127 children_.erase(std::find(children_.begin(), children_.end(), child)); | |
128 Views::iterator i = std::find(children_.begin(), children_.end(), relative); | |
129 if (direction == mojo::ORDER_DIRECTION_ABOVE) { | |
130 DCHECK(i != children_.end()); | |
131 children_.insert(++i, child); | |
132 } else if (direction == mojo::ORDER_DIRECTION_BELOW) { | |
133 DCHECK(i != children_.end()); | |
134 children_.insert(i, child); | |
135 } | |
136 FOR_EACH_OBSERVER(ServerViewObserver, observers_, | |
137 OnViewReordered(this, relative, direction)); | |
138 } | |
139 | |
140 void ServerView::SetBounds(const gfx::Rect& bounds) { | |
141 if (bounds_ == bounds) | |
142 return; | |
143 | |
144 const gfx::Rect old_bounds = bounds_; | |
145 bounds_ = bounds; | |
146 FOR_EACH_OBSERVER(ServerViewObserver, observers_, | |
147 OnViewBoundsChanged(this, old_bounds, bounds)); | |
148 } | |
149 | |
150 const ServerView* ServerView::GetRoot() const { | |
151 return delegate_->GetRootView(this); | |
152 } | |
153 | |
154 std::vector<const ServerView*> ServerView::GetChildren() const { | |
155 std::vector<const ServerView*> children; | |
156 children.reserve(children_.size()); | |
157 for (size_t i = 0; i < children_.size(); ++i) | |
158 children.push_back(children_[i]); | |
159 return children; | |
160 } | |
161 | |
162 std::vector<ServerView*> ServerView::GetChildren() { | |
163 // TODO(sky): rename to children() and fix return type. | |
164 return children_; | |
165 } | |
166 | |
167 bool ServerView::Contains(const ServerView* view) const { | |
168 for (const ServerView* parent = view; parent; parent = parent->parent_) { | |
169 if (parent == this) | |
170 return true; | |
171 } | |
172 return false; | |
173 } | |
174 | |
175 void ServerView::SetVisible(bool value) { | |
176 if (visible_ == value) | |
177 return; | |
178 | |
179 FOR_EACH_OBSERVER(ServerViewObserver, observers_, | |
180 OnWillChangeViewVisibility(this)); | |
181 visible_ = value; | |
182 FOR_EACH_OBSERVER(ServerViewObserver, observers_, | |
183 OnViewVisibilityChanged(this)); | |
184 } | |
185 | |
186 void ServerView::SetOpacity(float value) { | |
187 if (value == opacity_) | |
188 return; | |
189 opacity_ = value; | |
190 delegate_->OnScheduleViewPaint(this); | |
191 } | |
192 | |
193 void ServerView::SetTransform(const gfx::Transform& transform) { | |
194 if (transform_ == transform) | |
195 return; | |
196 | |
197 transform_ = transform; | |
198 delegate_->OnScheduleViewPaint(this); | |
199 } | |
200 | |
201 void ServerView::SetProperty(const std::string& name, | |
202 const std::vector<uint8_t>* value) { | |
203 auto it = properties_.find(name); | |
204 if (it != properties_.end()) { | |
205 if (value && it->second == *value) | |
206 return; | |
207 } else if (!value) { | |
208 // This property isn't set in |properties_| and |value| is NULL, so there's | |
209 // no change. | |
210 return; | |
211 } | |
212 | |
213 if (value) { | |
214 properties_[name] = *value; | |
215 } else if (it != properties_.end()) { | |
216 properties_.erase(it); | |
217 } | |
218 | |
219 FOR_EACH_OBSERVER(ServerViewObserver, observers_, | |
220 OnViewSharedPropertyChanged(this, name, value)); | |
221 } | |
222 | |
223 void ServerView::SetTextInputState(const ui::TextInputState& state) { | |
224 const bool changed = !(text_input_state_ == state); | |
225 if (changed) { | |
226 text_input_state_ = state; | |
227 // keyboard even if the state is not changed. So we have to notify | |
228 // |observers_|. | |
229 FOR_EACH_OBSERVER(ServerViewObserver, observers_, | |
230 OnViewTextInputStateChanged(this, state)); | |
231 } | |
232 } | |
233 | |
234 bool ServerView::IsDrawn() const { | |
235 const ServerView* root = delegate_->GetRootView(this); | |
236 if (!root || !root->visible()) | |
237 return false; | |
238 const ServerView* view = this; | |
239 while (view && view != root && view->visible()) | |
240 view = view->parent(); | |
241 return root == view; | |
242 } | |
243 | |
244 void ServerView::SetSurfaceId(cc::SurfaceId surface_id) { | |
245 surface_id_ = surface_id; | |
246 delegate_->OnScheduleViewPaint(this); | |
247 } | |
248 | |
249 void ServerView::SubmitCompositorFrame( | |
250 mojo::CompositorFramePtr frame, | |
251 const SubmitCompositorFrameCallback& callback) { | |
252 gfx::Size frame_size = frame->passes[0]->output_rect.To<gfx::Rect>().size(); | |
253 // Create Surfaces state on demand. | |
254 if (!surface_factory_) { | |
255 surface_factory_.reset( | |
256 new cc::SurfaceFactory(delegate_->GetSurfacesState()->manager(), this)); | |
257 } | |
258 if (!surface_id_allocator_) { | |
259 surface_id_allocator_.reset( | |
260 new cc::SurfaceIdAllocator( | |
261 delegate_->GetSurfacesState()->next_id_namespace())); | |
262 } | |
263 if (surface_id().is_null()) { | |
264 // Create a Surface ID for the first time for this view. | |
265 cc::SurfaceId surface_id(surface_id_allocator_->GenerateId()); | |
266 surface_factory_->Create(surface_id); | |
267 SetSurfaceId(surface_id); | |
268 } else { | |
269 // If the size of the CompostiorFrame has changed then destroy the existing | |
270 // Surface and create a new one of the appropriate size. | |
271 if (frame_size != last_submitted_frame_size()) { | |
272 surface_factory_->Destroy(surface_id()); | |
273 cc::SurfaceId surface_id(surface_id_allocator_->GenerateId()); | |
274 surface_factory_->Create(surface_id); | |
275 SetSurfaceId(surface_id); | |
276 } | |
277 } | |
278 surface_factory_->SubmitCompositorFrame( | |
279 surface_id(), | |
280 delegate_->UpdateViewTreeFromCompositorFrame(frame), | |
281 base::Bind(&CallCallback, callback)); | |
282 delegate_->GetSurfacesState()->scheduler()->SetNeedsDraw(); | |
283 last_submitted_frame_size_ = frame_size; | |
284 } | |
285 | |
286 #if !defined(NDEBUG) | |
287 std::string ServerView::GetDebugWindowHierarchy() const { | |
288 std::string result; | |
289 BuildDebugInfo(std::string(), &result); | |
290 return result; | |
291 } | |
292 | |
293 void ServerView::BuildDebugInfo(const std::string& depth, | |
294 std::string* result) const { | |
295 *result += base::StringPrintf( | |
296 "%sid=%d,%d visible=%s bounds=%d,%d %dx%d surface_id=%" PRIu64 "\n", | |
297 depth.c_str(), static_cast<int>(id_.connection_id), | |
298 static_cast<int>(id_.view_id), visible_ ? "true" : "false", bounds_.x(), | |
299 bounds_.y(), bounds_.width(), bounds_.height(), surface_id_.id); | |
300 for (const ServerView* child : children_) | |
301 child->BuildDebugInfo(depth + " ", result); | |
302 } | |
303 #endif | |
304 | |
305 void ServerView::ReturnResources( | |
306 const cc::ReturnedResourceArray& resources) { | |
307 if (!client_) | |
308 return; | |
309 client_->ReturnResources( | |
310 mojo::Array<mojo::ReturnedResourcePtr>::From(resources)); | |
311 } | |
312 | |
313 void ServerView::RemoveImpl(ServerView* view) { | |
314 view->parent_ = NULL; | |
315 children_.erase(std::find(children_.begin(), children_.end(), view)); | |
316 } | |
317 | |
318 } // namespace view_manager | |
OLD | NEW |