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

Side by Side Diff: services/ui/ws/frame_generator.cc

Issue 2489003002: Convert mustash use surface references. (Closed)
Patch Set: Rebase + fixes. Created 4 years, 1 month 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
« no previous file with comments | « services/ui/ws/frame_generator.h ('k') | services/ui/ws/platform_display.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "services/ui/ws/frame_generator.h" 5 #include "services/ui/ws/frame_generator.h"
6 6
7 #include "base/containers/adapters.h" 7 #include "base/containers/adapters.h"
8 #include "cc/output/compositor_frame.h" 8 #include "cc/output/compositor_frame.h"
9 #include "cc/quads/render_pass.h" 9 #include "cc/quads/render_pass.h"
10 #include "cc/quads/render_pass_draw_quad.h" 10 #include "cc/quads/render_pass_draw_quad.h"
11 #include "cc/quads/shared_quad_state.h" 11 #include "cc/quads/shared_quad_state.h"
12 #include "cc/quads/surface_draw_quad.h" 12 #include "cc/quads/surface_draw_quad.h"
13 #include "cc/surfaces/surface_id.h"
14 #include "gpu/ipc/client/gpu_channel_host.h" 13 #include "gpu/ipc/client/gpu_channel_host.h"
15 #include "services/ui/surfaces/display_compositor.h" 14 #include "services/ui/surfaces/display_compositor.h"
16 #include "services/ui/surfaces/surfaces_context_provider.h" 15 #include "services/ui/surfaces/surfaces_context_provider.h"
17 #include "services/ui/ws/frame_generator_delegate.h" 16 #include "services/ui/ws/frame_generator_delegate.h"
18 #include "services/ui/ws/server_window.h" 17 #include "services/ui/ws/server_window.h"
19 #include "services/ui/ws/server_window_compositor_frame_sink_manager.h" 18 #include "services/ui/ws/server_window_compositor_frame_sink_manager.h"
20 #include "services/ui/ws/server_window_delegate.h" 19 #include "services/ui/ws/server_window_delegate.h"
21 20
22 namespace ui { 21 namespace ui {
23 22
24 namespace ws { 23 namespace ws {
24 namespace {
25
26 // Walks up window tree and finds the SurfaceId for the ServerWindow embedding
27 // |window|. If |window| is a display root, then return |root_surface_id|.
28 cc::SurfaceId FindParentSurfaceId(ServerWindow* window,
29 const cc::SurfaceId& root_surface_id) {
30 if (!window->parent())
31 return root_surface_id;
32
33 ServerWindow* current = window;
34 while (current->parent()) {
35 current = current->parent();
36 if (current->compositor_frame_sink_manager() &&
37 current->compositor_frame_sink_manager()->HasCompositorFrameSinkOfType(
38 mojom::CompositorFrameSinkType::DEFAULT)) {
39 return current->compositor_frame_sink_manager()->GetLatestSurfaceId(
40 mojom::CompositorFrameSinkType::DEFAULT);
41 }
42 }
43
44 NOTREACHED();
45 return cc::SurfaceId();
46 }
47
48 } // namespace
25 49
26 FrameGenerator::FrameGenerator(FrameGeneratorDelegate* delegate, 50 FrameGenerator::FrameGenerator(FrameGeneratorDelegate* delegate,
27 ServerWindow* root_window) 51 ServerWindow* root_window)
28 : delegate_(delegate), 52 : delegate_(delegate),
29 frame_sink_id_(
30 WindowIdToTransportId(root_window->id()),
31 static_cast<uint32_t>(mojom::CompositorFrameSinkType::DEFAULT)),
32 root_window_(root_window), 53 root_window_(root_window),
54 root_surface_id_(cc::FrameSinkId(),
55 cc::LocalFrameId(0, base::UnguessableToken::Create())),
33 binding_(this), 56 binding_(this),
34 weak_factory_(this) { 57 weak_factory_(this) {
35 DCHECK(delegate_); 58 DCHECK(delegate_);
36 surface_sequence_generator_.set_frame_sink_id(frame_sink_id_);
37 } 59 }
38 60
39 FrameGenerator::~FrameGenerator() { 61 FrameGenerator::~FrameGenerator() {
40 ReleaseAllSurfaceReferences(); 62 RemoveDeadSurfaceReferences();
63 RemoveAllSurfaceReferences();
41 // Invalidate WeakPtrs now to avoid callbacks back into the 64 // Invalidate WeakPtrs now to avoid callbacks back into the
42 // FrameGenerator during destruction of |compositor_frame_sink_|. 65 // FrameGenerator during destruction of |compositor_frame_sink_|.
43 weak_factory_.InvalidateWeakPtrs(); 66 weak_factory_.InvalidateWeakPtrs();
44 compositor_frame_sink_.reset(); 67 compositor_frame_sink_.reset();
45 } 68 }
46 69
47 void FrameGenerator::OnGpuChannelEstablished( 70 void FrameGenerator::OnGpuChannelEstablished(
48 scoped_refptr<gpu::GpuChannelHost> channel) { 71 scoped_refptr<gpu::GpuChannelHost> channel) {
49 if (widget_ != gfx::kNullAcceleratedWidget) { 72 if (widget_ != gfx::kNullAcceleratedWidget) {
50 cc::mojom::MojoCompositorFrameSinkRequest request = 73 cc::mojom::MojoCompositorFrameSinkRequest request =
(...skipping 28 matching lines...) Expand all
79 new SurfacesContextProvider(widget_, std::move(gpu_channel_)), 102 new SurfacesContextProvider(widget_, std::move(gpu_channel_)),
80 std::move(request), binding_.CreateInterfacePtrAndBind()); 103 std::move(request), binding_.CreateInterfacePtrAndBind());
81 // TODO(fsamuel): This means we're always requesting a new BeginFrame signal 104 // TODO(fsamuel): This means we're always requesting a new BeginFrame signal
82 // even when we don't need it. Once surface ID propagation work is done, 105 // even when we don't need it. Once surface ID propagation work is done,
83 // this will not be necessary because FrameGenerator will only need a 106 // this will not be necessary because FrameGenerator will only need a
84 // BeginFrame if the window manager changes. 107 // BeginFrame if the window manager changes.
85 compositor_frame_sink_->SetNeedsBeginFrame(true); 108 compositor_frame_sink_->SetNeedsBeginFrame(true);
86 } 109 }
87 } 110 }
88 111
112 void FrameGenerator::AddSurfaceReference(const cc::SurfaceId& surface_id,
113 ServerWindow* window) {
114 DCHECK(surface_id.is_valid());
115
116 // TODO(kylechar): Adding surface references should be synchronized with
117 // CompositorFrameSink::SubmitCompositorFrame().
118 DisplayCompositor* display_compositor = GetDisplayCompositor();
119
120 auto it = active_references_.find(surface_id.frame_sink_id());
121 if (it == active_references_.end()) {
122 cc::SurfaceId parent_id = FindParentSurfaceId(window, root_surface_id_);
123
124 if (!window->parent())
125 display_compositor->AddRootSurfaceReference(surface_id);
126 else
127 display_compositor->AddSurfaceReference(parent_id, surface_id);
128
129 // Add new reference from parent to surface, plus add reference to local
130 // cache.
131 display_compositor->AddSurfaceReference(parent_id, surface_id);
Fady Samuel 2016/11/18 03:46:06 I'm confused about this line. Aren't you already d
kylechar 2016/11/18 17:24:10 Disregard this patch. It was more WIP than anythin
132 active_references_[surface_id.frame_sink_id()] =
133 SurfaceReference({parent_id, surface_id});
134
135 // Observe |window| so that we can remove references when it's destroyed.
136 Add(window);
137 return;
138 }
139
140 SurfaceReference& ref = it->second;
141 DCHECK(surface_id.frame_sink_id() == ref.child_id.frame_sink_id());
142
143 // Check if we're holding a reference to the surface and return early.
144 if (surface_id.local_frame_id() == ref.child_id.local_frame_id())
145 return;
146
147 // Add a reference from parent to new surface first.
148 display_compositor->AddSurfaceReference(ref.parent_id, surface_id);
149
150 // If the display root surface has changed, update all references to embedded
151 // surfaces.
152 if (!window->parent())
153 AddNewParentReferences(ref.child_id, surface_id);
154
155 // Move the existing reference to list of references to remove after we submit
156 // the next CompositorFrame and update local reference cache to be the new
157 // reference. If this is the display root surface then removing this reference
158 // will recursively remove any references it held.
159 dead_references_.push_back(ref);
160 ref.child_id = surface_id;
161 }
162
89 void FrameGenerator::DidReceiveCompositorFrameAck() {} 163 void FrameGenerator::DidReceiveCompositorFrameAck() {}
90 164
91 void FrameGenerator::OnBeginFrame(const cc::BeginFrameArgs& begin_frame_arags) { 165 void FrameGenerator::OnBeginFrame(const cc::BeginFrameArgs& begin_frame_arags) {
92 if (!root_window_->visible()) 166 if (!root_window_->visible())
93 return; 167 return;
94 168
95 // TODO(fsamuel): We should add a trace for generating a top level frame. 169 // TODO(fsamuel): We should add a trace for generating a top level frame.
96 cc::CompositorFrame frame(GenerateCompositorFrame(root_window_->bounds())); 170 cc::CompositorFrame frame(GenerateCompositorFrame(root_window_->bounds()));
97 if (compositor_frame_sink_) 171 if (compositor_frame_sink_) {
98 compositor_frame_sink_->SubmitCompositorFrame(std::move(frame)); 172 compositor_frame_sink_->SubmitCompositorFrame(std::move(frame));
173
174 // Remove dead references after we submit a frame. This has to happen after
175 // the frame is submitted otherwise we could end up deleting a surface that
176 // is still embedded in the last frame.
177 // TODO(kylechar): This should be synchronized with SubmitCompositorFrame().
178 RemoveDeadSurfaceReferences();
179 }
99 } 180 }
100 181
101 void FrameGenerator::ReclaimResources( 182 void FrameGenerator::ReclaimResources(
102 const cc::ReturnedResourceArray& resources) { 183 const cc::ReturnedResourceArray& resources) {
103 // Nothing to do here because FrameGenerator CompositorFrames don't reference 184 // Nothing to do here because FrameGenerator CompositorFrames don't reference
104 // any resources. 185 // any resources.
105 } 186 }
106 187
107 cc::CompositorFrame FrameGenerator::GenerateCompositorFrame( 188 cc::CompositorFrame FrameGenerator::GenerateCompositorFrame(
108 const gfx::Rect& output_rect) { 189 const gfx::Rect& output_rect) {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 const gfx::Rect bounds_at_origin(window->bounds().size()); 260 const gfx::Rect bounds_at_origin(window->bounds().size());
180 // TODO(fsamuel): These clipping and visible rects are incorrect. They need 261 // TODO(fsamuel): These clipping and visible rects are incorrect. They need
181 // to be populated from CompositorFrame structs. 262 // to be populated from CompositorFrame structs.
182 sqs->SetAll(quad_to_target_transform, 263 sqs->SetAll(quad_to_target_transform,
183 bounds_at_origin.size() /* layer_bounds */, 264 bounds_at_origin.size() /* layer_bounds */,
184 bounds_at_origin /* visible_layer_bounds */, 265 bounds_at_origin /* visible_layer_bounds */,
185 bounds_at_origin /* clip_rect */, false /* is_clipped */, 266 bounds_at_origin /* clip_rect */, false /* is_clipped */,
186 combined_opacity, SkXfermode::kSrcOver_Mode, 267 combined_opacity, SkXfermode::kSrcOver_Mode,
187 0 /* sorting-context_id */); 268 0 /* sorting-context_id */);
188 auto* quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); 269 auto* quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>();
189 AddOrUpdateSurfaceReference(mojom::CompositorFrameSinkType::DEFAULT,
190 window);
191 quad->SetAll(sqs, bounds_at_origin /* rect */, 270 quad->SetAll(sqs, bounds_at_origin /* rect */,
192 gfx::Rect() /* opaque_rect */, 271 gfx::Rect() /* opaque_rect */,
193 bounds_at_origin /* visible_rect */, true /* needs_blending*/, 272 bounds_at_origin /* visible_rect */, true /* needs_blending*/,
194 default_surface_id); 273 default_surface_id);
195 } 274 }
196 if (underlay_surface_id.is_valid()) { 275 if (underlay_surface_id.is_valid()) {
197 const gfx::Rect underlay_absolute_bounds = 276 const gfx::Rect underlay_absolute_bounds =
198 absolute_bounds - window->underlay_offset(); 277 absolute_bounds - window->underlay_offset();
199 gfx::Transform quad_to_target_transform; 278 gfx::Transform quad_to_target_transform;
200 quad_to_target_transform.Translate(underlay_absolute_bounds.x(), 279 quad_to_target_transform.Translate(underlay_absolute_bounds.x(),
201 underlay_absolute_bounds.y()); 280 underlay_absolute_bounds.y());
202 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); 281 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
203 const gfx::Rect bounds_at_origin( 282 const gfx::Rect bounds_at_origin(
204 window->compositor_frame_sink_manager()->GetLatestFrameSize( 283 window->compositor_frame_sink_manager()->GetLatestFrameSize(
205 mojom::CompositorFrameSinkType::UNDERLAY)); 284 mojom::CompositorFrameSinkType::UNDERLAY));
206 sqs->SetAll(quad_to_target_transform, 285 sqs->SetAll(quad_to_target_transform,
207 bounds_at_origin.size() /* layer_bounds */, 286 bounds_at_origin.size() /* layer_bounds */,
208 bounds_at_origin /* visible_layer_bounds */, 287 bounds_at_origin /* visible_layer_bounds */,
209 bounds_at_origin /* clip_rect */, false /* is_clipped */, 288 bounds_at_origin /* clip_rect */, false /* is_clipped */,
210 combined_opacity, SkXfermode::kSrcOver_Mode, 289 combined_opacity, SkXfermode::kSrcOver_Mode,
211 0 /* sorting-context_id */); 290 0 /* sorting-context_id */);
212 291
213 auto* quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); 292 auto* quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>();
214 AddOrUpdateSurfaceReference(mojom::CompositorFrameSinkType::UNDERLAY,
215 window);
216 quad->SetAll(sqs, bounds_at_origin /* rect */, 293 quad->SetAll(sqs, bounds_at_origin /* rect */,
217 gfx::Rect() /* opaque_rect */, 294 gfx::Rect() /* opaque_rect */,
218 bounds_at_origin /* visible_rect */, true /* needs_blending*/, 295 bounds_at_origin /* visible_rect */, true /* needs_blending*/,
219 underlay_surface_id); 296 underlay_surface_id);
220 } 297 }
221 } 298 }
222 299
223 void FrameGenerator::AddOrUpdateSurfaceReference( 300 void FrameGenerator::AddNewParentReferences(
224 mojom::CompositorFrameSinkType type, 301 const cc::SurfaceId& old_surface_id,
225 ServerWindow* window) { 302 const cc::SurfaceId& new_surface_id) {
226 cc::SurfaceId surface_id = 303 DCHECK(old_surface_id.frame_sink_id() == new_surface_id.frame_sink_id());
227 window->compositor_frame_sink_manager()->GetLatestSurfaceId(type); 304
228 if (!surface_id.is_valid()) 305 DisplayCompositor* display_compositor = GetDisplayCompositor();
229 return; 306 for (auto& map_entry : active_references_) {
230 auto it = dependencies_.find(surface_id.frame_sink_id()); 307 SurfaceReference& ref = map_entry.second;
231 if (it == dependencies_.end()) { 308 if (ref.parent_id == old_surface_id) {
232 SurfaceDependency dependency = { 309 display_compositor->AddSurfaceReference(new_surface_id, ref.child_id);
233 surface_id.local_frame_id(), 310 ref.parent_id = new_surface_id;
234 surface_sequence_generator_.CreateSurfaceSequence()}; 311 }
235 dependencies_[surface_id.frame_sink_id()] = dependency;
236 GetDisplayCompositor()->AddSurfaceReference(surface_id,
237 dependency.sequence);
238 // Observe |window_surface|'s window so that we can release references when
239 // the window is destroyed.
240 Add(window);
241 return;
242 } 312 }
313 }
243 314
244 // We are already holding a reference to this surface so there's no work to do 315 void FrameGenerator::RemoveDeadSurfaceReferences() {
245 // here. 316 if (dead_references_.empty())
246 if (surface_id.local_frame_id() == it->second.local_frame_id)
247 return; 317 return;
248 318
249 // If we have have an existing reference to a surface from the given 319 DisplayCompositor* display_compositor = GetDisplayCompositor();
250 // FrameSink, then we should release the reference, and then add this new 320 for (auto& ref : dead_references_) {
251 // reference. This results in a delete and lookup in the map but simplifies 321 if (ref.parent_id == root_surface_id_)
252 // the code. 322 display_compositor->RemoveRootSurfaceReference(ref.child_id);
253 ReleaseFrameSinkReference(surface_id.frame_sink_id()); 323 else
254 324 display_compositor->RemoveSurfaceReference(ref.parent_id, ref.child_id);
255 // This recursion will always terminate. This line is being called because 325 }
256 // there was a stale surface reference. The stale reference has been released 326 dead_references_.clear();
257 // in the previous line and cleared from the dependencies_ map. Thus, in the
258 // recursive call, we'll enter the second if blcok because the FrameSinkId
259 // is no longer referenced in the map.
260 AddOrUpdateSurfaceReference(type, window);
261 } 327 }
262 328
263 void FrameGenerator::ReleaseFrameSinkReference( 329 void FrameGenerator::RemoveFrameSinkReference(
264 const cc::FrameSinkId& frame_sink_id) { 330 const cc::FrameSinkId& frame_sink_id) {
265 auto it = dependencies_.find(frame_sink_id); 331 auto it = active_references_.find(frame_sink_id);
266 if (it == dependencies_.end()) 332 if (it == active_references_.end())
267 return; 333 return;
268 std::vector<uint32_t> sequences; 334 dead_references_.push_back(it->second);
269 sequences.push_back(it->second.sequence.sequence); 335 active_references_.erase(it);
270 GetDisplayCompositor()->ReturnSurfaceReferences(frame_sink_id, sequences);
271 dependencies_.erase(it);
272 } 336 }
273 337
274 void FrameGenerator::ReleaseAllSurfaceReferences() { 338 void FrameGenerator::RemoveAllSurfaceReferences() {
275 std::vector<uint32_t> sequences; 339 DisplayCompositor* display_compositor = GetDisplayCompositor();
276 for (auto& dependency : dependencies_) 340 for (auto& map_entry : active_references_) {
277 sequences.push_back(dependency.second.sequence.sequence); 341 const SurfaceReference& ref = map_entry.second;
278 GetDisplayCompositor()->ReturnSurfaceReferences(frame_sink_id_, sequences); 342 display_compositor->RemoveSurfaceReference(ref.parent_id, ref.child_id);
279 dependencies_.clear(); 343 }
344 active_references_.clear();
280 } 345 }
281 346
282 ui::DisplayCompositor* FrameGenerator::GetDisplayCompositor() { 347 ui::DisplayCompositor* FrameGenerator::GetDisplayCompositor() {
283 return root_window_->delegate()->GetDisplayCompositor(); 348 return root_window_->delegate()->GetDisplayCompositor();
284 } 349 }
285 350
286 void FrameGenerator::OnWindowDestroying(ServerWindow* window) { 351 void FrameGenerator::OnWindowDestroying(ServerWindow* window) {
287 Remove(window); 352 Remove(window);
288 ServerWindowCompositorFrameSinkManager* compositor_frame_sink_manager = 353 ServerWindowCompositorFrameSinkManager* compositor_frame_sink_manager =
289 window->compositor_frame_sink_manager(); 354 window->compositor_frame_sink_manager();
290 // If FrameGenerator was observing |window|, then that means it had a 355 // If FrameGenerator was observing |window|, then that means it had a
291 // CompositorFrame at some point in time and should have a 356 // CompositorFrame at some point in time and should have a
292 // ServerWindowCompositorFrameSinkManager. 357 // ServerWindowCompositorFrameSinkManager.
293 DCHECK(compositor_frame_sink_manager); 358 DCHECK(compositor_frame_sink_manager);
294 359
295 cc::SurfaceId default_surface_id = 360 cc::SurfaceId default_surface_id =
296 window->compositor_frame_sink_manager()->GetLatestSurfaceId( 361 window->compositor_frame_sink_manager()->GetLatestSurfaceId(
297 mojom::CompositorFrameSinkType::DEFAULT); 362 mojom::CompositorFrameSinkType::DEFAULT);
298 if (default_surface_id.is_valid()) 363 if (default_surface_id.is_valid())
299 ReleaseFrameSinkReference(default_surface_id.frame_sink_id()); 364 RemoveFrameSinkReference(default_surface_id.frame_sink_id());
300 365
301 cc::SurfaceId underlay_surface_id = 366 cc::SurfaceId underlay_surface_id =
302 window->compositor_frame_sink_manager()->GetLatestSurfaceId( 367 window->compositor_frame_sink_manager()->GetLatestSurfaceId(
303 mojom::CompositorFrameSinkType::UNDERLAY); 368 mojom::CompositorFrameSinkType::UNDERLAY);
304 if (underlay_surface_id.is_valid()) 369 if (underlay_surface_id.is_valid())
305 ReleaseFrameSinkReference(underlay_surface_id.frame_sink_id()); 370 RemoveFrameSinkReference(underlay_surface_id.frame_sink_id());
306 } 371 }
307 372
308 } // namespace ws 373 } // namespace ws
309 374
310 } // namespace ui 375 } // namespace ui
OLDNEW
« no previous file with comments | « services/ui/ws/frame_generator.h ('k') | services/ui/ws/platform_display.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698