| OLD | NEW |
| 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 <utility> | 7 #include <utility> |
| 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/containers/adapters.h" | 10 #include "base/containers/adapters.h" |
| 10 #include "cc/output/compositor_frame.h" | 11 #include "cc/output/compositor_frame.h" |
| 11 #include "cc/quads/render_pass.h" | 12 #include "cc/quads/render_pass.h" |
| 12 #include "cc/quads/render_pass_draw_quad.h" | 13 #include "cc/quads/render_pass_draw_quad.h" |
| 13 #include "cc/quads/shared_quad_state.h" | 14 #include "cc/quads/shared_quad_state.h" |
| 14 #include "cc/quads/surface_draw_quad.h" | 15 #include "cc/quads/surface_draw_quad.h" |
| 15 #include "services/ui/ws/frame_generator_delegate.h" | 16 #include "services/ui/ws/frame_generator_delegate.h" |
| 16 #include "services/ui/ws/server_window.h" | 17 #include "services/ui/ws/server_window.h" |
| 17 #include "services/ui/ws/server_window_compositor_frame_sink_manager.h" | 18 #include "services/ui/ws/server_window_compositor_frame_sink_manager.h" |
| 18 #include "services/ui/ws/server_window_delegate.h" | 19 #include "services/ui/ws/server_window_delegate.h" |
| 19 | 20 |
| 20 namespace ui { | 21 namespace ui { |
| 21 | 22 |
| 22 namespace ws { | 23 namespace ws { |
| 23 | 24 |
| 24 FrameGenerator::FrameGenerator(FrameGeneratorDelegate* delegate, | 25 FrameGenerator::FrameGenerator(FrameGeneratorDelegate* delegate, |
| 25 ServerWindow* root_window) | 26 ServerWindow* root_window) |
| 26 : delegate_(delegate), | 27 : delegate_(delegate), |
| 27 root_window_(root_window), | 28 root_window_(root_window), |
| 28 binding_(this), | 29 binding_(this), |
| 29 weak_factory_(this) { | 30 weak_factory_(this) { |
| 30 DCHECK(delegate_); | 31 DCHECK(delegate_); |
| 31 } | 32 } |
| 32 | 33 |
| 33 FrameGenerator::~FrameGenerator() { | 34 FrameGenerator::~FrameGenerator() { |
| 34 RemoveAllSurfaceReferences(); | 35 // Remove reference from top level root to the display root surface, if one |
| 36 // exists. This will make everything referenced by the display surface |
| 37 // unreachable so it can be garbage collected. |
| 38 if (surface_tracker_.HasValidSurfaceId()) { |
| 39 compositor_frame_sink_->RemoveSurfaceReferences( |
| 40 std::vector<cc::SurfaceReference>{ |
| 41 cc::SurfaceReference(root_window_->delegate()->GetRootSurfaceId(), |
| 42 surface_tracker_.current_surface_id())}); |
| 43 } |
| 44 |
| 35 // Invalidate WeakPtrs now to avoid callbacks back into the | 45 // Invalidate WeakPtrs now to avoid callbacks back into the |
| 36 // FrameGenerator during destruction of |compositor_frame_sink_|. | 46 // FrameGenerator during destruction of |compositor_frame_sink_|. |
| 37 weak_factory_.InvalidateWeakPtrs(); | 47 weak_factory_.InvalidateWeakPtrs(); |
| 38 compositor_frame_sink_.reset(); | 48 compositor_frame_sink_.reset(); |
| 39 } | 49 } |
| 40 | 50 |
| 41 void FrameGenerator::OnAcceleratedWidgetAvailable( | 51 void FrameGenerator::OnAcceleratedWidgetAvailable( |
| 42 gfx::AcceleratedWidget widget) { | 52 gfx::AcceleratedWidget widget) { |
| 43 DCHECK_NE(gfx::kNullAcceleratedWidget, widget); | 53 DCHECK_NE(gfx::kNullAcceleratedWidget, widget); |
| 44 cc::mojom::MojoCompositorFrameSinkRequest request(&compositor_frame_sink_); | 54 cc::mojom::MojoCompositorFrameSinkRequest request(&compositor_frame_sink_); |
| 45 cc::mojom::DisplayPrivateRequest display_private_request(&display_private_); | 55 cc::mojom::DisplayPrivateRequest display_private_request(&display_private_); |
| 46 root_window_->CreateDisplayCompositorFrameSink( | 56 root_window_->CreateDisplayCompositorFrameSink( |
| 47 widget, std::move(request), binding_.CreateInterfacePtrAndBind(), | 57 widget, std::move(request), binding_.CreateInterfacePtrAndBind(), |
| 48 std::move(display_private_request)); | 58 std::move(display_private_request)); |
| 49 // TODO(fsamuel): This means we're always requesting a new BeginFrame signal | 59 // TODO(fsamuel): This means we're always requesting a new BeginFrame signal |
| 50 // even when we don't need it. Once surface ID propagation work is done, | 60 // even when we don't need it. Once surface ID propagation work is done, |
| 51 // this will not be necessary because FrameGenerator will only need a | 61 // this will not be necessary because FrameGenerator will only need a |
| 52 // BeginFrame if the window manager changes. | 62 // BeginFrame if the window manager changes. |
| 53 compositor_frame_sink_->SetNeedsBeginFrame(true); | 63 compositor_frame_sink_->SetNeedsBeginFrame(true); |
| 54 } | 64 } |
| 55 | 65 |
| 56 void FrameGenerator::OnSurfaceCreated(const cc::SurfaceId& surface_id, | 66 void FrameGenerator::OnSurfaceCreated(const cc::SurfaceId& surface_id, |
| 57 ServerWindow* window) { | 67 ServerWindow* window) { |
| 58 DCHECK(surface_id.is_valid()); | 68 DCHECK(surface_id.is_valid()); |
| 59 | 69 |
| 60 auto iter = active_references_.find(surface_id.frame_sink_id()); | 70 // Only handle embedded surfaces changing here. The display root surface |
| 61 if (iter == active_references_.end()) { | 71 // changing is handled immediately after the CompositorFrame is submitted. |
| 62 AddFirstReference(surface_id, window); | 72 if (window != root_window_) |
| 63 return; | 73 surface_tracker_.EmbedSurface(surface_id); |
| 64 } | |
| 65 | |
| 66 cc::SurfaceReference& ref = iter->second; | |
| 67 | |
| 68 // This shouldn't be called multiple times for the same SurfaceId. | |
| 69 DCHECK_EQ(surface_id.frame_sink_id(), ref.child_id().frame_sink_id()); | |
| 70 DCHECK_NE(surface_id.local_frame_id(), ref.child_id().local_frame_id()); | |
| 71 | |
| 72 // The current reference will be removed after the next CompositorFrame is | |
| 73 // submitted or FrameGenerator is destroyed. | |
| 74 references_to_remove_.push_back(ref); | |
| 75 cc::SurfaceId old_surface_id = ref.child_id(); | |
| 76 | |
| 77 // New surface reference is recorded and will be added at end of this method. | |
| 78 ref = cc::SurfaceReference(ref.parent_id(), surface_id); | |
| 79 references_to_add_.push_back(ref); | |
| 80 | |
| 81 // If the display root surface has changed, add references from the new | |
| 82 // SurfaceId to all embedded surfaces. For example, this would happen when the | |
| 83 // display resolution or device scale factor changes. | |
| 84 if (window == root_window_) | |
| 85 AddNewParentReferences(old_surface_id, surface_id); | |
| 86 | |
| 87 PerformAddSurfaceReferences(); | |
| 88 } | 74 } |
| 89 | 75 |
| 90 void FrameGenerator::DidReceiveCompositorFrameAck() {} | 76 void FrameGenerator::DidReceiveCompositorFrameAck() {} |
| 91 | 77 |
| 92 void FrameGenerator::OnBeginFrame(const cc::BeginFrameArgs& begin_frame_arags) { | 78 void FrameGenerator::OnBeginFrame(const cc::BeginFrameArgs& begin_frame_arags) { |
| 93 if (!root_window_->visible()) | 79 if (!root_window_->visible()) |
| 94 return; | 80 return; |
| 95 | 81 |
| 96 // TODO(fsamuel): We should add a trace for generating a top level frame. | 82 // TODO(fsamuel): We should add a trace for generating a top level frame. |
| 97 cc::CompositorFrame frame(GenerateCompositorFrame(root_window_->bounds())); | 83 cc::CompositorFrame frame(GenerateCompositorFrame(root_window_->bounds())); |
| 98 | 84 |
| 99 if (compositor_frame_sink_) { | 85 if (compositor_frame_sink_) { |
| 100 gfx::Size frame_size = last_submitted_frame_size_; | 86 gfx::Size frame_size = last_submitted_frame_size_; |
| 101 if (!frame.render_pass_list.empty()) | 87 if (!frame.render_pass_list.empty()) |
| 102 frame_size = frame.render_pass_list[0]->output_rect.size(); | 88 frame_size = frame.render_pass_list[0]->output_rect.size(); |
| 103 if (!local_frame_id_.is_valid() || frame_size != last_submitted_frame_size_) | 89 |
| 90 bool display_surface_changed = false; |
| 91 if (!local_frame_id_.is_valid() || |
| 92 frame_size != last_submitted_frame_size_) { |
| 104 local_frame_id_ = id_allocator_.GenerateId(); | 93 local_frame_id_ = id_allocator_.GenerateId(); |
| 94 display_surface_changed = true; |
| 95 } else { |
| 96 // If the display surface is changing then we shouldn't add references |
| 97 // from the old display surface. We want to add references from the new |
| 98 // display surface, this happens after we submit the first CompositorFrame |
| 99 // so the new display surface exists. |
| 100 surface_tracker_.SendAddSurfaceReferences(compositor_frame_sink_.get()); |
| 101 } |
| 102 |
| 105 compositor_frame_sink_->SubmitCompositorFrame(local_frame_id_, | 103 compositor_frame_sink_->SubmitCompositorFrame(local_frame_id_, |
| 106 std::move(frame)); | 104 std::move(frame)); |
| 107 last_submitted_frame_size_ = frame_size; | 105 last_submitted_frame_size_ = frame_size; |
| 108 | 106 |
| 109 // Remove dead references after we submit a frame. This has to happen after | 107 if (display_surface_changed) { |
| 110 // the frame is submitted otherwise we could end up deleting a surface that | 108 UpdateDisplaySurfaceId(); |
| 111 // is still embedded in the last frame. | 109 } else { |
| 112 PerformRemoveSurfaceReferences(); | 110 // Remove references to surfaces that are no longer embedded. This has to |
| 111 // happen after the frame is submitted otherwise we could end up deleting |
| 112 // a surface that is still embedded in the last submitted frame. |
| 113 surface_tracker_.SendRemoveSurfaceReferences( |
| 114 compositor_frame_sink_.get()); |
| 115 } |
| 113 } | 116 } |
| 114 } | 117 } |
| 115 | 118 |
| 116 void FrameGenerator::ReclaimResources( | 119 void FrameGenerator::ReclaimResources( |
| 117 const cc::ReturnedResourceArray& resources) { | 120 const cc::ReturnedResourceArray& resources) { |
| 118 // Nothing to do here because FrameGenerator CompositorFrames don't reference | 121 // Nothing to do here because FrameGenerator CompositorFrames don't reference |
| 119 // any resources. | 122 // any resources. |
| 120 } | 123 } |
| 121 | 124 |
| 122 void FrameGenerator::WillDrawSurface() { | 125 void FrameGenerator::WillDrawSurface() { |
| 123 // TODO(fsamuel, staraz): Implement this. | 126 // TODO(fsamuel, staraz): Implement this. |
| 124 } | 127 } |
| 125 | 128 |
| 129 void FrameGenerator::UpdateDisplaySurfaceId() { |
| 130 // FrameGenerator owns the display root surface and is a bit of a special |
| 131 // case. There is no surface that embeds the display surface, so nothing |
| 132 // would reference it. Instead, a reference from the top level root is added |
| 133 // to mark the display surface as visible. As a result, FrameGenerator is |
| 134 // responsible for maintaining a reference from the top level root to the |
| 135 // display surface, in addition to references from the display surface to |
| 136 // embedded surfaces. |
| 137 const cc::SurfaceId old_surface_id = surface_tracker_.current_surface_id(); |
| 138 const cc::SurfaceId new_surface_id( |
| 139 cc::FrameSinkId(WindowIdToTransportId(root_window_->id()), 1u), |
| 140 local_frame_id_); |
| 141 |
| 142 DCHECK_NE(old_surface_id, new_surface_id); |
| 143 |
| 144 // Adds a reference from the top level root to the new display surface. |
| 145 compositor_frame_sink_->AddSurfaceReferences( |
| 146 std::vector<cc::SurfaceReference>{cc::SurfaceReference( |
| 147 root_window_->delegate()->GetRootSurfaceId(), new_surface_id)}); |
| 148 |
| 149 // Set new SurfaceId for the display surface. This will add references from |
| 150 // the new display surface to all embedded surfaces. |
| 151 surface_tracker_.SetCurrentSurfaceId(new_surface_id); |
| 152 surface_tracker_.SendAddSurfaceReferences(compositor_frame_sink_.get()); |
| 153 |
| 154 // Remove the reference from the top level root to the old display surface |
| 155 // last. Not applicable for the first display surface. |
| 156 if (old_surface_id.is_valid()) { |
| 157 compositor_frame_sink_->RemoveSurfaceReferences( |
| 158 std::vector<cc::SurfaceReference>{cc::SurfaceReference( |
| 159 root_window_->delegate()->GetRootSurfaceId(), old_surface_id)}); |
| 160 } |
| 161 } |
| 162 |
| 126 cc::CompositorFrame FrameGenerator::GenerateCompositorFrame( | 163 cc::CompositorFrame FrameGenerator::GenerateCompositorFrame( |
| 127 const gfx::Rect& output_rect) { | 164 const gfx::Rect& output_rect) { |
| 128 const int render_pass_id = 1; | 165 const int render_pass_id = 1; |
| 129 std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); | 166 std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); |
| 130 render_pass->SetNew(render_pass_id, output_rect, output_rect, | 167 render_pass->SetNew(render_pass_id, output_rect, output_rect, |
| 131 gfx::Transform()); | 168 gfx::Transform()); |
| 132 | 169 |
| 133 DrawWindowTree(render_pass.get(), root_window_, gfx::Vector2d(), 1.0f); | 170 DrawWindowTree(render_pass.get(), root_window_, gfx::Vector2d(), 1.0f); |
| 134 | 171 |
| 135 cc::CompositorFrame frame; | 172 cc::CompositorFrame frame; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 combined_opacity, SkBlendMode::kSrcOver, 0 /* sorting-context_id */); | 261 combined_opacity, SkBlendMode::kSrcOver, 0 /* sorting-context_id */); |
| 225 | 262 |
| 226 auto* quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); | 263 auto* quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); |
| 227 quad->SetAll(sqs, bounds_at_origin /* rect */, | 264 quad->SetAll(sqs, bounds_at_origin /* rect */, |
| 228 gfx::Rect() /* opaque_rect */, | 265 gfx::Rect() /* opaque_rect */, |
| 229 bounds_at_origin /* visible_rect */, true /* needs_blending*/, | 266 bounds_at_origin /* visible_rect */, true /* needs_blending*/, |
| 230 underlay_surface_id); | 267 underlay_surface_id); |
| 231 } | 268 } |
| 232 } | 269 } |
| 233 | 270 |
| 234 cc::SurfaceId FrameGenerator::FindParentSurfaceId(ServerWindow* window) { | |
| 235 if (window == root_window_) | |
| 236 return root_window_->delegate()->GetRootSurfaceId(); | |
| 237 | |
| 238 // The root window holds the parent SurfaceId. This SurfaceId will have an | |
| 239 // invalid LocalFrameId before FrameGenerator has submitted a CompositorFrame. | |
| 240 // After the first frame is submitted it will always be a valid SurfaceId. | |
| 241 return root_window_->compositor_frame_sink_manager()->GetLatestSurfaceId( | |
| 242 mojom::CompositorFrameSinkType::DEFAULT); | |
| 243 } | |
| 244 | |
| 245 void FrameGenerator::AddSurfaceReference(const cc::SurfaceId& parent_id, | |
| 246 const cc::SurfaceId& child_id) { | |
| 247 DCHECK_NE(parent_id, child_id); | |
| 248 | |
| 249 // Add new reference from parent to surface and record reference. | |
| 250 cc::SurfaceReference ref(parent_id, child_id); | |
| 251 active_references_[child_id.frame_sink_id()] = ref; | |
| 252 references_to_add_.push_back(ref); | |
| 253 } | |
| 254 | |
| 255 void FrameGenerator::AddFirstReference(const cc::SurfaceId& surface_id, | |
| 256 ServerWindow* window) { | |
| 257 cc::SurfaceId parent_id = FindParentSurfaceId(window); | |
| 258 | |
| 259 if (parent_id.local_frame_id().is_valid()) { | |
| 260 AddSurfaceReference(parent_id, surface_id); | |
| 261 | |
| 262 // For the first display root surface, add references to any child surfaces | |
| 263 // that were created before it, since no reference has been added yet. | |
| 264 if (window == root_window_) { | |
| 265 for (auto& child_surface_id : waiting_for_references_) | |
| 266 AddSurfaceReference(surface_id, child_surface_id); | |
| 267 waiting_for_references_.clear(); | |
| 268 } | |
| 269 | |
| 270 PerformAddSurfaceReferences(); | |
| 271 } else { | |
| 272 // This isn't the display root surface and display root surface hasn't | |
| 273 // submitted a CF yet. We can't add a reference to an unknown SurfaceId. | |
| 274 waiting_for_references_.push_back(surface_id); | |
| 275 } | |
| 276 | |
| 277 // Observe |window| so that we can remove references when it's destroyed. | |
| 278 Add(window); | |
| 279 } | |
| 280 | |
| 281 void FrameGenerator::AddNewParentReferences( | |
| 282 const cc::SurfaceId& old_surface_id, | |
| 283 const cc::SurfaceId& new_surface_id) { | |
| 284 DCHECK_EQ(old_surface_id.frame_sink_id(), new_surface_id.frame_sink_id()); | |
| 285 | |
| 286 for (auto& map_entry : active_references_) { | |
| 287 cc::SurfaceReference& ref = map_entry.second; | |
| 288 if (ref.parent_id() == old_surface_id) { | |
| 289 ref = cc::SurfaceReference(new_surface_id, ref.child_id()); | |
| 290 references_to_add_.push_back(ref); | |
| 291 } | |
| 292 } | |
| 293 } | |
| 294 | |
| 295 void FrameGenerator::PerformAddSurfaceReferences() { | |
| 296 if (references_to_add_.empty()) | |
| 297 return; | |
| 298 | |
| 299 compositor_frame_sink_->AddSurfaceReferences(references_to_add_); | |
| 300 references_to_add_.clear(); | |
| 301 } | |
| 302 | |
| 303 void FrameGenerator::PerformRemoveSurfaceReferences() { | |
| 304 if (references_to_remove_.empty()) | |
| 305 return; | |
| 306 | |
| 307 compositor_frame_sink_->RemoveSurfaceReferences(references_to_remove_); | |
| 308 references_to_remove_.clear(); | |
| 309 } | |
| 310 | |
| 311 void FrameGenerator::RemoveFrameSinkReference( | |
| 312 const cc::FrameSinkId& frame_sink_id) { | |
| 313 auto it = active_references_.find(frame_sink_id); | |
| 314 if (it == active_references_.end()) | |
| 315 return; | |
| 316 references_to_remove_.push_back(it->second); | |
| 317 active_references_.erase(it); | |
| 318 } | |
| 319 | |
| 320 void FrameGenerator::RemoveAllSurfaceReferences() { | |
| 321 for (auto& map_entry : active_references_) | |
| 322 references_to_remove_.push_back(map_entry.second); | |
| 323 active_references_.clear(); | |
| 324 PerformRemoveSurfaceReferences(); | |
| 325 } | |
| 326 | |
| 327 void FrameGenerator::OnWindowDestroying(ServerWindow* window) { | 271 void FrameGenerator::OnWindowDestroying(ServerWindow* window) { |
| 328 Remove(window); | 272 Remove(window); |
| 329 ServerWindowCompositorFrameSinkManager* compositor_frame_sink_manager = | 273 ServerWindowCompositorFrameSinkManager* compositor_frame_sink_manager = |
| 330 window->compositor_frame_sink_manager(); | 274 window->compositor_frame_sink_manager(); |
| 331 // If FrameGenerator was observing |window|, then that means it had a | 275 // If FrameGenerator was observing |window|, then that means it had a |
| 332 // CompositorFrame at some point in time and should have a | 276 // CompositorFrame at some point in time and should have a |
| 333 // ServerWindowCompositorFrameSinkManager. | 277 // ServerWindowCompositorFrameSinkManager. |
| 334 DCHECK(compositor_frame_sink_manager); | 278 DCHECK(compositor_frame_sink_manager); |
| 335 | 279 |
| 336 cc::SurfaceId default_surface_id = | 280 cc::SurfaceId default_surface_id = |
| 337 window->compositor_frame_sink_manager()->GetLatestSurfaceId( | 281 window->compositor_frame_sink_manager()->GetLatestSurfaceId( |
| 338 mojom::CompositorFrameSinkType::DEFAULT); | 282 mojom::CompositorFrameSinkType::DEFAULT); |
| 339 if (default_surface_id.is_valid()) | 283 if (default_surface_id.is_valid()) |
| 340 RemoveFrameSinkReference(default_surface_id.frame_sink_id()); | 284 surface_tracker_.UnembedSurface(default_surface_id.frame_sink_id()); |
| 341 | 285 |
| 342 cc::SurfaceId underlay_surface_id = | 286 cc::SurfaceId underlay_surface_id = |
| 343 window->compositor_frame_sink_manager()->GetLatestSurfaceId( | 287 window->compositor_frame_sink_manager()->GetLatestSurfaceId( |
| 344 mojom::CompositorFrameSinkType::UNDERLAY); | 288 mojom::CompositorFrameSinkType::UNDERLAY); |
| 345 if (underlay_surface_id.is_valid()) | 289 if (underlay_surface_id.is_valid()) |
| 346 RemoveFrameSinkReference(underlay_surface_id.frame_sink_id()); | 290 surface_tracker_.UnembedSurface(underlay_surface_id.frame_sink_id()); |
| 347 } | 291 } |
| 348 | 292 |
| 349 } // namespace ws | 293 } // namespace ws |
| 350 | 294 |
| 351 } // namespace ui | 295 } // namespace ui |
| OLD | NEW |