OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/ui/ws/gpu_compositor_frame_sink.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/single_thread_task_runner.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "cc/output/compositor_frame.h" | |
12 #include "cc/output/output_surface.h" | |
13 #include "cc/output/texture_mailbox_deleter.h" | |
14 #include "cc/quads/shared_quad_state.h" | |
15 #include "cc/quads/surface_draw_quad.h" | |
16 #include "cc/scheduler/begin_frame_source.h" | |
17 #include "cc/surfaces/display_scheduler.h" | |
18 #include "services/ui/surfaces/direct_output_surface.h" | |
19 #include "services/ui/surfaces/display_compositor.h" | |
20 | |
21 #if defined(USE_OZONE) | |
22 #include "gpu/command_buffer/client/gles2_interface.h" | |
23 #include "services/ui/surfaces/direct_output_surface_ozone.h" | |
24 #endif | |
25 | |
26 namespace ui { | |
27 namespace ws { | |
28 | |
29 GpuCompositorFrameSink::GpuCompositorFrameSink( | |
30 scoped_refptr<DisplayCompositor> display_compositor, | |
31 const cc::FrameSinkId& frame_sink_id, | |
32 gfx::AcceleratedWidget widget, | |
33 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
34 scoped_refptr<SurfacesContextProvider> context_provider, | |
35 cc::mojom::MojoCompositorFrameSinkRequest request, | |
36 cc::mojom::MojoCompositorFrameSinkClientPtr client) | |
37 : frame_sink_id_(frame_sink_id), | |
38 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
39 display_compositor_(display_compositor), | |
40 surface_factory_(frame_sink_id_, display_compositor_->manager(), this), | |
41 client_(std::move(client)), | |
42 binding_(this, std::move(request)) { | |
43 display_compositor_->manager()->RegisterFrameSinkId(frame_sink_id_); | |
44 display_compositor_->manager()->RegisterSurfaceFactoryClient(frame_sink_id_, | |
45 this); | |
46 | |
47 if (widget != gfx::kNullAcceleratedWidget) | |
48 InitDisplay(widget, gpu_memory_buffer_manager, std::move(context_provider)); | |
49 } | |
50 | |
51 GpuCompositorFrameSink::~GpuCompositorFrameSink() { | |
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_|'s resources early on. | |
55 surface_factory_.DestroyAll(); | |
56 display_compositor_->manager()->UnregisterSurfaceFactoryClient( | |
57 frame_sink_id_); | |
58 display_compositor_->manager()->InvalidateFrameSinkId(frame_sink_id_); | |
59 } | |
60 | |
61 void GpuCompositorFrameSink::SetNeedsBeginFrame(bool needs_begin_frame) { | |
62 needs_begin_frame_ = needs_begin_frame; | |
63 UpdateNeedsBeginFramesInternal(); | |
64 } | |
65 | |
66 void GpuCompositorFrameSink::SubmitCompositorFrame(cc::CompositorFrame frame) { | |
67 gfx::Size frame_size = frame.render_pass_list[0]->output_rect.size(); | |
68 // If the size of the CompostiorFrame has changed then destroy the existing | |
69 // Surface and create a new one of the appropriate size. | |
70 if (!local_frame_id_.is_valid() || frame_size != last_submitted_frame_size_) { | |
71 if (local_frame_id_.is_valid()) | |
72 surface_factory_.Destroy(local_frame_id_); | |
73 local_frame_id_ = surface_id_allocator_.GenerateId(); | |
74 surface_factory_.Create(local_frame_id_); | |
75 if (display_) | |
76 display_->Resize(frame_size); | |
77 } | |
78 ++ack_pending_count_; | |
79 surface_factory_.SubmitCompositorFrame( | |
80 local_frame_id_, std::move(frame), | |
81 base::Bind(&GpuCompositorFrameSink::DidReceiveCompositorFrameAck, | |
82 base::Unretained(this))); | |
83 if (display_) { | |
84 display_->SetLocalFrameId(local_frame_id_, | |
85 frame.metadata.device_scale_factor); | |
86 } | |
87 last_submitted_frame_size_ = frame_size; | |
88 } | |
89 | |
90 void GpuCompositorFrameSink::DidReceiveCompositorFrameAck() { | |
91 if (!client_) | |
92 return; | |
93 client_->DidReceiveCompositorFrameAck(); | |
94 DCHECK_GT(ack_pending_count_, 0); | |
95 if (!surface_returned_resources_.empty()) { | |
96 client_->ReclaimResources(surface_returned_resources_); | |
97 surface_returned_resources_.clear(); | |
98 } | |
99 ack_pending_count_--; | |
100 } | |
101 | |
102 void GpuCompositorFrameSink::AddChildFrameSink( | |
103 const cc::FrameSinkId& child_frame_sink_id) { | |
104 cc::SurfaceManager* surface_manager = display_compositor_->manager(); | |
105 surface_manager->RegisterFrameSinkHierarchy(frame_sink_id_, | |
106 child_frame_sink_id); | |
107 } | |
108 | |
109 void GpuCompositorFrameSink::RemoveChildFrameSink( | |
110 const cc::FrameSinkId& child_frame_sink_id) { | |
111 cc::SurfaceManager* surface_manager = display_compositor_->manager(); | |
112 surface_manager->UnregisterFrameSinkHierarchy(frame_sink_id_, | |
113 child_frame_sink_id); | |
114 } | |
115 | |
116 void GpuCompositorFrameSink::InitDisplay( | |
117 gfx::AcceleratedWidget widget, | |
118 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
119 scoped_refptr<SurfacesContextProvider> context_provider) { | |
120 // TODO(rjkroege): If there is something better to do than CHECK, add it. | |
121 CHECK(context_provider->BindToCurrentThread()); | |
122 | |
123 std::unique_ptr<cc::SyntheticBeginFrameSource> synthetic_begin_frame_source( | |
124 new cc::DelayBasedBeginFrameSource( | |
125 base::MakeUnique<cc::DelayBasedTimeSource>(task_runner_.get()))); | |
126 | |
127 std::unique_ptr<cc::OutputSurface> display_output_surface; | |
128 if (context_provider->ContextCapabilities().surfaceless) { | |
129 #if defined(USE_OZONE) | |
130 display_output_surface = base::MakeUnique<DirectOutputSurfaceOzone>( | |
131 context_provider, widget, synthetic_begin_frame_source.get(), | |
132 gpu_memory_buffer_manager, GL_TEXTURE_2D, GL_RGB); | |
133 #else | |
134 NOTREACHED(); | |
135 #endif | |
136 } else { | |
137 display_output_surface = base::MakeUnique<DirectOutputSurface>( | |
138 context_provider, synthetic_begin_frame_source.get()); | |
139 } | |
140 | |
141 int max_frames_pending = | |
142 display_output_surface->capabilities().max_frames_pending; | |
143 DCHECK_GT(max_frames_pending, 0); | |
144 | |
145 std::unique_ptr<cc::DisplayScheduler> scheduler( | |
146 new cc::DisplayScheduler(synthetic_begin_frame_source.get(), | |
147 task_runner_.get(), max_frames_pending)); | |
148 | |
149 display_.reset(new cc::Display( | |
150 nullptr /* bitmap_manager */, gpu_memory_buffer_manager, | |
151 cc::RendererSettings(), frame_sink_id_, | |
152 std::move(synthetic_begin_frame_source), | |
153 std::move(display_output_surface), std::move(scheduler), | |
154 base::MakeUnique<cc::TextureMailboxDeleter>(task_runner_.get()))); | |
155 display_->Initialize(this, display_compositor_->manager()); | |
156 display_->SetVisible(true); | |
157 } | |
158 | |
159 void GpuCompositorFrameSink::DisplayOutputSurfaceLost() {} | |
160 | |
161 void GpuCompositorFrameSink::DisplayWillDrawAndSwap( | |
162 bool will_draw_and_swap, | |
163 const cc::RenderPassList& render_passes) {} | |
164 | |
165 void GpuCompositorFrameSink::DisplayDidDrawAndSwap() {} | |
166 | |
167 void GpuCompositorFrameSink::ReturnResources( | |
168 const cc::ReturnedResourceArray& resources) { | |
169 if (resources.empty()) | |
170 return; | |
171 | |
172 if (!ack_pending_count_ && client_) { | |
173 client_->ReclaimResources(resources); | |
174 return; | |
175 } | |
176 | |
177 std::copy(resources.begin(), resources.end(), | |
178 std::back_inserter(surface_returned_resources_)); | |
179 } | |
180 | |
181 void GpuCompositorFrameSink::SetBeginFrameSource( | |
182 cc::BeginFrameSource* begin_frame_source) { | |
183 // TODO(tansell): Implement this. | |
184 if (begin_frame_source_ && added_frame_observer_) { | |
185 begin_frame_source_->RemoveObserver(this); | |
186 added_frame_observer_ = false; | |
187 } | |
188 begin_frame_source_ = begin_frame_source; | |
189 UpdateNeedsBeginFramesInternal(); | |
190 } | |
191 | |
192 void GpuCompositorFrameSink::OnBeginFrame(const cc::BeginFrameArgs& args) { | |
193 UpdateNeedsBeginFramesInternal(); | |
194 last_begin_frame_args_ = args; | |
195 if (client_) | |
196 client_->OnBeginFrame(args); | |
197 } | |
198 | |
199 const cc::BeginFrameArgs& GpuCompositorFrameSink::LastUsedBeginFrameArgs() | |
200 const { | |
201 return last_begin_frame_args_; | |
202 } | |
203 | |
204 void GpuCompositorFrameSink::OnBeginFrameSourcePausedChanged(bool paused) {} | |
205 | |
206 void GpuCompositorFrameSink::UpdateNeedsBeginFramesInternal() { | |
207 if (!begin_frame_source_) | |
208 return; | |
209 | |
210 if (needs_begin_frame_ == added_frame_observer_) | |
211 return; | |
212 | |
213 added_frame_observer_ = needs_begin_frame_; | |
214 if (needs_begin_frame_) | |
215 begin_frame_source_->AddObserver(this); | |
216 else | |
217 begin_frame_source_->RemoveObserver(this); | |
218 } | |
219 | |
220 } // namespace ws | |
221 } // namespace ui | |
OLD | NEW |