OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/client/support/compositor/blimp_embedder_compositor.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/lazy_instance.h" | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/threading/thread_task_runner_handle.h" | |
12 #include "blimp/client/public/compositor/compositor_dependencies.h" | |
13 #include "blimp/client/support/compositor/blimp_context_provider.h" | |
14 #include "cc/animation/animation_host.h" | |
15 #include "cc/layers/layer.h" | |
16 #include "cc/output/output_surface.h" | |
17 #include "cc/output/output_surface_frame.h" | |
18 #include "cc/output/texture_mailbox_deleter.h" | |
19 #include "cc/raster/single_thread_task_graph_runner.h" | |
20 #include "cc/surfaces/direct_compositor_frame_sink.h" | |
21 #include "cc/surfaces/display.h" | |
22 #include "cc/surfaces/surface_id_allocator.h" | |
23 #include "cc/surfaces/surface_manager.h" | |
24 #include "cc/trees/layer_tree_host_in_process.h" | |
25 #include "gpu/command_buffer/client/context_support.h" | |
26 #include "gpu/command_buffer/client/gles2_lib.h" | |
27 | |
28 namespace blimp { | |
29 namespace client { | |
30 | |
31 namespace { | |
32 | |
33 class SimpleTaskGraphRunner : public cc::SingleThreadTaskGraphRunner { | |
34 public: | |
35 SimpleTaskGraphRunner() { | |
36 Start("BlimpBrowserCompositorWorker", | |
37 base::SimpleThread::Options(base::ThreadPriority::BACKGROUND)); | |
38 } | |
39 | |
40 ~SimpleTaskGraphRunner() override { Shutdown(); } | |
41 }; | |
42 | |
43 class DisplayOutputSurface : public cc::OutputSurface { | |
44 public: | |
45 explicit DisplayOutputSurface( | |
46 scoped_refptr<cc::ContextProvider> context_provider, | |
47 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
48 : cc::OutputSurface(std::move(context_provider)), | |
49 task_runner_(std::move(task_runner)), | |
50 weak_ptr_factory_(this) {} | |
51 | |
52 ~DisplayOutputSurface() override = default; | |
53 | |
54 // cc::OutputSurface implementation | |
55 void BindToClient(cc::OutputSurfaceClient* client) override { | |
56 client_ = client; | |
57 } | |
58 | |
59 void EnsureBackbuffer() override {} | |
60 void DiscardBackbuffer() override { | |
61 context_provider()->ContextGL()->DiscardBackbufferCHROMIUM(); | |
62 } | |
63 void BindFramebuffer() override { | |
64 context_provider()->ContextGL()->BindFramebuffer(GL_FRAMEBUFFER, 0); | |
65 } | |
66 void Reshape(const gfx::Size& size, | |
67 float device_scale_factor, | |
68 const gfx::ColorSpace& color_space, | |
69 bool has_alpha) override { | |
70 context_provider()->ContextGL()->ResizeCHROMIUM( | |
71 size.width(), size.height(), device_scale_factor, has_alpha); | |
72 } | |
73 void SwapBuffers(cc::OutputSurfaceFrame frame) override { | |
74 context_provider_->ContextSupport()->Swap(); | |
75 // The ack for SwapBuffers must be run asynchronously, that will be | |
76 // satisfied since we will go through a PostTask here. | |
77 task_runner_->PostTask( | |
78 FROM_HERE, base::Bind(&DisplayOutputSurface::SwapBuffersCallback, | |
79 weak_ptr_factory_.GetWeakPtr())); | |
80 } | |
81 cc::OverlayCandidateValidator* GetOverlayCandidateValidator() const override { | |
82 return nullptr; | |
83 } | |
84 bool IsDisplayedAsOverlayPlane() const override { return false; } | |
85 unsigned GetOverlayTextureId() const override { return 0; } | |
86 bool SurfaceIsSuspendForRecycle() const override { return false; } | |
87 uint32_t GetFramebufferCopyTextureFormat() override { | |
88 // We assume we have an alpha channel from the BlimpContextProvider, so use | |
89 // GL_RGBA here. | |
90 return GL_RGBA; | |
91 } | |
92 bool HasExternalStencilTest() const override { return false; } | |
93 void ApplyExternalStencil() override {} | |
94 | |
95 private: | |
96 void SwapBuffersCallback() { client_->DidReceiveSwapBuffersAck(); } | |
97 | |
98 cc::OutputSurfaceClient* client_ = nullptr; | |
99 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
100 base::WeakPtrFactory<DisplayOutputSurface> weak_ptr_factory_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(DisplayOutputSurface); | |
103 }; | |
104 | |
105 base::LazyInstance<SimpleTaskGraphRunner> g_task_graph_runner = | |
106 LAZY_INSTANCE_INITIALIZER; | |
107 | |
108 } // namespace | |
109 | |
110 BlimpEmbedderCompositor::BlimpEmbedderCompositor( | |
111 CompositorDependencies* compositor_dependencies) | |
112 : compositor_dependencies_(compositor_dependencies), | |
113 frame_sink_id_(compositor_dependencies->AllocateFrameSinkId()), | |
114 compositor_frame_sink_request_pending_(false), | |
115 root_layer_(cc::Layer::Create()) { | |
116 compositor_dependencies_->GetSurfaceManager()->RegisterFrameSinkId( | |
117 frame_sink_id_); | |
118 | |
119 animation_host_ = cc::AnimationHost::CreateMainInstance(); | |
120 | |
121 cc::LayerTreeHostInProcess::InitParams params; | |
122 params.client = this; | |
123 params.task_graph_runner = g_task_graph_runner.Pointer(); | |
124 cc::LayerTreeSettings settings; | |
125 params.settings = &settings; | |
126 params.main_task_runner = base::ThreadTaskRunnerHandle::Get(); | |
127 params.mutator_host = animation_host_.get(); | |
128 host_ = cc::LayerTreeHostInProcess::CreateSingleThreaded(this, ¶ms); | |
129 | |
130 root_layer_->SetBackgroundColor(SK_ColorWHITE); | |
131 host_->GetLayerTree()->SetRootLayer(root_layer_); | |
132 host_->SetFrameSinkId(frame_sink_id_); | |
133 host_->SetVisible(true); | |
134 } | |
135 | |
136 BlimpEmbedderCompositor::~BlimpEmbedderCompositor() { | |
137 SetContextProvider(nullptr); | |
138 compositor_dependencies_->GetSurfaceManager()->InvalidateFrameSinkId( | |
139 frame_sink_id_); | |
140 } | |
141 | |
142 void BlimpEmbedderCompositor::SetContentLayer( | |
143 scoped_refptr<cc::Layer> content_layer) { | |
144 root_layer_->RemoveAllChildren(); | |
145 root_layer_->AddChild(content_layer); | |
146 } | |
147 | |
148 void BlimpEmbedderCompositor::SetSize(const gfx::Size& size_in_px) { | |
149 viewport_size_in_px_ = size_in_px; | |
150 | |
151 // Update the host. | |
152 host_->GetLayerTree()->SetViewportSize(viewport_size_in_px_); | |
153 root_layer_->SetBounds(viewport_size_in_px_); | |
154 | |
155 // Update the display. | |
156 if (display_) { | |
157 display_->Resize(viewport_size_in_px_); | |
158 } | |
159 } | |
160 | |
161 void BlimpEmbedderCompositor::SetContextProvider( | |
162 scoped_refptr<cc::ContextProvider> context_provider) { | |
163 if (context_provider_) { | |
164 DCHECK(host_->IsVisible()); | |
165 host_->SetVisible(false); | |
166 host_->ReleaseCompositorFrameSink(); | |
167 display_.reset(); | |
168 } | |
169 | |
170 context_provider_ = std::move(context_provider); | |
171 | |
172 if (context_provider_) { | |
173 host_->SetVisible(true); | |
174 if (compositor_frame_sink_request_pending_) { | |
175 HandlePendingCompositorFrameSinkRequest(); | |
176 } | |
177 } | |
178 } | |
179 | |
180 void BlimpEmbedderCompositor::RequestNewCompositorFrameSink() { | |
181 DCHECK(!compositor_frame_sink_request_pending_) | |
182 << "We already have a pending request?"; | |
183 compositor_frame_sink_request_pending_ = true; | |
184 HandlePendingCompositorFrameSinkRequest(); | |
185 } | |
186 | |
187 void BlimpEmbedderCompositor::DidInitializeCompositorFrameSink() { | |
188 compositor_frame_sink_request_pending_ = false; | |
189 } | |
190 | |
191 void BlimpEmbedderCompositor::DidFailToInitializeCompositorFrameSink() { | |
192 NOTREACHED() << "Can't fail to initialize the CompositorFrameSink here"; | |
193 } | |
194 | |
195 void BlimpEmbedderCompositor::HandlePendingCompositorFrameSinkRequest() { | |
196 DCHECK(compositor_frame_sink_request_pending_); | |
197 | |
198 // Can't handle the request right now since we don't have a widget. | |
199 if (!host_->IsVisible()) | |
200 return; | |
201 | |
202 DCHECK(context_provider_); | |
203 context_provider_->BindToCurrentThread(); | |
204 | |
205 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager = | |
206 compositor_dependencies_->GetGpuMemoryBufferManager(); | |
207 cc::SharedBitmapManager* shared_bitmap_manager = nullptr; | |
208 | |
209 auto task_runner = base::ThreadTaskRunnerHandle::Get(); | |
210 auto display_output_surface = | |
211 base::MakeUnique<DisplayOutputSurface>(context_provider_, task_runner); | |
212 | |
213 begin_frame_source_.reset(new cc::DelayBasedBeginFrameSource( | |
214 base::MakeUnique<cc::DelayBasedTimeSource>(task_runner.get()))); | |
215 std::unique_ptr<cc::DisplayScheduler> scheduler(new cc::DisplayScheduler( | |
216 task_runner.get(), | |
217 display_output_surface->capabilities().max_frames_pending)); | |
218 | |
219 display_ = base::MakeUnique<cc::Display>( | |
220 shared_bitmap_manager, gpu_memory_buffer_manager, | |
221 host_->GetSettings().renderer_settings, frame_sink_id_, | |
222 begin_frame_source_.get(), std::move(display_output_surface), | |
223 std::move(scheduler), | |
224 base::MakeUnique<cc::TextureMailboxDeleter>(task_runner.get())); | |
225 display_->SetVisible(true); | |
226 display_->Resize(viewport_size_in_px_); | |
227 | |
228 // The Browser compositor and display share the same context provider. | |
229 auto compositor_frame_sink = base::MakeUnique<cc::DirectCompositorFrameSink>( | |
230 frame_sink_id_, compositor_dependencies_->GetSurfaceManager(), | |
231 display_.get(), context_provider_, nullptr, gpu_memory_buffer_manager, | |
232 shared_bitmap_manager); | |
233 | |
234 host_->SetCompositorFrameSink(std::move(compositor_frame_sink)); | |
235 } | |
236 | |
237 } // namespace client | |
238 } // namespace blimp | |
OLD | NEW |