summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/modules/webaudio/semi_realtime_audio_worklet_thread.cc
blob: 5a3de4bc4f35aeefd15bc89cee53ea1e71ca1186 (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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/webaudio/semi_realtime_audio_worklet_thread.h"

#include "base/feature_list.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/modules/webaudio/audio_worklet_global_scope.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"

namespace blink {

template class WorkletThreadHolder<SemiRealtimeAudioWorkletThread>;

int SemiRealtimeAudioWorkletThread::s_ref_count_ = 0;

SemiRealtimeAudioWorkletThread::SemiRealtimeAudioWorkletThread(
    WorkerReportingProxy& worker_reporting_proxy)
    : WorkerThread(worker_reporting_proxy) {
  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("audio-worklet"),
               "SemiRealtimeAudioWorklet()");

  DCHECK(IsMainThread());

  ThreadCreationParams params =
      ThreadCreationParams(ThreadType::kSemiRealtimeAudioWorkletThread);

  // Use a higher priority thread only when it is allowed by Finch.
  if (base::FeatureList::IsEnabled(
          features::kAudioWorkletThreadRealtimePriority)) {
    // TODO(crbug.com/1022888): The worklet thread priority is always NORMAL on
    // Linux and Chrome OS regardless of this thread priority setting.
    params.thread_priority = base::ThreadPriority::DISPLAY;
  } else {
    params.thread_priority = base::ThreadPriority::NORMAL;
  }

  if (++s_ref_count_ == 1)
    EnsureSharedBackingThread(params);
}

SemiRealtimeAudioWorkletThread::~SemiRealtimeAudioWorkletThread() {
  DCHECK(IsMainThread());
  if (--s_ref_count_ == 0)
    ClearSharedBackingThread();
}

WorkerBackingThread& SemiRealtimeAudioWorkletThread::GetWorkerBackingThread() {
  return *WorkletThreadHolder<SemiRealtimeAudioWorkletThread>::GetInstance()
      ->GetThread();
}

void SemiRealtimeAudioWorkletThread::EnsureSharedBackingThread(
    const ThreadCreationParams& params) {
  DCHECK(IsMainThread());
  WorkletThreadHolder<SemiRealtimeAudioWorkletThread>::EnsureInstance(params);
}

void SemiRealtimeAudioWorkletThread::ClearSharedBackingThread() {
  DCHECK(IsMainThread());
  CHECK_EQ(s_ref_count_, 0);
  WorkletThreadHolder<SemiRealtimeAudioWorkletThread>::ClearInstance();
}

WorkerOrWorkletGlobalScope*
SemiRealtimeAudioWorkletThread::CreateWorkerGlobalScope(
    std::unique_ptr<GlobalScopeCreationParams> creation_params) {
  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("audio-worklet"),
               "SemiRealtimeAudioWorkletThread::CreateWorkerGlobalScope");
  return MakeGarbageCollected<AudioWorkletGlobalScope>(
      std::move(creation_params), this);
}

}  // namespace blink