blob: 8e8440dedf8b5b615b73c6830dc3080c34cce29a [file] [log] [blame]
Evan Liu45289592024-03-21 17:46:121// Copyright 2024 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/soda/soda_util.h"
6
Evan Liu45289592024-03-21 17:46:127#include "build/build_config.h"
Lei Zhang33c34e42024-05-06 18:59:128#include "build/chromeos_buildflags.h"
Evan Liu45289592024-03-21 17:46:129
10#if BUILDFLAG(IS_CHROMEOS_ASH)
11#include "ash/constants/ash_features.h"
Lei Zhang33c34e42024-05-06 18:59:1212#include "base/feature_list.h"
Evan Liu45289592024-03-21 17:46:1213#endif
14
15#if BUILDFLAG(IS_CHROMEOS_LACROS)
16#include "chromeos/startup/browser_params_proxy.h"
17#endif
18
19#if BUILDFLAG(IS_WIN)
20#include "base/win/windows_version.h"
21#endif
22
Lei Zhang33c34e42024-05-06 18:59:1223#if BUILDFLAG(IS_LINUX) && defined(ARCH_CPU_X86_FAMILY)
24#include "base/cpu.h"
25#endif
26
Evan Liu45289592024-03-21 17:46:1227namespace speech {
28
Lei Zhang33c34e42024-05-06 18:59:1229namespace {
30
31#if BUILDFLAG(IS_CHROMEOS)
32bool IsSupportedChromeOS() {
Evan Liu45289592024-03-21 17:46:1233// Some Chrome OS devices do not support on-device speech.
34#if BUILDFLAG(IS_CHROMEOS_ASH)
35 if (!base::FeatureList::IsEnabled(
36 ash::features::kOnDeviceSpeechRecognition)) {
37 return false;
38 }
39#elif BUILDFLAG(IS_CHROMEOS_LACROS)
40 if (!chromeos::BrowserParamsProxy::Get()->IsOndeviceSpeechSupported()) {
41 return false;
42 }
Lei Zhang33c34e42024-05-06 18:59:1243#endif // BUILDFLAG(IS_CHROMEOS_ASH)
44 return true;
45}
46#endif // BUILDFLAG(IS_CHROMEOS)
Evan Liu45289592024-03-21 17:46:1247
48#if BUILDFLAG(IS_LINUX)
Lei Zhang33c34e42024-05-06 18:59:1249bool IsSupportedLinux() {
50#if defined(ARCH_CPU_X86_FAMILY)
Evan Liu45289592024-03-21 17:46:1251 // Check if the CPU has the required instruction set to run the Speech
52 // On-Device API (SODA) library.
53 static bool has_sse41 = base::CPU().has_sse41();
Lei Zhang33c34e42024-05-06 18:59:1254 return has_sse41;
55#else
56 // Other architectures are not supported.
57 return false;
58#endif // defined(ARCH_CPU_X86_FAMILY)
59}
60#endif // BUILDFLAG(IS_LINUX)
Evan Liu45289592024-03-21 17:46:1261
Lei Zhang33c34e42024-05-06 18:59:1262#if BUILDFLAG(IS_WIN)
63bool IsSupportedWin() {
64#if defined(ARCH_CPU_ARM64)
Evan Liu45289592024-03-21 17:46:1265 // The Speech On-Device API (SODA) component does not support Windows on
66 // arm64.
67 return false;
68#else
69 return true;
Lei Zhang33c34e42024-05-06 18:59:1270#endif // defined(ARCH_CPU_ARM64)
71}
72#endif // BUILDFLAG(IS_WIN)
73
74} // namespace
75
76bool IsOnDeviceSpeechRecognitionSupported() {
77#if BUILDFLAG(IS_CHROMEOS)
78 return IsSupportedChromeOS();
79#elif BUILDFLAG(IS_LINUX)
80 return IsSupportedLinux();
81#elif BUILDFLAG(IS_WIN)
82 return IsSupportedWin();
83#else
84 return true;
Evan Liu45289592024-03-21 17:46:1285#endif
86}
87
88} // namespace speech