// Copyright (C) 2018 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "web_engine_context.h" #include "base/bind.h" #include "base/task/post_task.h" #include "base/threading/platform_thread.h" #include "base/threading/thread_restrictions.h" #include "content/browser/gpu/gpu_main_thread_factory.h" #include "content/browser/renderer_host/render_process_host_impl.h" #include "content/browser/utility_process_host.h" #include "content/gpu/gpu_child_thread.h" #include "content/gpu/gpu_process.h" #include "content/gpu/in_process_gpu_thread.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "content/renderer/in_process_renderer_thread.h" #include "content/utility/in_process_utility_thread.h" #include "gpu/ipc/service/gpu_init.h" #include #include namespace QtWebEngineCore { struct GpuThreadControllerQt : content::GpuThreadController { GpuThreadControllerQt(const content::InProcessChildThreadParams ¶ms, const gpu::GpuPreferences &gpuPreferences) { base::PostTask( FROM_HERE, { content::BrowserThread::UI }, base::BindOnce(&GpuThreadControllerQt::createGpuProcess, params, gpuPreferences)); } ~GpuThreadControllerQt() override { base::PostTask( FROM_HERE, { content::BrowserThread::UI }, base::BindOnce(&GpuThreadControllerQt::destroyGpuProcess)); } static void createGpuProcess( const content::InProcessChildThreadParams ¶ms, const gpu::GpuPreferences &gpuPreferences) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); if (s_gpuProcessDestroyed) return; s_gpuProcess = std::make_unique(base::ThreadPriority::NORMAL); auto gpuInit = std::make_unique(); gpuInit->InitializeInProcess(base::CommandLine::ForCurrentProcess(), gpuPreferences); auto childThread = new content::GpuChildThread(params, std::move(gpuInit)); childThread->Init(base::Time::Now()); s_gpuProcess->set_main_thread(childThread); } static void destroyGpuProcess() { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); if (s_gpuProcessDestroyed) return; // viz::GpuServiceImpl::~GpuServiceImpl waits for io task. base::ScopedAllowBaseSyncPrimitivesForTesting allow; s_gpuProcess.reset(); s_gpuProcessDestroyed = true; } static std::unique_ptr s_gpuProcess; static bool s_gpuProcessDestroyed; }; std::unique_ptr GpuThreadControllerQt::s_gpuProcess; bool GpuThreadControllerQt::s_gpuProcessDestroyed = false; static std::unique_ptr createGpuThreadController( const content::InProcessChildThreadParams ¶ms, const gpu::GpuPreferences &gpuPreferences) { return std::make_unique(params, gpuPreferences); } // static void WebEngineContext::destroyGpuProcess() { GpuThreadControllerQt::destroyGpuProcess(); } // static void WebEngineContext::registerMainThreadFactories() { content::UtilityProcessHost::RegisterUtilityMainThreadFactory(content::CreateInProcessUtilityThread); content::RenderProcessHostImpl::RegisterRendererMainThreadFactory(content::CreateInProcessRendererThread); if (!isGpuServiceOnUIThread()) content::RegisterGpuMainThreadFactory(content::CreateInProcessGpuThread); else content::RegisterGpuMainThreadFactory(createGpuThreadController); } } // namespace QtWebEngineCore