Evan Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 1 | // 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 Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 7 | #include "build/build_config.h" |
Lei Zhang | 33c34e4 | 2024-05-06 18:59:12 | [diff] [blame^] | 8 | #include "build/chromeos_buildflags.h" |
Evan Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 9 | |
| 10 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
| 11 | #include "ash/constants/ash_features.h" |
Lei Zhang | 33c34e4 | 2024-05-06 18:59:12 | [diff] [blame^] | 12 | #include "base/feature_list.h" |
Evan Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 13 | #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 Zhang | 33c34e4 | 2024-05-06 18:59:12 | [diff] [blame^] | 23 | #if BUILDFLAG(IS_LINUX) && defined(ARCH_CPU_X86_FAMILY) |
| 24 | #include "base/cpu.h" |
| 25 | #endif |
| 26 | |
Evan Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 27 | namespace speech { |
| 28 | |
Lei Zhang | 33c34e4 | 2024-05-06 18:59:12 | [diff] [blame^] | 29 | namespace { |
| 30 | |
| 31 | #if BUILDFLAG(IS_CHROMEOS) |
| 32 | bool IsSupportedChromeOS() { |
Evan Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 33 | // 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 Zhang | 33c34e4 | 2024-05-06 18:59:12 | [diff] [blame^] | 43 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
| 44 | return true; |
| 45 | } |
| 46 | #endif // BUILDFLAG(IS_CHROMEOS) |
Evan Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 47 | |
| 48 | #if BUILDFLAG(IS_LINUX) |
Lei Zhang | 33c34e4 | 2024-05-06 18:59:12 | [diff] [blame^] | 49 | bool IsSupportedLinux() { |
| 50 | #if defined(ARCH_CPU_X86_FAMILY) |
Evan Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 51 | // 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 Zhang | 33c34e4 | 2024-05-06 18:59:12 | [diff] [blame^] | 54 | 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 Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 61 | |
Lei Zhang | 33c34e4 | 2024-05-06 18:59:12 | [diff] [blame^] | 62 | #if BUILDFLAG(IS_WIN) |
| 63 | bool IsSupportedWin() { |
| 64 | #if defined(ARCH_CPU_ARM64) |
Evan Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 65 | // The Speech On-Device API (SODA) component does not support Windows on |
| 66 | // arm64. |
| 67 | return false; |
| 68 | #else |
| 69 | return true; |
Lei Zhang | 33c34e4 | 2024-05-06 18:59:12 | [diff] [blame^] | 70 | #endif // defined(ARCH_CPU_ARM64) |
| 71 | } |
| 72 | #endif // BUILDFLAG(IS_WIN) |
| 73 | |
| 74 | } // namespace |
| 75 | |
| 76 | bool 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 Liu | 4528959 | 2024-03-21 17:46:12 | [diff] [blame] | 85 | #endif |
| 86 | } |
| 87 | |
| 88 | } // namespace speech |