Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 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 CONTENT_GPU_GPU_CHILD_THREAD_H_ |
| 6 | #define CONTENT_GPU_GPU_CHILD_THREAD_H_ |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 7 | |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 10 | #include <memory> |
Ken Rockot | b2928f3 | 2019-03-12 04:43:03 | [diff] [blame] | 11 | #include <vector> |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 12 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 13 | #include "base/command_line.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 14 | #include "base/functional/callback.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
ben | d623444 | 2017-04-24 22:51:13 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 17 | #include "base/task/single_thread_task_runner.h" |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 18 | #include "base/task/task_observer.h" |
[email protected] | abb52216 | 2013-06-28 01:54:16 | [diff] [blame] | 19 | #include "base/time/time.h" |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 20 | #include "build/build_config.h" |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 21 | #include "components/performance_manager/scenario_api/performance_scenarios.h" |
Sadrul Habib Chowdhury | ef1abe78 | 2017-08-01 17:20:38 | [diff] [blame] | 22 | #include "components/viz/service/gl/gpu_service_impl.h" |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 23 | #include "components/viz/service/main/viz_main_impl.h" |
jam | 8a021512a | 2015-02-03 18:16:08 | [diff] [blame] | 24 | #include "content/child/child_thread_impl.h" |
ericrk | 41a1579e | 2017-02-10 20:56:28 | [diff] [blame] | 25 | #include "gpu/config/gpu_feature_info.h" |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 26 | #include "gpu/config/gpu_info.h" |
Jonathan Backer | 0af50996 | 2018-05-30 16:05:07 | [diff] [blame] | 27 | #include "gpu/config/gpu_preferences.h" |
markdittmer | d88b835 | 2016-04-08 15:28:45 | [diff] [blame] | 28 | #include "gpu/ipc/service/gpu_channel.h" |
| 29 | #include "gpu/ipc/service/gpu_channel_manager.h" |
| 30 | #include "gpu/ipc/service/gpu_channel_manager_delegate.h" |
| 31 | #include "gpu/ipc/service/gpu_config.h" |
| 32 | #include "gpu/ipc/service/x_util.h" |
liberato | 441ca70 | 2017-05-13 16:50:38 | [diff] [blame] | 33 | #include "media/base/android_overlay_mojo_factory.h" |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 34 | |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 35 | namespace base::sequence_manager { |
| 36 | class SequenceManager; |
| 37 | } // namespace base::sequence_manager |
| 38 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 39 | namespace content { |
ben | 507aa81 | 2016-08-10 23:26:19 | [diff] [blame] | 40 | class GpuServiceFactory; |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 41 | |
| 42 | // The main thread of the GPU child process. There will only ever be one of |
| 43 | // these per process. It does process initialization and shutdown. It forwards |
markdittmer | d88b835 | 2016-04-08 15:28:45 | [diff] [blame] | 44 | // IPC messages to gpu::GpuChannelManager, which is responsible for issuing |
| 45 | // rendering commands to the GPU. |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 46 | class GpuChildThread : public ChildThreadImpl, |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 47 | public viz::VizMainImpl::Delegate, |
| 48 | public base::TaskObserver { |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 49 | public: |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 50 | GpuChildThread(base::RepeatingClosure quit_closure, |
Dale Curtis | 1b6becebb | 2020-03-30 20:13:35 | [diff] [blame] | 51 | std::unique_ptr<gpu::GpuInit> gpu_init); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 52 | |
sadrul | 3f8d0fa | 2016-09-08 17:47:41 | [diff] [blame] | 53 | GpuChildThread(const InProcessChildThreadParams& params, |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 54 | std::unique_ptr<gpu::GpuInit> gpu_init); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 55 | |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 56 | GpuChildThread(const GpuChildThread&) = delete; |
| 57 | GpuChildThread& operator=(const GpuChildThread&) = delete; |
| 58 | |
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 59 | ~GpuChildThread() override; |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 60 | |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 61 | void Init( |
| 62 | const base::TimeTicks& process_start_time, |
| 63 | base::sequence_manager::SequenceManager* sequence_manager = nullptr); |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 64 | |
penghuang | 7404df988 | 2016-02-29 23:07:29 | [diff] [blame] | 65 | private: |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 66 | GpuChildThread(base::RepeatingClosure quit_closure, |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 67 | ChildThreadImpl::Options options, |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 68 | std::unique_ptr<gpu::GpuInit> gpu_init); |
sadrul | 041ca72 | 2017-03-24 22:35:35 | [diff] [blame] | 69 | |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 70 | bool in_process_gpu() const; |
| 71 | |
Ken Rockot | b2928f3 | 2019-03-12 04:43:03 | [diff] [blame] | 72 | // ChildThreadImpl: |
Ken Rockot | 8322a04 | 2019-08-20 18:02:00 | [diff] [blame] | 73 | void BindServiceInterface(mojo::GenericPendingReceiver receiver) override; |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 74 | |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 75 | // viz::VizMainImpl::Delegate: |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 76 | void OnInitializationFailed() override; |
| 77 | void OnGpuServiceConnection(viz::GpuServiceImpl* gpu_service) override; |
Sadrul Habib Chowdhury | 97c6f1f | 2018-03-02 01:21:16 | [diff] [blame] | 78 | void PostCompositorThreadCreated( |
| 79 | base::SingleThreadTaskRunner* task_runner) override; |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 80 | void QuitMainMessageLoop() override; |
fsamuel | a660ce0 | 2016-02-25 03:56:16 | [diff] [blame] | 81 | |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 82 | // base::TaskObserver: |
| 83 | void WillProcessTask(const base::PendingTask& pending_task, |
| 84 | bool was_blocked_or_low_priority) override; |
| 85 | void DidProcessTask(const base::PendingTask& pending_task) override {} |
| 86 | |
Patrick Monette | 845ec83 | 2025-10-07 18:23:23 | [diff] [blame] | 87 | void OnMemoryPressure(base::MemoryPressureLevel level); |
Khushal | 0aac6207 | 2018-06-01 19:33:17 | [diff] [blame] | 88 | |
Eric Karl | 23af711f | 2018-11-02 20:47:34 | [diff] [blame] | 89 | // Returns a closure which calls into the VizMainImpl to perform shutdown |
| 90 | // before quitting the main message loop. Must be called on the main thread. |
| 91 | static base::RepeatingClosure MakeQuitSafelyClosure(); |
| 92 | static void QuitSafelyHelper( |
| 93 | scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 94 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 95 | #if BUILDFLAG(IS_ANDROID) |
liberato | 441ca70 | 2017-05-13 16:50:38 | [diff] [blame] | 96 | static std::unique_ptr<media::AndroidOverlay> CreateAndroidOverlay( |
Chris Watkins | 88a4000 | 2017-09-28 01:16:42 | [diff] [blame] | 97 | scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
liberato | 441ca70 | 2017-05-13 16:50:38 | [diff] [blame] | 98 | const base::UnguessableToken& routing_token, |
| 99 | media::AndroidOverlayConfig); |
| 100 | #endif |
| 101 | |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 102 | viz::VizMainImpl viz_main_; |
[email protected] | ec4bda6 | 2013-06-14 15:51:03 | [diff] [blame] | 103 | |
Ken Rockot | a292417 | 2019-12-18 03:27:05 | [diff] [blame] | 104 | // ServiceFactory for Mojo service hosting. |
ben | 507aa81 | 2016-08-10 23:26:19 | [diff] [blame] | 105 | std::unique_ptr<GpuServiceFactory> service_factory_; |
rockot | 4a93a86 | 2016-07-07 20:50:14 | [diff] [blame] | 106 | |
Ken Rockot | a292417 | 2019-12-18 03:27:05 | [diff] [blame] | 107 | // A queue of incoming service interface requests received prior to |
| 108 | // |service_factory_| initialization. |
| 109 | std::vector<mojo::GenericPendingReceiver> pending_service_receivers_; |
| 110 | |
Eric Karl | 23af711f | 2018-11-02 20:47:34 | [diff] [blame] | 111 | // A closure which quits the main message loop. |
| 112 | base::RepeatingClosure quit_closure_; |
| 113 | |
Patrick Monette | cc99f49 | 2025-10-08 20:22:00 | [diff] [blame] | 114 | std::unique_ptr<base::AsyncMemoryPressureListenerRegistration> |
| 115 | memory_pressure_listener_registration_; |
Khushal | 0aac6207 | 2018-06-01 19:33:17 | [diff] [blame] | 116 | |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 117 | performance_scenarios::InputScenario last_input_scenario_; |
| 118 | |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 119 | base::WeakPtrFactory<GpuChildThread> weak_factory_{this}; |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 120 | }; |
| 121 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 122 | } // namespace content |
| 123 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 124 | #endif // CONTENT_GPU_GPU_CHILD_THREAD_H_ |