Chromium Code Reviews| 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. | |
|
jbauman
2017/01/11 00:26:25
It's a bit odd that this would matter, as the comp
kylechar
2017/01/11 18:04:49
I hadn't really thought too much about it but you'
| |
| 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 // Add observer for window the first time it's seen. |
| 74 if (surface_tracker_.EmbedSurface(surface_id)) | |
| 75 Add(window); | |
| 64 } | 76 } |
| 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 } | 77 } |
| 89 | 78 |
| 90 void FrameGenerator::DidReceiveCompositorFrameAck() {} | 79 void FrameGenerator::DidReceiveCompositorFrameAck() {} |
| 91 | 80 |
| 92 void FrameGenerator::OnBeginFrame(const cc::BeginFrameArgs& begin_frame_arags) { | 81 void FrameGenerator::OnBeginFrame(const cc::BeginFrameArgs& begin_frame_arags) { |
| 93 if (!root_window_->visible()) | 82 if (!root_window_->visible()) |
| 94 return; | 83 return; |
| 95 | 84 |
| 96 // TODO(fsamuel): We should add a trace for generating a top level frame. | 85 // TODO(fsamuel): We should add a trace for generating a top level frame. |
| 97 cc::CompositorFrame frame(GenerateCompositorFrame(root_window_->bounds())); | 86 cc::CompositorFrame frame(GenerateCompositorFrame(root_window_->bounds())); |
| 98 | 87 |
| 99 if (compositor_frame_sink_) { | 88 if (compositor_frame_sink_) { |
| 100 gfx::Size frame_size = last_submitted_frame_size_; | 89 gfx::Size frame_size = last_submitted_frame_size_; |
| 101 if (!frame.render_pass_list.empty()) | 90 if (!frame.render_pass_list.empty()) |
| 102 frame_size = frame.render_pass_list[0]->output_rect.size(); | 91 frame_size = frame.render_pass_list[0]->output_rect.size(); |
| 103 if (!local_frame_id_.is_valid() || frame_size != last_submitted_frame_size_) | 92 |
| 93 bool display_surface_changed = false; | |
| 94 if (!local_frame_id_.is_valid() || | |
| 95 frame_size != last_submitted_frame_size_) { | |
| 104 local_frame_id_ = id_allocator_.GenerateId(); | 96 local_frame_id_ = id_allocator_.GenerateId(); |
| 97 display_surface_changed = true; | |
| 98 } else { | |
| 99 // If the display surface is changing then we shouldn't add references | |
| 100 // from the old display surface. We want to add references from the new | |
| 101 // display surface, this happens after we submit the first CompositorFrame | |
| 102 // so the new display surface exists. | |
| 103 if (surface_tracker_.HasReferencesToAdd()) { | |
| 104 compositor_frame_sink_->AddSurfaceReferences( | |
| 105 surface_tracker_.GetReferencesToAdd()); | |
| 106 } | |
| 107 } | |
| 108 | |
| 105 compositor_frame_sink_->SubmitCompositorFrame(local_frame_id_, | 109 compositor_frame_sink_->SubmitCompositorFrame(local_frame_id_, |
| 106 std::move(frame)); | 110 std::move(frame)); |
| 107 last_submitted_frame_size_ = frame_size; | 111 last_submitted_frame_size_ = frame_size; |
| 108 | 112 |
| 109 // Remove dead references after we submit a frame. This has to happen after | 113 if (display_surface_changed) |
| 110 // the frame is submitted otherwise we could end up deleting a surface that | 114 UpdateDisplaySurfaceId(); |
| 111 // is still embedded in the last frame. | 115 |
| 112 PerformRemoveSurfaceReferences(); | 116 // Remove references to surfaces that are no longer embedded. This has to |
| 117 // happen after the frame is submitted otherwise we could end up deleting | |
| 118 // a surface that is still embedded in the last submitted frame. | |
| 119 if (surface_tracker_.HasReferencesToRemove()) { | |
| 120 compositor_frame_sink_->RemoveSurfaceReferences( | |
| 121 surface_tracker_.GetReferencesToRemove()); | |
| 122 } | |
| 113 } | 123 } |
| 114 } | 124 } |
| 115 | 125 |
| 116 void FrameGenerator::ReclaimResources( | 126 void FrameGenerator::ReclaimResources( |
| 117 const cc::ReturnedResourceArray& resources) { | 127 const cc::ReturnedResourceArray& resources) { |
| 118 // Nothing to do here because FrameGenerator CompositorFrames don't reference | 128 // Nothing to do here because FrameGenerator CompositorFrames don't reference |
| 119 // any resources. | 129 // any resources. |
| 120 } | 130 } |
| 121 | 131 |
| 122 void FrameGenerator::WillDrawSurface() { | 132 void FrameGenerator::WillDrawSurface() { |
| 123 // TODO(fsamuel, staraz): Implement this. | 133 // TODO(fsamuel, staraz): Implement this. |
| 124 } | 134 } |
| 125 | 135 |
| 136 void FrameGenerator::UpdateDisplaySurfaceId() { | |
| 137 // FrameGenerator owns the display root surface and is a bit of a special | |
| 138 // case. There is no surface that embeds the display surface, so nothing | |
| 139 // would reference it. Instead, a reference from the top level root is added | |
| 140 // to mark the display surface as visible. As a result, FrameGenerator is | |
| 141 // responsible for maintaining a reference from the top level root to the | |
| 142 // display surface, in addition to references from the display surface to | |
| 143 // embedded surfaces. | |
| 144 const cc::SurfaceId old_surface_id = surface_tracker_.current_surface_id(); | |
| 145 const cc::SurfaceId new_surface_id( | |
| 146 cc::FrameSinkId(WindowIdToTransportId(root_window_->id()), 1u), | |
| 147 local_frame_id_); | |
| 148 | |
| 149 DCHECK_NE(old_surface_id, new_surface_id); | |
| 150 | |
| 151 // Set new SurfaceId for the display surface. This will add references from | |
| 152 // the new display surface to all embedded surfaces. | |
| 153 surface_tracker_.SetCurrentSurfaceId(new_surface_id); | |
| 154 std::vector<cc::SurfaceReference> references_to_add = | |
| 155 surface_tracker_.GetReferencesToAdd(); | |
| 156 | |
| 157 // Adds a reference from the top level root to the new display surface. | |
| 158 references_to_add.push_back(cc::SurfaceReference( | |
| 159 root_window_->delegate()->GetRootSurfaceId(), new_surface_id)); | |
| 160 | |
| 161 compositor_frame_sink_->AddSurfaceReferences(references_to_add); | |
| 162 | |
| 163 // Remove the reference from the top level root to the old display surface | |
| 164 // after we have added references from the new display surface. Not applicable | |
| 165 // for the first display surface. | |
| 166 if (old_surface_id.is_valid()) { | |
| 167 compositor_frame_sink_->RemoveSurfaceReferences( | |
| 168 std::vector<cc::SurfaceReference>{cc::SurfaceReference( | |
| 169 root_window_->delegate()->GetRootSurfaceId(), old_surface_id)}); | |
| 170 } | |
| 171 } | |
| 172 | |
| 126 cc::CompositorFrame FrameGenerator::GenerateCompositorFrame( | 173 cc::CompositorFrame FrameGenerator::GenerateCompositorFrame( |
| 127 const gfx::Rect& output_rect) { | 174 const gfx::Rect& output_rect) { |
| 128 const int render_pass_id = 1; | 175 const int render_pass_id = 1; |
| 129 std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); | 176 std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); |
| 130 render_pass->SetNew(render_pass_id, output_rect, output_rect, | 177 render_pass->SetNew(render_pass_id, output_rect, output_rect, |
| 131 gfx::Transform()); | 178 gfx::Transform()); |
| 132 | 179 |
| 133 DrawWindowTree(render_pass.get(), root_window_, gfx::Vector2d(), 1.0f); | 180 DrawWindowTree(render_pass.get(), root_window_, gfx::Vector2d(), 1.0f); |
| 134 | 181 |
| 135 cc::CompositorFrame frame; | 182 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 */); | 271 combined_opacity, SkBlendMode::kSrcOver, 0 /* sorting-context_id */); |
| 225 | 272 |
| 226 auto* quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); | 273 auto* quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); |
| 227 quad->SetAll(sqs, bounds_at_origin /* rect */, | 274 quad->SetAll(sqs, bounds_at_origin /* rect */, |
| 228 gfx::Rect() /* opaque_rect */, | 275 gfx::Rect() /* opaque_rect */, |
| 229 bounds_at_origin /* visible_rect */, true /* needs_blending*/, | 276 bounds_at_origin /* visible_rect */, true /* needs_blending*/, |
| 230 underlay_surface_id); | 277 underlay_surface_id); |
| 231 } | 278 } |
| 232 } | 279 } |
| 233 | 280 |
| 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) { | 281 void FrameGenerator::OnWindowDestroying(ServerWindow* window) { |
| 328 Remove(window); | 282 Remove(window); |
| 329 ServerWindowCompositorFrameSinkManager* compositor_frame_sink_manager = | 283 ServerWindowCompositorFrameSinkManager* compositor_frame_sink_manager = |
| 330 window->compositor_frame_sink_manager(); | 284 window->compositor_frame_sink_manager(); |
| 331 // If FrameGenerator was observing |window|, then that means it had a | 285 // If FrameGenerator was observing |window|, then that means it had a |
| 332 // CompositorFrame at some point in time and should have a | 286 // CompositorFrame at some point in time and should have a |
| 333 // ServerWindowCompositorFrameSinkManager. | 287 // ServerWindowCompositorFrameSinkManager. |
| 334 DCHECK(compositor_frame_sink_manager); | 288 DCHECK(compositor_frame_sink_manager); |
| 335 | 289 |
| 336 cc::SurfaceId default_surface_id = | 290 cc::SurfaceId default_surface_id = |
| 337 window->compositor_frame_sink_manager()->GetLatestSurfaceId( | 291 window->compositor_frame_sink_manager()->GetLatestSurfaceId( |
| 338 mojom::CompositorFrameSinkType::DEFAULT); | 292 mojom::CompositorFrameSinkType::DEFAULT); |
| 339 if (default_surface_id.is_valid()) | 293 if (default_surface_id.is_valid()) |
| 340 RemoveFrameSinkReference(default_surface_id.frame_sink_id()); | 294 surface_tracker_.UnembedSurface(default_surface_id.frame_sink_id()); |
| 341 | 295 |
| 342 cc::SurfaceId underlay_surface_id = | 296 cc::SurfaceId underlay_surface_id = |
| 343 window->compositor_frame_sink_manager()->GetLatestSurfaceId( | 297 window->compositor_frame_sink_manager()->GetLatestSurfaceId( |
| 344 mojom::CompositorFrameSinkType::UNDERLAY); | 298 mojom::CompositorFrameSinkType::UNDERLAY); |
| 345 if (underlay_surface_id.is_valid()) | 299 if (underlay_surface_id.is_valid()) |
| 346 RemoveFrameSinkReference(underlay_surface_id.frame_sink_id()); | 300 surface_tracker_.UnembedSurface(underlay_surface_id.frame_sink_id()); |
| 347 } | 301 } |
| 348 | 302 |
| 349 } // namespace ws | 303 } // namespace ws |
| 350 | 304 |
| 351 } // namespace ui | 305 } // namespace ui |
| OLD | NEW |