Commit 5b1ca7b1 authored by Hongchan Choi's avatar Hongchan Choi Committed by Commit Bot

Remove CreateWebAudioThread() and use ThreadCreationParams for WebAudio instead

The previous CL [1] does not work anymore due to the changes in Worker
infrastructure, so this CL changes ThreadCreationParams to fix the
issue.

ThreadCreationParams will check the thread type and assign the right
priority for the WebAudio operation.

[1] https://chromium-review.googlesource.com/c/chromium/src/+/1580221

Bug: 813825, 877978
Test: third_party/blink/renderer/modules/webaudio/audio_worklet_thread_test.cc
Change-Id: I9be6b2ccc37feaed22c2342391d5ef27388ef912
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1865013
Commit-Queue: Hongchan Choi <hongchan@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715335}
parent b99b969a
......@@ -6,7 +6,9 @@
#include <memory>
#include "base/feature_list.h"
#include "base/memory/ptr_util.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/core/workers/global_scope_creation_params.h"
#include "third_party/blink/renderer/core/workers/worker_backing_thread.h"
......@@ -52,8 +54,17 @@ WorkerBackingThread& AudioWorkletThread::GetWorkerBackingThread() {
void AudioWorkletThread::EnsureSharedBackingThread() {
DCHECK(IsMainThread());
WorkletThreadHolder<AudioWorkletThread>::EnsureInstance(
ThreadCreationParams(ThreadType::kAudioWorkletThread));
ThreadCreationParams params =
ThreadCreationParams(ThreadType::kAudioWorkletThread);
// TODO(crbug.com/1022888): The worklet thread priority is always NORMAL on
// linux.
params.thread_priority =
base::FeatureList::IsEnabled(features::kAudioWorkletRealtimeThread)
? base::ThreadPriority::REALTIME_AUDIO
: base::ThreadPriority::DISPLAY;
WorkletThreadHolder<AudioWorkletThread>::EnsureInstance(params);
}
void AudioWorkletThread::ClearSharedBackingThread() {
......
......@@ -5,8 +5,12 @@
#include "third_party/blink/renderer/modules/webaudio/audio_worklet_thread.h"
#include <memory>
#include "base/feature_list.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/renderer/bindings/core/v8/module_record.h"
......@@ -25,6 +29,7 @@
#include "third_party/blink/renderer/core/workers/worker_or_worklet_global_scope.h"
#include "third_party/blink/renderer/core/workers/worker_reporting_proxy.h"
#include "third_party/blink/renderer/core/workers/worklet_module_responses_map.h"
#include "third_party/blink/renderer/platform/wtf/threading.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_loader_options.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
......@@ -37,12 +42,15 @@ namespace blink {
class AudioWorkletThreadTest : public PageTestBase {
public:
void SetUp() override {
AudioWorkletThread::EnsureSharedBackingThread();
PageTestBase::SetUp(IntSize());
NavigateTo(KURL("https://example.com/"));
reporting_proxy_ = std::make_unique<WorkerReportingProxy>();
}
void TearDown() override {
AudioWorkletThread::ClearSharedBackingThread();
}
std::unique_ptr<AudioWorkletThread> CreateAudioWorkletThread() {
std::unique_ptr<AudioWorkletThread> thread =
AudioWorkletThread::Create(*reporting_proxy_);
......@@ -77,6 +85,20 @@ class AudioWorkletThreadTest : public PageTestBase {
wait_event.Wait();
}
void CheckWorkletThreadPriority(WorkerThread* thread,
base::ThreadPriority expected_priority) {
base::WaitableEvent wait_event;
PostCrossThreadTask(
*thread->GetWorkerBackingThread().BackingThread().GetTaskRunner(),
FROM_HERE,
CrossThreadBindOnce(&AudioWorkletThreadTest::CheckThreadPriority,
CrossThreadUnretained(this),
CrossThreadUnretained(thread),
expected_priority,
CrossThreadUnretained(&wait_event)));
wait_event.Wait();
}
private:
void ExecuteScriptInWorklet(WorkerThread* thread,
base::WaitableEvent* wait_event) {
......@@ -99,6 +121,22 @@ class AudioWorkletThreadTest : public PageTestBase {
wait_event->Signal();
}
void CheckThreadPriority(WorkerThread* thread,
base::ThreadPriority expected_priority,
base::WaitableEvent* wait_event) {
ASSERT_TRUE(thread->IsCurrentThread());
// TODO(crbug.com/1022888): The worklet thread priority is always NORMAL on
// linux.
#if defined(OS_LINUX)
EXPECT_EQ(base::PlatformThread::GetCurrentThreadPriority(),
base::ThreadPriority::NORMAL);
#else
EXPECT_EQ(base::PlatformThread::GetCurrentThreadPriority(),
expected_priority);
#endif
wait_event->Signal();
}
std::unique_ptr<WorkerReportingProxy> reporting_proxy_;
};
......@@ -195,4 +233,39 @@ TEST_F(AudioWorkletThreadTest, CreatingSecondDuringTerminationOfFirst) {
second_worklet->WaitForShutdownForTesting();
}
class AudioWorkletThreadDisplayPriorityTest : public AudioWorkletThreadTest {
public:
AudioWorkletThreadDisplayPriorityTest() {
feature_list_.InitAndDisableFeature(features::kAudioWorkletRealtimeThread);
}
private:
base::test::ScopedFeatureList feature_list_;
};
TEST_F(AudioWorkletThreadDisplayPriorityTest, DisplayPriority) {
std::unique_ptr<AudioWorkletThread> worklet = CreateAudioWorkletThread();
CheckWorkletThreadPriority(worklet.get(), base::ThreadPriority::DISPLAY);
worklet->Terminate();
worklet->WaitForShutdownForTesting();
}
class AudioWorkletThreadRealtimePriorityTest : public AudioWorkletThreadTest {
public:
AudioWorkletThreadRealtimePriorityTest() {
feature_list_.InitAndEnableFeature(features::kAudioWorkletRealtimeThread);
}
private:
base::test::ScopedFeatureList feature_list_;
};
TEST_F(AudioWorkletThreadRealtimePriorityTest, RealtimePriority) {
std::unique_ptr<AudioWorkletThread> worklet = CreateAudioWorkletThread();
CheckWorkletThreadPriority(worklet.get(),
base::ThreadPriority::REALTIME_AUDIO);
worklet->Terminate();
worklet->WaitForShutdownForTesting();
}
} // namespace blink
......@@ -83,21 +83,6 @@ std::unique_ptr<Thread> Thread::CreateThread(
return std::move(thread);
}
std::unique_ptr<Thread> Thread::CreateWebAudioThread() {
ThreadCreationParams params(ThreadType::kAudioWorkletThread);
params.supports_gc = true;
// WebAudio uses a thread with |DISPLAY| priority to avoid glitch when the
// system is under the high pressure. Note that the main browser thread also
// runs with same priority. (see: crbug.com/734539)
params.thread_priority =
base::FeatureList::IsEnabled(features::kAudioWorkletRealtimeThread)
? base::ThreadPriority::REALTIME_AUDIO
: base::ThreadPriority::DISPLAY;
return CreateThread(params);
}
void Thread::CreateAndSetCompositorThread() {
DCHECK(!GetCompositorThread());
......
......@@ -66,7 +66,11 @@ struct PLATFORM_EXPORT ThreadCreationParams {
ThreadType thread_type;
const char* name;
FrameOrWorkerScheduler* frame_or_worker_scheduler; // NOT OWNED
// Do NOT set the thread priority for non-WebAudio usages. Please consult
// scheduler-dev@ first in order to use an elevated thread priority.
base::ThreadPriority thread_priority = base::ThreadPriority::NORMAL;
bool supports_gc = false;
};
......@@ -91,10 +95,6 @@ class PLATFORM_EXPORT Thread {
// nested Web workers).
static std::unique_ptr<Thread> CreateThread(const ThreadCreationParams&);
// Creates a WebAudio-specific thread with the elevated priority. Do NOT use
// for any other purpose.
static std::unique_ptr<Thread> CreateWebAudioThread();
// Create and save (as a global variable) the compositor thread. The thread
// will be accessible through CompositorThread().
static void CreateAndSetCompositorThread();
......
......@@ -938,7 +938,11 @@ _CONFIG = [
{
'paths': ['third_party/blink/renderer/core/frame/local_frame_view.cc'],
'allowed': ['cc::frame_viewer_instrumentation::IsTracingLayerTreeSnapshots'],
}
},
{
'paths': ['third_party/blink/renderer/modules/webaudio/audio_worklet_thread.cc'],
'allowed': ['base::ThreadPriority'],
},
]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment