Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [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 | |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 5 | #include "content/gpu/gpu_child_thread.h" |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 6 | |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 7 | #include <stddef.h> |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 8 | |
| 9 | #include <memory> |
dcheng | f63a125 | 2015-12-26 20:43:13 | [diff] [blame] | 10 | #include <utility> |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 11 | |
kylechar | 150e09e1 | 2017-10-19 13:57:52 | [diff] [blame] | 12 | #include "base/command_line.h" |
kylechar | 4664a16 | 2020-09-15 03:19:34 | [diff] [blame] | 13 | #include "base/debug/dump_without_crashing.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 14 | #include "base/functional/bind.h" |
| 15 | #include "base/functional/callback_helpers.h" |
ben | d623444 | 2017-04-24 22:51:13 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
Tom McKee | 1467915 | 2020-01-20 16:06:52 | [diff] [blame] | 17 | #include "base/power_monitor/power_monitor.h" |
| 18 | #include "base/power_monitor/power_monitor_device_source.h" |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 19 | #include "base/run_loop.h" |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 20 | #include "base/task/sequence_manager/sequence_manager.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 21 | #include "base/task/sequenced_task_runner.h" |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 22 | #include "base/task/single_thread_task_runner.h" |
ben | d623444 | 2017-04-24 22:51:13 | [diff] [blame] | 23 | #include "base/threading/thread_checker.h" |
[email protected] | 274aa588 | 2010-07-15 21:12:23 | [diff] [blame] | 24 | #include "build/build_config.h" |
[email protected] | 10208ea | 2013-06-06 20:08:03 | [diff] [blame] | 25 | #include "content/child/child_process.h" |
Oksana Zhuravlova | 0465118 | 2021-03-02 20:52:20 | [diff] [blame] | 26 | #include "content/common/process_visibility_tracker.h" |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 27 | #include "content/gpu/browser_exposed_gpu_interfaces.h" |
ben | 507aa81 | 2016-08-10 23:26:19 | [diff] [blame] | 28 | #include "content/gpu/gpu_service_factory.h" |
[email protected] | 085170ca | 2012-05-17 20:27:28 | [diff] [blame] | 29 | #include "content/public/common/content_client.h" |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 30 | #include "content/public/common/content_features.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 31 | #include "content/public/common/content_switches.h" |
kcwu | 08377ad | 2016-02-18 19:12:42 | [diff] [blame] | 32 | #include "content/public/gpu/content_gpu_client.h" |
Bo Liu | 01d003e4e | 2023-08-16 19:46:50 | [diff] [blame] | 33 | #include "gpu/command_buffer/common/shm_count.h" |
Dan Sanders | 0134bec | 2018-04-17 20:12:12 | [diff] [blame] | 34 | #include "gpu/ipc/service/gpu_channel_manager.h" |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 35 | #include "gpu/ipc/service/gpu_init.h" |
sadrul | 2fb7e15 | 2016-08-30 05:21:45 | [diff] [blame] | 36 | #include "gpu/ipc/service/gpu_watchdog_thread.h" |
xhwang | b2402d9 | 2016-10-15 07:29:37 | [diff] [blame] | 37 | #include "media/gpu/ipc/service/media_gpu_channel_manager.h" |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 38 | #include "mojo/public/cpp/bindings/binder_map.h" |
Miyoung Shin | 18de6bf | 2019-10-17 07:06:24 | [diff] [blame] | 39 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
| 40 | #include "mojo/public/cpp/bindings/pending_remote.h" |
kylechar | 4664a16 | 2020-09-15 03:19:34 | [diff] [blame] | 41 | #include "mojo/public/cpp/bindings/scoped_message_error_crash_key.h" |
| 42 | #include "mojo/public/cpp/system/functions.h" |
Ken Rockot | 47c47d86 | 2019-09-26 17:38:07 | [diff] [blame] | 43 | #include "services/metrics/public/cpp/mojo_ukm_recorder.h" |
| 44 | #include "services/metrics/public/mojom/ukm_interface.mojom.h" |
ben | d623444 | 2017-04-24 22:51:13 | [diff] [blame] | 45 | #include "services/service_manager/public/cpp/binder_registry.h" |
Miyoung Shin | 2be27f5 | 2019-07-27 15:35:39 | [diff] [blame] | 46 | #include "services/viz/privileged/mojom/gl/gpu_service.mojom.h" |
Khushal | 0aac6207 | 2018-06-01 19:33:17 | [diff] [blame] | 47 | #include "third_party/skia/include/core/SkGraphics.h" |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 48 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 49 | #if BUILDFLAG(IS_ANDROID) |
chcunningham | a4a8efb | 2017-03-22 16:55:02 | [diff] [blame] | 50 | #include "media/base/android/media_drm_bridge_client.h" |
liberato | 441ca70 | 2017-05-13 16:50:38 | [diff] [blame] | 51 | #include "media/mojo/clients/mojo_android_overlay.h" |
watk | ad8743d | 2016-04-27 20:27:49 | [diff] [blame] | 52 | #endif |
| 53 | |
Georg Neis | 18fc514 | 2024-12-18 00:58:41 | [diff] [blame] | 54 | #if BUILDFLAG(IS_CHROMEOS) |
Eric Sum | c8607e7 | 2022-02-18 00:48:03 | [diff] [blame] | 55 | #include "components/services/font/public/cpp/font_loader.h" // nogncheck |
| 56 | #include "components/services/font/public/mojom/font_service.mojom.h" // nogncheck |
| 57 | #include "third_party/skia/include/core/SkRefCnt.h" |
| 58 | #include "third_party/skia/include/ports/SkFontConfigInterface.h" |
| 59 | #endif |
| 60 | |
Matthew Denton | 66b2fa5 | 2023-10-17 00:54:22 | [diff] [blame] | 61 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
| 62 | #include "content/child/sandboxed_process_thread_type_handler.h" |
| 63 | #endif |
| 64 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 65 | namespace content { |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 66 | namespace { |
| 67 | |
kylechar | 4664a16 | 2020-09-15 03:19:34 | [diff] [blame] | 68 | // Called when the GPU process receives a bad IPC message. |
| 69 | void HandleBadMessage(const std::string& error) { |
| 70 | LOG(ERROR) << "Mojo error in GPU process: " << error; |
| 71 | mojo::debug::ScopedMessageErrorCrashKey crash_key_value(error); |
| 72 | base::debug::DumpWithoutCrashing(); |
| 73 | } |
| 74 | |
Ken Rockot | 2d483bc | 2021-06-17 22:39:02 | [diff] [blame] | 75 | ChildThreadImpl::Options GetOptions( |
| 76 | const InProcessChildThreadParams* in_process_params = nullptr) { |
skyostil | f28ccd0b | 2015-03-04 20:20:46 | [diff] [blame] | 77 | ChildThreadImpl::Options::Builder builder; |
wittman | 757af87 | 2016-08-16 00:13:46 | [diff] [blame] | 78 | builder.ConnectToBrowser(true); |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 79 | builder.ExposesInterfacesToBrowser(); |
Ken Rockot | 2d483bc | 2021-06-17 22:39:02 | [diff] [blame] | 80 | if (in_process_params) |
| 81 | builder.InBrowserProcess(*in_process_params); |
skyostil | f28ccd0b | 2015-03-04 20:20:46 | [diff] [blame] | 82 | return builder.Build(); |
alexst | 6154ce77 | 2015-01-21 12:41:41 | [diff] [blame] | 83 | } |
| 84 | |
Ken Rockot | 47c47d86 | 2019-09-26 17:38:07 | [diff] [blame] | 85 | viz::VizMainImpl::ExternalDependencies CreateVizMainDependencies() { |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 86 | viz::VizMainImpl::ExternalDependencies deps; |
Helmut Januschka | bcf7321 | 2024-09-03 23:49:18 | [diff] [blame] | 87 | if (!base::PowerMonitor::GetInstance()->IsInitialized()) { |
Tom McKee | 1467915 | 2020-01-20 16:06:52 | [diff] [blame] | 88 | deps.power_monitor_source = |
| 89 | std::make_unique<base::PowerMonitorDeviceSource>(); |
| 90 | } |
Colin Blundell | 7eb345b | 2024-11-05 13:20:14 | [diff] [blame] | 91 | |
| 92 | #if BUILDFLAG(IS_ANDROID) |
Peng Huang | e3b8c1d | 2019-03-05 17:21:12 | [diff] [blame] | 93 | if (GetContentClient()->gpu()) { |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 94 | deps.sync_point_manager = GetContentClient()->gpu()->GetSyncPointManager(); |
Peng Huang | e3b8c1d | 2019-03-05 17:21:12 | [diff] [blame] | 95 | deps.shared_image_manager = |
| 96 | GetContentClient()->gpu()->GetSharedImageManager(); |
Bo Liu | eca0009f | 2022-08-25 22:59:32 | [diff] [blame] | 97 | deps.scheduler = GetContentClient()->gpu()->GetScheduler(); |
Bo Liu | a7b2ce6 | 2019-08-02 22:16:23 | [diff] [blame] | 98 | deps.viz_compositor_thread_runner = |
| 99 | GetContentClient()->gpu()->GetVizCompositorThreadRunner(); |
Alex Mitra | c246bcc | 2024-11-29 16:22:10 | [diff] [blame] | 100 | deps.gr_context_options_provider = |
| 101 | GetContentClient()->gpu()->GetGrContextOptionsProvider(); |
Peng Huang | e3b8c1d | 2019-03-05 17:21:12 | [diff] [blame] | 102 | } |
Colin Blundell | 7eb345b | 2024-11-05 13:20:14 | [diff] [blame] | 103 | #endif |
| 104 | |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 105 | auto* process = ChildProcess::current(); |
| 106 | deps.shutdown_event = process->GetShutDownEvent(); |
| 107 | deps.io_thread_task_runner = process->io_task_runner(); |
Ken Rockot | 47c47d86 | 2019-09-26 17:38:07 | [diff] [blame] | 108 | |
Aman Verma | 8c1c5e61 | 2023-04-03 23:33:58 | [diff] [blame] | 109 | mojo::Remote<ukm::mojom::UkmRecorderFactory> factory; |
| 110 | ChildThread::Get()->BindHostReceiver(factory.BindNewPipeAndPassReceiver()); |
| 111 | deps.ukm_recorder = ukm::MojoUkmRecorder::Create(*factory); |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 112 | return deps; |
| 113 | } |
| 114 | |
[email protected] | e6ff5a31f | 2011-01-31 20:36:22 | [diff] [blame] | 115 | } // namespace |
[email protected] | 9158f2f | 2011-01-27 21:08:57 | [diff] [blame] | 116 | |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 117 | GpuChildThread::GpuChildThread(base::RepeatingClosure quit_closure, |
Dale Curtis | 1b6becebb | 2020-03-30 20:13:35 | [diff] [blame] | 118 | std::unique_ptr<gpu::GpuInit> gpu_init) |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 119 | : GpuChildThread(std::move(quit_closure), |
| 120 | GetOptions(), |
Dale Curtis | 1b6becebb | 2020-03-30 20:13:35 | [diff] [blame] | 121 | std::move(gpu_init)) {} |
[email protected] | 9158f2f | 2011-01-27 21:08:57 | [diff] [blame] | 122 | |
sadrul | 6d41b82 | 2017-04-02 03:38:50 | [diff] [blame] | 123 | GpuChildThread::GpuChildThread(const InProcessChildThreadParams& params, |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 124 | std::unique_ptr<gpu::GpuInit> gpu_init) |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 125 | : GpuChildThread(base::DoNothing(), |
Ken Rockot | 2d483bc | 2021-06-17 22:39:02 | [diff] [blame] | 126 | GetOptions(¶ms), |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 127 | std::move(gpu_init)) {} |
sadrul | 041ca72 | 2017-03-24 22:35:35 | [diff] [blame] | 128 | |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 129 | GpuChildThread::GpuChildThread(base::RepeatingClosure quit_closure, |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 130 | ChildThreadImpl::Options options, |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 131 | std::unique_ptr<gpu::GpuInit> gpu_init) |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 132 | : ChildThreadImpl(MakeQuitSafelyClosure(), std::move(options)), |
Ken Rockot | 47c47d86 | 2019-09-26 17:38:07 | [diff] [blame] | 133 | viz_main_(this, CreateVizMainDependencies(), std::move(gpu_init)), |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 134 | quit_closure_(std::move(quit_closure)) { |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 135 | if (in_process_gpu()) { |
sadrul | 041ca72 | 2017-03-24 22:35:35 | [diff] [blame] | 136 | DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 137 | switches::kSingleProcess) || |
| 138 | base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 139 | switches::kInProcessGPU)); |
| 140 | } |
[email protected] | 7a31f7c | 2011-03-21 23:22:04 | [diff] [blame] | 141 | } |
| 142 | |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 143 | GpuChildThread::~GpuChildThread() = default; |
[email protected] | ce79d851 | 2013-04-22 22:44:41 | [diff] [blame] | 144 | |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 145 | void GpuChildThread::Init( |
| 146 | const base::TimeTicks& process_start_time, |
| 147 | base::sequence_manager::SequenceManager* sequence_manager) { |
kylechar | 4664a16 | 2020-09-15 03:19:34 | [diff] [blame] | 148 | if (!in_process_gpu()) |
| 149 | mojo::SetDefaultProcessErrorHandler(base::BindRepeating(&HandleBadMessage)); |
| 150 | |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 151 | viz_main_.gpu_service()->set_start_time(process_start_time); |
xhwang | 9c8e128 | 2015-10-10 01:54:07 | [diff] [blame] | 152 | |
Matthew Denton | 66b2fa5 | 2023-10-17 00:54:22 | [diff] [blame] | 153 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
| 154 | SandboxedProcessThreadTypeHandler::NotifyMainChildThreadCreated(); |
| 155 | #endif |
| 156 | |
xhwang | ded3b6e | 2016-05-25 05:24:51 | [diff] [blame] | 157 | // When running in in-process mode, this has been set in the browser at |
| 158 | // ChromeBrowserMainPartsAndroid::PreMainMessageLoopRun(). |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 159 | #if BUILDFLAG(IS_ANDROID) |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 160 | if (!in_process_gpu()) { |
chcunningham | a4a8efb | 2017-03-22 16:55:02 | [diff] [blame] | 161 | media::SetMediaDrmBridgeClient( |
| 162 | GetContentClient()->GetMediaDrmBridgeClient()); |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 163 | } |
xhwang | ded3b6e | 2016-05-25 05:24:51 | [diff] [blame] | 164 | #endif |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 165 | |
Georg Neis | 18fc514 | 2024-12-18 00:58:41 | [diff] [blame] | 166 | #if BUILDFLAG(IS_CHROMEOS) |
Eric Sum | c8607e7 | 2022-02-18 00:48:03 | [diff] [blame] | 167 | if (!in_process_gpu()) { |
| 168 | mojo::PendingRemote<font_service::mojom::FontService> font_service; |
| 169 | BindHostReceiver(font_service.InitWithNewPipeAndPassReceiver()); |
| 170 | SkFontConfigInterface::SetGlobal( |
| 171 | sk_make_sp<font_service::FontLoader>(std::move(font_service))); |
| 172 | } |
| 173 | #endif |
| 174 | |
Patrick Monette | cc99f49 | 2025-10-08 20:22:00 | [diff] [blame] | 175 | memory_pressure_listener_registration_ = |
| 176 | std::make_unique<base::AsyncMemoryPressureListenerRegistration>( |
Anthony Vallee-Dubois | 5cb31fd | 2025-08-29 22:28:15 | [diff] [blame] | 177 | FROM_HERE, base::MemoryPressureListenerTag::kGpuChildThread, |
| 178 | base::BindRepeating(&GpuChildThread::OnMemoryPressure, |
| 179 | base::Unretained(this))); |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 180 | if (sequence_manager && |
| 181 | base::FeatureList::IsEnabled( |
| 182 | features::kBoostThreadsPriorityDuringInputScenario)) { |
| 183 | sequence_manager->AddTaskObserver(this); |
| 184 | } |
asvitkine | 5dc812a | 2016-06-07 18:20:30 | [diff] [blame] | 185 | } |
| 186 | |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 187 | bool GpuChildThread::in_process_gpu() const { |
Sadrul Habib Chowdhury | 55fc2f0 | 2017-10-27 02:40:06 | [diff] [blame] | 188 | return viz_main_.gpu_service()->gpu_info().in_process_gpu; |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 189 | } |
| 190 | |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 191 | void GpuChildThread::OnInitializationFailed() { |
| 192 | OnChannelError(); |
| 193 | } |
[email protected] | 9665973 | 2012-08-24 01:55:17 | [diff] [blame] | 194 | |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 195 | void GpuChildThread::OnGpuServiceConnection(viz::GpuServiceImpl* gpu_service) { |
Chris Watkins | 88a4000 | 2017-09-28 01:16:42 | [diff] [blame] | 196 | media::AndroidOverlayMojoFactoryCB overlay_factory_cb; |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 197 | #if BUILDFLAG(IS_ANDROID) |
kylechar | ed18a48 | 2019-02-07 14:07:21 | [diff] [blame] | 198 | overlay_factory_cb = |
| 199 | base::BindRepeating(&GpuChildThread::CreateAndroidOverlay, |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 200 | base::SingleThreadTaskRunner::GetCurrentDefault()); |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 201 | gpu_service->media_gpu_channel_manager()->SetOverlayFactory( |
Chris Watkins | 88a4000 | 2017-09-28 01:16:42 | [diff] [blame] | 202 | overlay_factory_cb); |
| 203 | #endif |
| 204 | |
Oksana Zhuravlova | 0465118 | 2021-03-02 20:52:20 | [diff] [blame] | 205 | if (!IsInBrowserProcess()) { |
| 206 | gpu_service->SetVisibilityChangedCallback( |
| 207 | base::BindRepeating([](bool visible) { |
| 208 | ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged( |
| 209 | visible); |
| 210 | })); |
| 211 | } |
| 212 | |
piman | 223a53c | 2016-07-21 03:41:52 | [diff] [blame] | 213 | // Only set once per process instance. |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 214 | service_factory_ = std::make_unique<GpuServiceFactory>( |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 215 | gpu_service->gpu_preferences(), |
Dan Sanders | 0134bec | 2018-04-17 20:12:12 | [diff] [blame] | 216 | gpu_service->gpu_channel_manager()->gpu_driver_bug_workarounds(), |
Ted Meyer | 68b35b9 | 2021-11-20 02:35:23 | [diff] [blame] | 217 | gpu_service->gpu_feature_info(), gpu_service->gpu_info(), |
Sadrul Habib Chowdhury | dbe4da9 | 2017-10-12 15:53:15 | [diff] [blame] | 218 | gpu_service->media_gpu_channel_manager()->AsWeakPtr(), |
Colin Blundell | da71730 | 2025-07-08 17:10:52 | [diff] [blame] | 219 | std::move(overlay_factory_cb)); |
Ken Rockot | a292417 | 2019-12-18 03:27:05 | [diff] [blame] | 220 | for (auto& receiver : pending_service_receivers_) |
| 221 | BindServiceInterface(std::move(receiver)); |
| 222 | pending_service_receivers_.clear(); |
liberato | 441ca70 | 2017-05-13 16:50:38 | [diff] [blame] | 223 | |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 224 | if (GetContentClient()->gpu()) // Null in tests. |
| 225 | GetContentClient()->gpu()->GpuServiceInitialized(); |
piman | 223a53c | 2016-07-21 03:41:52 | [diff] [blame] | 226 | |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 227 | // Start allowing browser-exposed interfaces to be bound. |
| 228 | // |
| 229 | // NOTE: Do not add new binders within this method. Instead modify |
| 230 | // |ExposeGpuInterfacesToBrowser()| in browser_exposed_gpu_interfaces.cc, as |
| 231 | // that will ensure security review coverage. |
| 232 | mojo::BinderMap binders; |
Chunbo Hua | d90bffb2 | 2020-09-28 15:34:36 | [diff] [blame] | 233 | content::ExposeGpuInterfacesToBrowser( |
Colin Blundell | 8b1809e | 2025-07-09 08:01:11 | [diff] [blame] | 234 | gpu_service, gpu_service->gpu_preferences(), |
Chunbo Hua | d90bffb2 | 2020-09-28 15:34:36 | [diff] [blame] | 235 | gpu_service->gpu_channel_manager()->gpu_driver_bug_workarounds(), |
| 236 | &binders); |
Ken Rockot | 6d9ed9503 | 2019-11-13 17:20:47 | [diff] [blame] | 237 | ExposeInterfacesToBrowser(std::move(binders)); |
[email protected] | 7709e71 | 2011-01-07 17:57:31 | [diff] [blame] | 238 | } |
| 239 | |
Sadrul Habib Chowdhury | 97c6f1f | 2018-03-02 01:21:16 | [diff] [blame] | 240 | void GpuChildThread::PostCompositorThreadCreated( |
| 241 | base::SingleThreadTaskRunner* task_runner) { |
| 242 | auto* gpu_client = GetContentClient()->gpu(); |
| 243 | if (gpu_client) |
| 244 | gpu_client->PostCompositorThreadCreated(task_runner); |
| 245 | } |
| 246 | |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 247 | void GpuChildThread::QuitMainMessageLoop() { |
Eric Karl | 23af711f | 2018-11-02 20:47:34 | [diff] [blame] | 248 | quit_closure_.Run(); |
Wez | 6979109b | 2018-09-07 17:30:56 | [diff] [blame] | 249 | } |
| 250 | |
Jiahe Zhang | 26d6676 | 2025-09-15 06:35:39 | [diff] [blame] | 251 | void GpuChildThread::WillProcessTask(const base::PendingTask& pending_task, |
| 252 | bool was_blocked_or_low_priority) { |
| 253 | performance_scenarios::InputScenario input_scenario = |
| 254 | performance_scenarios::GetInputScenario( |
| 255 | performance_scenarios::ScenarioScope::kGlobal) |
| 256 | ->load(std::memory_order_relaxed); |
| 257 | |
| 258 | // Post a task to the IO thread if the input scenario has changed. This is |
| 259 | // used to make sure the IO thread checks the scenarios in time. |
| 260 | if (input_scenario != last_input_scenario_) { |
| 261 | last_input_scenario_ = input_scenario; |
| 262 | ChildProcess::current()->io_task_runner()->PostTask(FROM_HERE, |
| 263 | base::DoNothing()); |
| 264 | } |
| 265 | } |
| 266 | |
Patrick Monette | 845ec83 | 2025-10-07 18:23:23 | [diff] [blame] | 267 | void GpuChildThread::OnMemoryPressure(base::MemoryPressureLevel level) { |
| 268 | if (level != base::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
Takashi Sakamoto | bb07480 | 2018-10-15 07:35:27 | [diff] [blame] | 269 | return; |
Patrick Monette | 845ec83 | 2025-10-07 18:23:23 | [diff] [blame] | 270 | } |
Khushal | 0aac6207 | 2018-06-01 19:33:17 | [diff] [blame] | 271 | |
Khushal | 0aac6207 | 2018-06-01 19:33:17 | [diff] [blame] | 272 | if (viz_main_.discardable_shared_memory_manager()) |
| 273 | viz_main_.discardable_shared_memory_manager()->ReleaseFreeMemory(); |
| 274 | SkGraphics::PurgeAllCaches(); |
| 275 | } |
| 276 | |
Eric Karl | 23af711f | 2018-11-02 20:47:34 | [diff] [blame] | 277 | void GpuChildThread::QuitSafelyHelper( |
| 278 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 279 | // Post a new task (even if we're called on the |task_runner|'s thread) to |
| 280 | // ensure that we are post-init. |
| 281 | task_runner->PostTask( |
| 282 | FROM_HERE, base::BindOnce([]() { |
| 283 | ChildThreadImpl* current_child_thread = ChildThreadImpl::current(); |
| 284 | if (!current_child_thread) |
| 285 | return; |
| 286 | GpuChildThread* gpu_child_thread = |
| 287 | static_cast<GpuChildThread*>(current_child_thread); |
Patrick To | df5911c | 2020-05-12 19:31:42 | [diff] [blame] | 288 | gpu_child_thread->viz_main_.ExitProcess( |
| 289 | viz::ExitCode::RESULT_CODE_NORMAL_EXIT); |
Eric Karl | 23af711f | 2018-11-02 20:47:34 | [diff] [blame] | 290 | })); |
| 291 | } |
| 292 | |
| 293 | // Returns a closure which calls into the VizMainImpl to perform shutdown |
| 294 | // before quitting the main message loop. Must be called on the main thread. |
| 295 | base::RepeatingClosure GpuChildThread::MakeQuitSafelyClosure() { |
| 296 | return base::BindRepeating(&GpuChildThread::QuitSafelyHelper, |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 297 | base::SingleThreadTaskRunner::GetCurrentDefault()); |
Eric Karl | 23af711f | 2018-11-02 20:47:34 | [diff] [blame] | 298 | } |
| 299 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 300 | #if BUILDFLAG(IS_ANDROID) |
liberato | 441ca70 | 2017-05-13 16:50:38 | [diff] [blame] | 301 | // static |
| 302 | std::unique_ptr<media::AndroidOverlay> GpuChildThread::CreateAndroidOverlay( |
Chris Watkins | 88a4000 | 2017-09-28 01:16:42 | [diff] [blame] | 303 | scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
liberato | 441ca70 | 2017-05-13 16:50:38 | [diff] [blame] | 304 | const base::UnguessableToken& routing_token, |
| 305 | media::AndroidOverlayConfig config) { |
Miyoung Shin | 18de6bf | 2019-10-17 07:06:24 | [diff] [blame] | 306 | mojo::PendingRemote<media::mojom::AndroidOverlayProvider> overlay_provider; |
Chris Watkins | 88a4000 | 2017-09-28 01:16:42 | [diff] [blame] | 307 | if (main_task_runner->RunsTasksInCurrentSequence()) { |
Miyoung Shin | 18de6bf | 2019-10-17 07:06:24 | [diff] [blame] | 308 | ChildThread::Get()->BindHostReceiver( |
| 309 | overlay_provider.InitWithNewPipeAndPassReceiver()); |
Chris Watkins | 88a4000 | 2017-09-28 01:16:42 | [diff] [blame] | 310 | } else { |
Chris Watkins | 88a4000 | 2017-09-28 01:16:42 | [diff] [blame] | 311 | main_task_runner->PostTask( |
Ken Rockot | 47c47d86 | 2019-09-26 17:38:07 | [diff] [blame] | 312 | FROM_HERE, |
| 313 | base::BindOnce( |
Miyoung Shin | 18de6bf | 2019-10-17 07:06:24 | [diff] [blame] | 314 | [](mojo::PendingReceiver<media::mojom::AndroidOverlayProvider> |
| 315 | receiver) { |
| 316 | ChildThread::Get()->BindHostReceiver(std::move(receiver)); |
Ken Rockot | 47c47d86 | 2019-09-26 17:38:07 | [diff] [blame] | 317 | }, |
Miyoung Shin | 18de6bf | 2019-10-17 07:06:24 | [diff] [blame] | 318 | overlay_provider.InitWithNewPipeAndPassReceiver())); |
Chris Watkins | 88a4000 | 2017-09-28 01:16:42 | [diff] [blame] | 319 | } |
| 320 | |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 321 | return std::make_unique<media::MojoAndroidOverlay>( |
[email protected] | 9a635ee | 2017-12-01 21:38:50 | [diff] [blame] | 322 | std::move(overlay_provider), std::move(config), routing_token); |
liberato | 441ca70 | 2017-05-13 16:50:38 | [diff] [blame] | 323 | } |
| 324 | #endif |
| 325 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 326 | } // namespace content |