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 #ifndef COMPONENTS_VIZ_SERVICE_MAIN_VIZ_MAIN_IMPL_H_
6 #define COMPONENTS_VIZ_SERVICE_MAIN_VIZ_MAIN_IMPL_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread.h"
14 #include "build/build_config.h"
15 #include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
16 #include "components/viz/service/main/viz_compositor_thread_runner_impl.h"
17 #include "gpu/ipc/gpu_in_process_thread_service.h"
18 #include "gpu/ipc/in_process_command_buffer.h"
19 #include "mojo/public/cpp/bindings/associated_receiver_set.h"
20 #include "mojo/public/cpp/bindings/pending_associated_receiver.h"
21 #include "mojo/public/cpp/bindings/pending_receiver.h"
22 #include "mojo/public/cpp/bindings/pending_remote.h"
23 #include "mojo/public/cpp/bindings/receiver.h"
24 #include "services/viz/privileged/mojom/gl/gpu_service.mojom.h"
25 #include "services/viz/privileged/mojom/viz_main.mojom.h"
26 #include "ui/gfx/font_render_params.h"
27 
28 namespace base {
29 class PowerMonitorSource;
30 }
31 
32 namespace gpu {
33 class GpuInit;
34 class SyncPointManager;
35 class SharedImageManager;
36 }  // namespace gpu
37 
38 namespace ukm {
39 class MojoUkmRecorder;
40 }
41 
42 namespace viz {
43 class GpuServiceImpl;
44 
45 class VizMainImpl : public mojom::VizMain,
46                     public gpu::GpuInProcessThreadServiceDelegate {
47  public:
48   class Delegate {
49    public:
50     virtual ~Delegate() = default;
51 
52     virtual void OnInitializationFailed() = 0;
53     virtual void OnGpuServiceConnection(GpuServiceImpl* gpu_service) = 0;
54     virtual void PostCompositorThreadCreated(
55         base::SingleThreadTaskRunner* task_runner) = 0;
56     virtual void QuitMainMessageLoop() = 0;
57   };
58 
59   struct ExternalDependencies {
60    public:
61     ExternalDependencies();
62     ExternalDependencies(ExternalDependencies&& other);
63     ~ExternalDependencies();
64 
65     ExternalDependencies& operator=(ExternalDependencies&& other);
66 
67     // Note that, due to the design of |base::PowerMonitor|, it is inherently
68     // racy to decide to initialize or not based on a call to
69     // |base::PowerMonitor::IsInitialized|. This makes it difficult for
70     // VizMainImpl to know whether to call initialize or not as the correct
71     // choice depends on the context in which VizMainImpl will be used.
72     //
73     // To work around this, calling code must understand whether VizMainImpl
74     // should initialize |base::PowerMonitor| or not and can then use the
75     // |power_monitor_source| to signal its intent.
76     //
77     // If |power_monitor_source| is null, |PowerMonitor::Initialize| will not
78     // be called. If |power_monitor_source| is non-null, it will be std::move'd
79     // in to a call of |PowerMonitor::Initialize|.
80     //
81     // We use a |PowerMonitorSource| here instead of a boolean flag so that
82     // tests can use mocks and fakes for testing.
83     mutable std::unique_ptr<base::PowerMonitorSource> power_monitor_source;
84     gpu::SyncPointManager* sync_point_manager = nullptr;
85     gpu::SharedImageManager* shared_image_manager = nullptr;
86     base::WaitableEvent* shutdown_event = nullptr;
87     scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner;
88     std::unique_ptr<ukm::MojoUkmRecorder> ukm_recorder;
89     VizCompositorThreadRunner* viz_compositor_thread_runner = nullptr;
90 
91    private:
92     DISALLOW_COPY_AND_ASSIGN(ExternalDependencies);
93   };
94 
95   VizMainImpl(Delegate* delegate,
96               ExternalDependencies dependencies,
97               std::unique_ptr<gpu::GpuInit> gpu_init);
98   // Destruction must happen on the GPU thread.
99   ~VizMainImpl() override;
100 
101   void BindAssociated(
102       mojo::PendingAssociatedReceiver<mojom::VizMain> pending_receiver);
103 
104   // mojom::VizMain implementation:
105   void CreateGpuService(
106       mojo::PendingReceiver<mojom::GpuService> pending_receiver,
107       mojo::PendingRemote<mojom::GpuHost> pending_gpu_host,
108       mojo::PendingRemote<
109           discardable_memory::mojom::DiscardableSharedMemoryManager>
110           discardable_memory_manager,
111       mojo::ScopedSharedBufferHandle activity_flags,
112       gfx::FontRenderParams::SubpixelRendering subpixel_rendering) override;
113   void CreateFrameSinkManager(mojom::FrameSinkManagerParamsPtr params) override;
114   void CreateVizDevTools(mojom::VizDevToolsParamsPtr params) override;
115 
116   // gpu::GpuInProcessThreadServiceDelegate implementation:
117   scoped_refptr<gpu::SharedContextState> GetSharedContextState() override;
118   scoped_refptr<gl::GLShareGroup> GetShareGroup() override;
119 
gpu_service()120   GpuServiceImpl* gpu_service() { return gpu_service_.get(); }
gpu_service()121   const GpuServiceImpl* gpu_service() const { return gpu_service_.get(); }
122 
123   // Note that this may be null if viz is running in the browser process and
124   // using the ServiceDiscardableSharedMemoryManager.
125   discardable_memory::ClientDiscardableSharedMemoryManager*
discardable_shared_memory_manager()126   discardable_shared_memory_manager() {
127     return discardable_shared_memory_manager_.get();
128   }
129 
130   // Cleanly exits the process.
131   void ExitProcess(bool immediately);
132 
viz_compositor_thread_runner()133   VizCompositorThreadRunner* viz_compositor_thread_runner() {
134     return viz_compositor_thread_runner_;
135   }
136 
137  private:
138   void CreateFrameSinkManagerInternal(mojom::FrameSinkManagerParamsPtr params);
139 
io_task_runner()140   scoped_refptr<base::SingleThreadTaskRunner> io_task_runner() const {
141     return io_thread_ ? io_thread_->task_runner()
142                       : dependencies_.io_thread_task_runner;
143   }
144 
145   Delegate* const delegate_;
146 
147   const ExternalDependencies dependencies_;
148 
149   // The thread that handles IO events for Gpu (if one isn't already provided).
150   // |io_thread_| must be ordered above GPU service related variables so it's
151   // destroyed after they are.
152   std::unique_ptr<base::Thread> io_thread_;
153 
154   std::unique_ptr<gpu::GpuInit> gpu_init_;
155   std::unique_ptr<GpuServiceImpl> gpu_service_;
156 
157   // Allows the display compositor to use InProcessCommandBuffer to send GPU
158   // commands to the GPU thread from the compositor thread. This must outlive
159   // |viz_compositor_thread_runner_|.
160   std::unique_ptr<gpu::CommandBufferTaskExecutor> task_executor_;
161 
162   // If the gpu service is not yet ready then we stash pending
163   // FrameSinkManagerParams.
164   mojom::FrameSinkManagerParamsPtr pending_frame_sink_manager_params_;
165 
166   // Runs the VizCompositorThread for the display compositor.
167   std::unique_ptr<VizCompositorThreadRunnerImpl>
168       viz_compositor_thread_runner_impl_;
169   // Note under Android WebView where VizCompositorThreadRunner is not created
170   // and owned by this, Viz does not interact with other objects in this class,
171   // such as GpuServiceImpl or CommandBufferTaskExecutor. Code should take care
172   // to avoid introducing such assumptions.
173   VizCompositorThreadRunner* viz_compositor_thread_runner_ = nullptr;
174 
175   const scoped_refptr<base::SingleThreadTaskRunner> gpu_thread_task_runner_;
176 
177   mojo::AssociatedReceiver<mojom::VizMain> receiver_{this};
178 
179   std::unique_ptr<discardable_memory::ClientDiscardableSharedMemoryManager>
180       discardable_shared_memory_manager_;
181 
182   DISALLOW_COPY_AND_ASSIGN(VizMainImpl);
183 };
184 
185 }  // namespace viz
186 
187 #endif  // COMPONENTS_VIZ_SERVICE_MAIN_VIZ_MAIN_IMPL_H_
188