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/surfaces/display_compositor.cc

Issue 2520513002: Add DisplayCompositorTest. (Closed)
Patch Set: Rebase + improve. Created 4 years 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/surfaces/display_compositor.h" 5 #include "services/ui/surfaces/display_compositor.h"
6 6
7 #include <utility>
8
7 #include "cc/output/in_process_context_provider.h" 9 #include "cc/output/in_process_context_provider.h"
8 #include "cc/surfaces/surface.h" 10 #include "cc/surfaces/surface.h"
9 #include "gpu/command_buffer/client/shared_memory_limits.h" 11 #include "gpu/command_buffer/client/shared_memory_limits.h"
10 #include "gpu/ipc/gpu_in_process_thread_service.h" 12 #include "gpu/ipc/gpu_in_process_thread_service.h"
11 #include "mojo/public/cpp/bindings/strong_binding.h" 13 #include "mojo/public/cpp/bindings/strong_binding.h"
12 #include "services/ui/surfaces/gpu_compositor_frame_sink.h" 14 #include "services/ui/surfaces/gpu_compositor_frame_sink.h"
13 15
14 namespace ui { 16 namespace ui {
15 17
16 DisplayCompositor::DisplayCompositor( 18 DisplayCompositor::DisplayCompositor(
17 scoped_refptr<gpu::InProcessCommandBuffer::Service> gpu_service, 19 scoped_refptr<gpu::InProcessCommandBuffer::Service> gpu_service,
18 std::unique_ptr<MusGpuMemoryBufferManager> gpu_memory_buffer_manager, 20 std::unique_ptr<MusGpuMemoryBufferManager> gpu_memory_buffer_manager,
19 gpu::ImageFactory* image_factory, 21 gpu::ImageFactory* image_factory,
20 cc::mojom::DisplayCompositorRequest request, 22 cc::mojom::DisplayCompositorRequest request,
21 cc::mojom::DisplayCompositorClientPtr client) 23 cc::mojom::DisplayCompositorClientPtr client)
22 : gpu_service_(std::move(gpu_service)), 24 : gpu_service_(std::move(gpu_service)),
23 gpu_memory_buffer_manager_(std::move(gpu_memory_buffer_manager)), 25 gpu_memory_buffer_manager_(std::move(gpu_memory_buffer_manager)),
24 image_factory_(image_factory), 26 image_factory_(image_factory),
25 client_(std::move(client)), 27 client_(std::move(client)),
28 reference_manager_(&manager_),
26 binding_(this, std::move(request)) { 29 binding_(this, std::move(request)) {
27 manager_.AddObserver(this); 30 manager_.AddObserver(this);
28 } 31 }
29 32
30 void DisplayCompositor::CreateCompositorFrameSink( 33 void DisplayCompositor::CreateCompositorFrameSink(
31 const cc::FrameSinkId& frame_sink_id, 34 const cc::FrameSinkId& frame_sink_id,
32 gpu::SurfaceHandle surface_handle, 35 gpu::SurfaceHandle surface_handle,
33 cc::mojom::MojoCompositorFrameSinkRequest request, 36 cc::mojom::MojoCompositorFrameSinkRequest request,
34 cc::mojom::MojoCompositorFrameSinkPrivateRequest private_request, 37 cc::mojom::MojoCompositorFrameSinkPrivateRequest private_request,
35 cc::mojom::MojoCompositorFrameSinkClientPtr client) { 38 cc::mojom::MojoCompositorFrameSinkClientPtr client) {
36 // We cannot create more than one CompositorFrameSink with a given 39 // We cannot create more than one CompositorFrameSink with a given
37 // |frame_sink_id|. 40 // |frame_sink_id|.
38 DCHECK_EQ(0u, compositor_frame_sinks_.count(frame_sink_id)); 41 DCHECK_EQ(0u, compositor_frame_sinks_.count(frame_sink_id));
39 scoped_refptr<cc::InProcessContextProvider> context_provider; 42 scoped_refptr<cc::InProcessContextProvider> context_provider;
40 if (surface_handle != gpu::kNullSurfaceHandle) { 43 if (surface_handle != gpu::kNullSurfaceHandle) {
41 context_provider = new cc::InProcessContextProvider( 44 context_provider = new cc::InProcessContextProvider(
42 gpu_service_, surface_handle, gpu_memory_buffer_manager_.get(), 45 gpu_service_, surface_handle, gpu_memory_buffer_manager_.get(),
43 image_factory_, gpu::SharedMemoryLimits(), 46 image_factory_, gpu::SharedMemoryLimits(),
44 nullptr /* shared_context */); 47 nullptr /* shared_context */);
45 } 48 }
46 compositor_frame_sinks_[frame_sink_id] = 49 compositor_frame_sinks_[frame_sink_id] =
47 base::MakeUnique<GpuCompositorFrameSink>( 50 base::MakeUnique<GpuCompositorFrameSink>(
48 this, frame_sink_id, surface_handle, gpu_memory_buffer_manager_.get(), 51 this, frame_sink_id, surface_handle, gpu_memory_buffer_manager_.get(),
49 std::move(context_provider), std::move(request), 52 std::move(context_provider), std::move(request),
50 std::move(private_request), std::move(client)); 53 std::move(private_request), std::move(client));
51 } 54 }
52 55
53 void DisplayCompositor::AddRootSurfaceReference(const cc::SurfaceId& child_id) { 56 void DisplayCompositor::AddRootSurfaceReference(const cc::SurfaceId& child_id) {
54 AddSurfaceReference(manager_.GetRootSurfaceId(), child_id); 57 AddSurfaceReference(GetRootSurfaceId(), child_id);
55 } 58 }
56 59
57 void DisplayCompositor::AddSurfaceReference(const cc::SurfaceId& parent_id, 60 void DisplayCompositor::AddSurfaceReference(const cc::SurfaceId& parent_id,
58 const cc::SurfaceId& child_id) { 61 const cc::SurfaceId& child_id) {
59 auto vector_iter = temp_references_.find(child_id.frame_sink_id()); 62 auto vector_iter = temp_references_.find(child_id.frame_sink_id());
60 63
61 // If there are no temporary references for the FrameSinkId then we can just 64 // If there are no temporary references for the FrameSinkId then we can just
62 // add reference and return. 65 // add reference and return.
63 if (vector_iter == temp_references_.end()) { 66 if (vector_iter == temp_references_.end()) {
64 manager_.AddSurfaceReference(parent_id, child_id); 67 reference_manager_->AddSurfaceReference(parent_id, child_id);
65 return; 68 return;
66 } 69 }
67 70
68 // Get the vector<LocalFrameId> for the appropriate FrameSinkId and look for 71 // Get the vector<LocalFrameId> for the appropriate FrameSinkId and look for
69 // |child_id.local_frame_id| in that vector. If found, there is a temporary 72 // |child_id.local_frame_id| in that vector. If found, there is a temporary
70 // reference to |child_id|. 73 // reference to |child_id|.
71 std::vector<cc::LocalFrameId>& refs = vector_iter->second; 74 std::vector<cc::LocalFrameId>& refs = vector_iter->second;
72 auto temp_ref_iter = 75 auto temp_ref_iter =
73 std::find(refs.begin(), refs.end(), child_id.local_frame_id()); 76 std::find(refs.begin(), refs.end(), child_id.local_frame_id());
74 77
75 if (temp_ref_iter == refs.end()) { 78 if (temp_ref_iter == refs.end()) {
76 manager_.AddSurfaceReference(parent_id, child_id); 79 reference_manager_->AddSurfaceReference(parent_id, child_id);
77 return; 80 return;
78 } 81 }
79 82
80 // All surfaces get a temporary reference to the top level root. If the parent 83 // All surfaces get a temporary reference to the top level root. If the parent
81 // wants to add a reference to the top level root then we do nothing. 84 // wants to add a reference to the top level root then we do nothing.
82 // Otherwise remove the temporary reference and add the reference. 85 // Otherwise remove the temporary reference and add the reference.
83 if (parent_id != manager_.GetRootSurfaceId()) { 86 if (parent_id != GetRootSurfaceId()) {
84 manager_.AddSurfaceReference(parent_id, child_id); 87 reference_manager_->AddSurfaceReference(parent_id, child_id);
85 manager_.RemoveSurfaceReference(manager_.GetRootSurfaceId(), child_id); 88 reference_manager_->RemoveSurfaceReference(GetRootSurfaceId(), child_id);
86 } 89 }
87 90
88 // Remove temporary references for surfaces with the same FrameSinkId that 91 // Remove temporary references for surfaces with the same FrameSinkId that
89 // were created before |child_id|. The earlier surfaces were never embedded in 92 // were created before |child_id|. The earlier surfaces were never embedded in
90 // the parent and the parent is embedding a later surface, so we know the 93 // the parent and the parent is embedding a later surface, so we know the
91 // parent doesn't need them anymore. 94 // parent doesn't need them anymore.
92 for (auto iter = refs.begin(); iter != temp_ref_iter; ++iter) { 95 for (auto iter = refs.begin(); iter != temp_ref_iter; ++iter) {
93 cc::SurfaceId id = cc::SurfaceId(child_id.frame_sink_id(), *iter); 96 cc::SurfaceId id = cc::SurfaceId(child_id.frame_sink_id(), *iter);
94 manager_.RemoveSurfaceReference(manager_.GetRootSurfaceId(), id); 97 reference_manager_->RemoveSurfaceReference(GetRootSurfaceId(), id);
95 } 98 }
96 99
97 // Remove markers for temporary references up to |child_id|, as the temporary 100 // Remove markers for temporary references up to |child_id|, as the temporary
98 // references they correspond to were removed above. If |ref_iter| is the last 101 // references they correspond to were removed above. If |ref_iter| is the last
99 // element in |refs| then we are removing all temporary references for the 102 // element in |refs| then we are removing all temporary references for the
100 // FrameSinkId and can remove the map entry entirely. 103 // FrameSinkId and can remove the map entry entirely.
101 if (++temp_ref_iter == refs.end()) 104 if (++temp_ref_iter == refs.end())
102 temp_references_.erase(child_id.frame_sink_id()); 105 temp_references_.erase(child_id.frame_sink_id());
103 else 106 else
104 refs.erase(refs.begin(), ++temp_ref_iter); 107 refs.erase(refs.begin(), ++temp_ref_iter);
105 } 108 }
106 109
107 void DisplayCompositor::RemoveRootSurfaceReference( 110 void DisplayCompositor::RemoveRootSurfaceReference(
108 const cc::SurfaceId& child_id) { 111 const cc::SurfaceId& child_id) {
109 RemoveSurfaceReference(manager_.GetRootSurfaceId(), child_id); 112 RemoveSurfaceReference(GetRootSurfaceId(), child_id);
110 } 113 }
111 114
112 void DisplayCompositor::RemoveSurfaceReference(const cc::SurfaceId& parent_id, 115 void DisplayCompositor::RemoveSurfaceReference(const cc::SurfaceId& parent_id,
113 const cc::SurfaceId& child_id) { 116 const cc::SurfaceId& child_id) {
114 // TODO(kylechar): Each remove reference can trigger GC, it would be better if 117 // TODO(kylechar): Each remove reference can trigger GC, it would be better if
115 // we GC only once if removing multiple references. 118 // we GC only once if removing multiple references.
116 manager_.RemoveSurfaceReference(parent_id, child_id); 119 reference_manager_->RemoveSurfaceReference(parent_id, child_id);
117 } 120 }
118 121
119 DisplayCompositor::~DisplayCompositor() { 122 DisplayCompositor::~DisplayCompositor() {
120 // Remove all temporary references on shutdown. 123 // Remove all temporary references on shutdown.
121 for (auto& map_entry : temp_references_) { 124 for (auto& map_entry : temp_references_) {
122 const cc::FrameSinkId& frame_sink_id = map_entry.first; 125 const cc::FrameSinkId& frame_sink_id = map_entry.first;
123 for (auto& local_frame_id : map_entry.second) { 126 for (auto& local_frame_id : map_entry.second) {
124 manager_.RemoveSurfaceReference( 127 reference_manager_->RemoveSurfaceReference(
125 manager_.GetRootSurfaceId(), 128 GetRootSurfaceId(), cc::SurfaceId(frame_sink_id, local_frame_id));
126 cc::SurfaceId(frame_sink_id, local_frame_id));
127 } 129 }
128 } 130 }
129 manager_.RemoveObserver(this); 131 manager_.RemoveObserver(this);
130 } 132 }
131 133
132 void DisplayCompositor::OnCompositorFrameSinkClientConnectionLost( 134 void DisplayCompositor::OnCompositorFrameSinkClientConnectionLost(
133 const cc::FrameSinkId& frame_sink_id, 135 const cc::FrameSinkId& frame_sink_id,
134 bool destroy_compositor_frame_sink) { 136 bool destroy_compositor_frame_sink) {
135 if (destroy_compositor_frame_sink) 137 if (destroy_compositor_frame_sink)
136 compositor_frame_sinks_.erase(frame_sink_id); 138 compositor_frame_sinks_.erase(frame_sink_id);
137 // TODO(fsamuel): Tell the display compositor host that the client connection 139 // TODO(fsamuel): Tell the display compositor host that the client connection
138 // has been lost so that it can drop its private connection and allow a new 140 // has been lost so that it can drop its private connection and allow a new
139 // client instance to create a new CompositorFrameSink. 141 // client instance to create a new CompositorFrameSink.
140 } 142 }
141 143
142 void DisplayCompositor::OnCompositorFrameSinkPrivateConnectionLost( 144 void DisplayCompositor::OnCompositorFrameSinkPrivateConnectionLost(
143 const cc::FrameSinkId& frame_sink_id, 145 const cc::FrameSinkId& frame_sink_id,
144 bool destroy_compositor_frame_sink) { 146 bool destroy_compositor_frame_sink) {
145 if (destroy_compositor_frame_sink) 147 if (destroy_compositor_frame_sink)
146 compositor_frame_sinks_.erase(frame_sink_id); 148 compositor_frame_sinks_.erase(frame_sink_id);
147 } 149 }
148 150
151 const cc::SurfaceId& DisplayCompositor::GetRootSurfaceId() const {
152 return reference_manager_->GetRootSurfaceId();
153 }
154
149 void DisplayCompositor::OnSurfaceCreated(const cc::SurfaceId& surface_id, 155 void DisplayCompositor::OnSurfaceCreated(const cc::SurfaceId& surface_id,
150 const gfx::Size& frame_size, 156 const gfx::Size& frame_size,
151 float device_scale_factor) { 157 float device_scale_factor) {
152 // We can get into a situation where multiple CompositorFrames arrive for a 158 // We can get into a situation where multiple CompositorFrames arrive for a
153 // CompositorFrameSink before the DisplayCompositorClient can add any 159 // CompositorFrameSink before the DisplayCompositorClient can add any
154 // references for the frame. When the second frame with a new size arrives, 160 // references for the frame. When the second frame with a new size arrives,
155 // the first will be destroyed and then if there are no references it will be 161 // the first will be destroyed and then if there are no references it will be
156 // deleted during surface GC. A temporary reference, removed when a real 162 // deleted during surface GC. A temporary reference, removed when a real
157 // reference is received, is added to prevent this from happening. 163 // reference is received, is added to prevent this from happening.
158 manager_.AddSurfaceReference(manager_.GetRootSurfaceId(), surface_id); 164 reference_manager_->AddSurfaceReference(GetRootSurfaceId(), surface_id);
159 temp_references_[surface_id.frame_sink_id()].push_back( 165 temp_references_[surface_id.frame_sink_id()].push_back(
160 surface_id.local_frame_id()); 166 surface_id.local_frame_id());
161 167
162 if (client_) 168 if (client_)
163 client_->OnSurfaceCreated(surface_id, frame_size, device_scale_factor); 169 client_->OnSurfaceCreated(surface_id, frame_size, device_scale_factor);
164 } 170 }
165 171
166 void DisplayCompositor::OnSurfaceDamaged(const cc::SurfaceId& surface_id, 172 void DisplayCompositor::OnSurfaceDamaged(const cc::SurfaceId& surface_id,
167 bool* changed) {} 173 bool* changed) {}
168 174
169 } // namespace ui 175 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698