blob: a29e19e1cc50729a4cf99d87787426f1fbcd4373 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]d7a2d892013-08-16 07:45:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]8707caa2013-09-04 16:41:305#include "content/gpu/in_process_gpu_thread.h"
[email protected]d7a2d892013-08-16 07:45:366
Thoren Paulson6dcf39e2017-08-02 16:33:297#include "base/command_line.h"
Clark DuVall80eecb32024-05-21 17:52:018#include "base/feature_list.h"
xhwang9c8e1282015-10-10 01:54:079#include "base/time/time.h"
avi66a07722015-12-25 23:38:1210#include "build/build_config.h"
Joe Mason94bebf12022-06-03 15:03:5311#include "content/child/child_process.h"
[email protected]d7a2d892013-08-16 07:45:3612#include "content/gpu/gpu_child_thread.h"
Zhenyao Modb2790b2017-08-30 00:10:1513#include "content/public/common/content_client.h"
Thoren Paulson6dcf39e2017-08-02 16:33:2914#include "content/public/common/content_switches.h"
Ian Barkley-Yeung48418c12022-08-16 01:10:1615#include "content/public/gpu/content_gpu_client.h"
Jonathan Backer0af509962018-05-30 16:05:0716#include "gpu/config/gpu_preferences.h"
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:5717#include "gpu/ipc/service/gpu_init.h"
Simeon Kuran372454c2019-12-11 19:02:1718#include "media/gpu/buildflags.h"
19
20#if BUILDFLAG(USE_VAAPI)
21#include "media/gpu/vaapi/vaapi_wrapper.h"
22#endif
[email protected]d7a2d892013-08-16 07:45:3623
Xiaohan Wang62737b52022-01-15 18:09:0224#if BUILDFLAG(IS_ANDROID)
hush5380add2015-12-18 00:41:4225#include "base/android/jni_android.h"
26#endif
27
Gyuyoung Kime40dc1e22025-04-03 14:52:5228#if BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_IOS_TVOS)
Dave Tapuska0c349432025-03-25 23:56:4529#include "gpu/ipc/common/ios/be_layer_hierarchy_transport.h"
30#endif
31
[email protected]d7a2d892013-08-16 07:45:3632namespace content {
Clark DuVall80eecb32024-05-21 17:52:0133namespace {
34
35BASE_FEATURE(kInProcessGpuUseIOThread,
36 "InProcessGpuUseIOThread",
37 base::FEATURE_DISABLED_BY_DEFAULT);
38
39} // namespace
[email protected]d7a2d892013-08-16 07:45:3640
Gyuyoung Kime40dc1e22025-04-03 14:52:5241#if BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_IOS_TVOS)
Dave Tapuska0c349432025-03-25 23:56:4542class 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 Mo83b895e2017-10-18 18:50:5460InProcessGpuThread::InProcessGpuThread(
61 const InProcessChildThreadParams& params,
62 const gpu::GpuPreferences& gpu_preferences)
[email protected]d7a2d892013-08-16 07:45:3663 : base::Thread("Chrome_InProcGpuThread"),
morritac6238ab2015-03-18 01:48:2964 params_(params),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2865 gpu_process_(nullptr),
Zhenyao Mo83b895e2017-10-18 18:50:5466 gpu_preferences_(gpu_preferences) {}
[email protected]d7a2d892013-08-16 07:45:3667
[email protected]8707caa2013-09-04 16:41:3068InProcessGpuThread::~InProcessGpuThread() {
[email protected]d7a2d892013-08-16 07:45:3669 Stop();
70}
71
[email protected]8707caa2013-09-04 16:41:3072void InProcessGpuThread::Init() {
Zhibo Wangd9e4a002022-07-07 04:34:5973 base::ThreadType io_thread_type = base::ThreadType::kDefault;
reveman7caf8cf2016-02-16 02:39:0574
Ian Barkley-Yeung48418c12022-08-16 01:10:1675 // 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 Wang62737b52022-01-15 18:09:0281#if BUILDFLAG(IS_ANDROID)
hush5380add2015-12-18 00:41:4282 // 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.
hush5380add2015-12-18 00:41:4286 base::android::AttachCurrentThreadWithName(thread_name());
reveman7caf8cf2016-02-16 02:39:0587 // Up the priority of the |io_thread_| on Android.
Zhibo Wangd9e4a002022-07-07 04:34:5988 io_thread_type = base::ThreadType::kDisplayCritical;
hush5380add2015-12-18 00:41:4289#endif
90
Gyuyoung Kime40dc1e22025-04-03 14:52:5291#if BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_IOS_TVOS)
Dave Tapuska0c349432025-03-25 23:56:4592 be_layer_transport_ =
93 std::make_unique<InProcessGpuThread::BELayerHierarchyTransportImpl>();
94#endif
95
Clark DuVall80eecb32024-05-21 17:52:0196 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 }
xhwang9c8e1282015-10-10 01:54:07101
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57102 auto gpu_init = std::make_unique<gpu::GpuInit>();
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57103 gpu_init->InitializeInProcess(base::CommandLine::ForCurrentProcess(),
Zhenyao Moe23f75262018-02-07 02:15:00104 gpu_preferences_);
kylechar002b2ee2017-03-15 23:50:12105
Simeon Kuran372454c2019-12-11 19:02:17106#if BUILDFLAG(USE_VAAPI)
107 media::VaapiWrapper::PreSandboxInitialization();
108#endif
109
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57110 GetContentClient()->SetGpuInfo(gpu_init->gpu_info());
ericrk41a1579e2017-02-10 20:56:28111
[email protected]d7a2d892013-08-16 07:45:36112 // The process object takes ownership of the thread object, so do not
113 // save and delete the pointer.
sadrul6d41b822017-04-02 03:38:50114 GpuChildThread* child_thread =
Sadrul Habib Chowdhurydb9021e2017-10-03 03:07:57115 new GpuChildThread(params_, std::move(gpu_init));
xhwang9c8e1282015-10-10 01:54:07116
117 // Since we are in the browser process, use the thread start time as the
118 // process start time.
Sean Maherf36d8122022-08-05 02:33:35119 child_thread->Init(base::TimeTicks::Now());
xhwang9c8e1282015-10-10 01:54:07120
121 gpu_process_->set_main_thread(child_thread);
[email protected]d7a2d892013-08-16 07:45:36122}
123
[email protected]8707caa2013-09-04 16:41:30124void InProcessGpuThread::CleanUp() {
[email protected]242f6cf2014-06-09 20:24:58125 SetThreadWasQuitProperly(true);
Ali Hijazi1b1fb432022-08-03 10:45:37126 gpu_process_.reset();
[email protected]d7a2d892013-08-16 07:45:36127}
128
morritac6238ab2015-03-18 01:48:29129base::Thread* CreateInProcessGpuThread(
Zhenyao Mo83b895e2017-10-18 18:50:54130 const InProcessChildThreadParams& params,
131 const gpu::GpuPreferences& gpu_preferences) {
132 return new InProcessGpuThread(params, gpu_preferences);
[email protected]d7a2d892013-08-16 07:45:36133}
134
135} // namespace content