Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [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] | 8707caa | 2013-09-04 16:41:30 | [diff] [blame] | 5 | #include "content/gpu/in_process_gpu_thread.h" |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 6 | |
Thoren Paulson | 6dcf39e | 2017-08-02 16:33:29 | [diff] [blame] | 7 | #include "base/command_line.h" |
Clark DuVall | 80eecb3 | 2024-05-21 17:52:01 | [diff] [blame] | 8 | #include "base/feature_list.h" |
xhwang | 9c8e128 | 2015-10-10 01:54:07 | [diff] [blame] | 9 | #include "base/time/time.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 10 | #include "build/build_config.h" |
Joe Mason | 94bebf1 | 2022-06-03 15:03:53 | [diff] [blame] | 11 | #include "content/child/child_process.h" |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 12 | #include "content/gpu/gpu_child_thread.h" |
Zhenyao Mo | db2790b | 2017-08-30 00:10:15 | [diff] [blame] | 13 | #include "content/public/common/content_client.h" |
Thoren Paulson | 6dcf39e | 2017-08-02 16:33:29 | [diff] [blame] | 14 | #include "content/public/common/content_switches.h" |
Ian Barkley-Yeung | 48418c1 | 2022-08-16 01:10:16 | [diff] [blame] | 15 | #include "content/public/gpu/content_gpu_client.h" |
Jonathan Backer | 0af50996 | 2018-05-30 16:05:07 | [diff] [blame] | 16 | #include "gpu/config/gpu_preferences.h" |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 17 | #include "gpu/ipc/service/gpu_init.h" |
Simeon Kuran | 372454c | 2019-12-11 19:02:17 | [diff] [blame] | 18 | #include "media/gpu/buildflags.h" |
| 19 | |
| 20 | #if BUILDFLAG(USE_VAAPI) |
| 21 | #include "media/gpu/vaapi/vaapi_wrapper.h" |
| 22 | #endif |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 23 | |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 24 | #if BUILDFLAG(IS_ANDROID) |
hush | 5380add | 2015-12-18 00:41:42 | [diff] [blame] | 25 | #include "base/android/jni_android.h" |
| 26 | #endif |
| 27 | |
Gyuyoung Kim | e40dc1e2 | 2025-04-03 14:52:52 | [diff] [blame] | 28 | #if BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_IOS_TVOS) |
Dave Tapuska | 0c34943 | 2025-03-25 23:56:45 | [diff] [blame] | 29 | #include "gpu/ipc/common/ios/be_layer_hierarchy_transport.h" |
| 30 | #endif |
| 31 | |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 32 | namespace content { |
Clark DuVall | 80eecb3 | 2024-05-21 17:52:01 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | BASE_FEATURE(kInProcessGpuUseIOThread, |
| 36 | "InProcessGpuUseIOThread", |
| 37 | base::FEATURE_DISABLED_BY_DEFAULT); |
| 38 | |
| 39 | } // namespace |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 40 | |
Gyuyoung Kim | e40dc1e2 | 2025-04-03 14:52:52 | [diff] [blame] | 41 | #if BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_IOS_TVOS) |
Dave Tapuska | 0c34943 | 2025-03-25 23:56:45 | [diff] [blame] | 42 | class InProcessGpuThread::BELayerHierarchyTransportImpl |
| 43 | : public gpu::BELayerHierarchyTransport { |
| 44 | public: |
| 45 | BELayerHierarchyTransportImpl() { |
| 46 | gpu::BELayerHierarchyTransport::SetInstance(this); |
| 47 | } |
| 48 | ~BELayerHierarchyTransportImpl() override { |
| 49 | gpu::BELayerHierarchyTransport::SetInstance(nullptr); |
| 50 | } |
| 51 | |
| 52 | void ForwardBELayerHierarchyToBrowser( |
| 53 | gpu::SurfaceHandle surface_handle, |
| 54 | xpc_object_t ipc_representation) override { |
| 55 | // Nothing to do. |
| 56 | } |
| 57 | }; |
| 58 | #endif |
| 59 | |
Zhenyao Mo | 83b895e | 2017-10-18 18:50:54 | [diff] [blame] | 60 | InProcessGpuThread::InProcessGpuThread( |
| 61 | const InProcessChildThreadParams& params, |
| 62 | const gpu::GpuPreferences& gpu_preferences) |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 63 | : base::Thread("Chrome_InProcGpuThread"), |
morrita | c6238ab | 2015-03-18 01:48:29 | [diff] [blame] | 64 | params_(params), |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 65 | gpu_process_(nullptr), |
Zhenyao Mo | 83b895e | 2017-10-18 18:50:54 | [diff] [blame] | 66 | gpu_preferences_(gpu_preferences) {} |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 67 | |
[email protected] | 8707caa | 2013-09-04 16:41:30 | [diff] [blame] | 68 | InProcessGpuThread::~InProcessGpuThread() { |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 69 | Stop(); |
| 70 | } |
| 71 | |
[email protected] | 8707caa | 2013-09-04 16:41:30 | [diff] [blame] | 72 | void InProcessGpuThread::Init() { |
Zhibo Wang | d9e4a00 | 2022-07-07 04:34:59 | [diff] [blame] | 73 | base::ThreadType io_thread_type = base::ThreadType::kDefault; |
reveman | 7caf8cf | 2016-02-16 02:39:05 | [diff] [blame] | 74 | |
Ian Barkley-Yeung | 48418c1 | 2022-08-16 01:10:16 | [diff] [blame] | 75 | // In single-process mode, we never enter the sandbox, so run the post-sandbox |
| 76 | // code now. |
| 77 | content::ContentGpuClient* client = GetContentClient()->gpu(); |
| 78 | if (client) { |
| 79 | client->PostSandboxInitialized(); |
| 80 | } |
Xiaohan Wang | 62737b5 | 2022-01-15 18:09:02 | [diff] [blame] | 81 | #if BUILDFLAG(IS_ANDROID) |
hush | 5380add | 2015-12-18 00:41:42 | [diff] [blame] | 82 | // Call AttachCurrentThreadWithName, before any other AttachCurrentThread() |
| 83 | // calls. The latter causes Java VM to assign Thread-??? to the thread name. |
| 84 | // Please note calls to AttachCurrentThreadWithName after AttachCurrentThread |
| 85 | // will not change the thread name kept in Java VM. |
hush | 5380add | 2015-12-18 00:41:42 | [diff] [blame] | 86 | base::android::AttachCurrentThreadWithName(thread_name()); |
reveman | 7caf8cf | 2016-02-16 02:39:05 | [diff] [blame] | 87 | // Up the priority of the |io_thread_| on Android. |
Zhibo Wang | d9e4a00 | 2022-07-07 04:34:59 | [diff] [blame] | 88 | io_thread_type = base::ThreadType::kDisplayCritical; |
hush | 5380add | 2015-12-18 00:41:42 | [diff] [blame] | 89 | #endif |
| 90 | |
Gyuyoung Kim | e40dc1e2 | 2025-04-03 14:52:52 | [diff] [blame] | 91 | #if BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_IOS_TVOS) |
Dave Tapuska | 0c34943 | 2025-03-25 23:56:45 | [diff] [blame] | 92 | be_layer_transport_ = |
| 93 | std::make_unique<InProcessGpuThread::BELayerHierarchyTransportImpl>(); |
| 94 | #endif |
| 95 | |
Clark DuVall | 80eecb3 | 2024-05-21 17:52:01 | [diff] [blame] | 96 | if (base::FeatureList::IsEnabled(kInProcessGpuUseIOThread)) { |
| 97 | gpu_process_ = std::make_unique<ChildProcess>(params_.child_io_runner()); |
| 98 | } else { |
| 99 | gpu_process_ = std::make_unique<ChildProcess>(io_thread_type); |
| 100 | } |
xhwang | 9c8e128 | 2015-10-10 01:54:07 | [diff] [blame] | 101 | |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 102 | auto gpu_init = std::make_unique<gpu::GpuInit>(); |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 103 | gpu_init->InitializeInProcess(base::CommandLine::ForCurrentProcess(), |
Zhenyao Mo | e23f7526 | 2018-02-07 02:15:00 | [diff] [blame] | 104 | gpu_preferences_); |
kylechar | 002b2ee | 2017-03-15 23:50:12 | [diff] [blame] | 105 | |
Simeon Kuran | 372454c | 2019-12-11 19:02:17 | [diff] [blame] | 106 | #if BUILDFLAG(USE_VAAPI) |
| 107 | media::VaapiWrapper::PreSandboxInitialization(); |
| 108 | #endif |
| 109 | |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 110 | GetContentClient()->SetGpuInfo(gpu_init->gpu_info()); |
ericrk | 41a1579e | 2017-02-10 20:56:28 | [diff] [blame] | 111 | |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 112 | // The process object takes ownership of the thread object, so do not |
| 113 | // save and delete the pointer. |
sadrul | 6d41b82 | 2017-04-02 03:38:50 | [diff] [blame] | 114 | GpuChildThread* child_thread = |
Sadrul Habib Chowdhury | db9021e | 2017-10-03 03:07:57 | [diff] [blame] | 115 | new GpuChildThread(params_, std::move(gpu_init)); |
xhwang | 9c8e128 | 2015-10-10 01:54:07 | [diff] [blame] | 116 | |
| 117 | // Since we are in the browser process, use the thread start time as the |
| 118 | // process start time. |
Sean Maher | f36d812 | 2022-08-05 02:33:35 | [diff] [blame] | 119 | child_thread->Init(base::TimeTicks::Now()); |
xhwang | 9c8e128 | 2015-10-10 01:54:07 | [diff] [blame] | 120 | |
| 121 | gpu_process_->set_main_thread(child_thread); |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | 8707caa | 2013-09-04 16:41:30 | [diff] [blame] | 124 | void InProcessGpuThread::CleanUp() { |
[email protected] | 242f6cf | 2014-06-09 20:24:58 | [diff] [blame] | 125 | SetThreadWasQuitProperly(true); |
Ali Hijazi | 1b1fb43 | 2022-08-03 10:45:37 | [diff] [blame] | 126 | gpu_process_.reset(); |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 127 | } |
| 128 | |
morrita | c6238ab | 2015-03-18 01:48:29 | [diff] [blame] | 129 | base::Thread* CreateInProcessGpuThread( |
Zhenyao Mo | 83b895e | 2017-10-18 18:50:54 | [diff] [blame] | 130 | const InProcessChildThreadParams& params, |
| 131 | const gpu::GpuPreferences& gpu_preferences) { |
| 132 | return new InProcessGpuThread(params, gpu_preferences); |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } // namespace content |