summaryrefslogtreecommitdiffstats
path: root/src/core/web_engine_context_threads.cpp
blob: 2d0f8a90cfb223a450d188327cbcb334958e7e76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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 <memory>

#include <QEventLoop>

namespace QtWebEngineCore {

struct GpuThreadControllerQt : content::GpuThreadController
{
    GpuThreadControllerQt(const content::InProcessChildThreadParams &params, 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 &params,
            const gpu::GpuPreferences &gpuPreferences)
    {
        DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
        if (s_gpuProcessDestroyed)
            return;

        s_gpuProcess = std::make_unique<content::GpuProcess>(base::ThreadPriority::NORMAL);
        auto gpuInit = std::make_unique<gpu::GpuInit>();
        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<content::GpuProcess> s_gpuProcess;
    static bool s_gpuProcessDestroyed;
};

std::unique_ptr<content::GpuProcess> GpuThreadControllerQt::s_gpuProcess;
bool GpuThreadControllerQt::s_gpuProcessDestroyed = false;

static std::unique_ptr<content::GpuThreadController> createGpuThreadController(
        const content::InProcessChildThreadParams &params,
        const gpu::GpuPreferences &gpuPreferences)
{
    return std::make_unique<GpuThreadControllerQt>(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