Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef ML_HANDWRITING_H_ |
| 6 | #define ML_HANDWRITING_H_ |
| 7 | |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 10 | #include <base/no_destructor.h> |
| 11 | #include <base/optional.h> |
| 12 | #include <base/scoped_native_library.h> |
Honglin Yu | cfb1930 | 2020-08-17 17:13:14 +1000 | [diff] [blame] | 13 | #include <chromeos/libhandwriting/handwriting_interface.h> |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 14 | |
Honglin Yu | cfb1930 | 2020-08-17 17:13:14 +1000 | [diff] [blame] | 15 | #include "chrome/knowledge/handwriting/handwriting_interface.pb.h" |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 16 | #include "ml/mojom/handwriting_recognizer.mojom.h" |
Honglin Yu | 3e360a0 | 2020-08-27 10:30:01 +1000 | [diff] [blame] | 17 | #include "ml/util.h" |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 18 | |
| 19 | namespace ml { |
| 20 | // A singleton proxy class for the handwriting DSO. |
| 21 | // Usage: |
| 22 | // auto* const hwr_library = HandwritingLibrary::GetInstance(); |
| 23 | // if (hwr_library->GetStatus() == HandwritingLibrary::kOk) { |
| 24 | // // Do the real handwriting here. |
| 25 | // recognizer = hwr_library->CreateHandwritingRecognizer(); |
| 26 | // ... |
| 27 | // } else { |
| 28 | // // Otherwise, use HandwritingLibrary::GetStatus() to get the error type. |
| 29 | // // Maybe return "not installed". |
| 30 | // ... |
| 31 | // } |
| 32 | class HandwritingLibrary { |
| 33 | public: |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 34 | enum class Status { |
| 35 | kOk = 0, |
| 36 | kUninitialized = 1, |
| 37 | kLoadLibraryFailed = 2, |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 38 | kFunctionLookupFailed = 3, |
| 39 | kNotSupported = 4, |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 40 | }; |
| 41 | |
Charles Zhao | 6d467e6 | 2020-08-31 10:02:03 +1000 | [diff] [blame] | 42 | // Default handwriting model directory on rootfs. |
| 43 | static constexpr char kHandwritingDefaultModelDir[] = |
| 44 | "/opt/google/chrome/ml_models/handwriting"; |
| 45 | |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 46 | // Returns whether HandwritingLibrary is supported. |
| 47 | static constexpr bool IsHandwritingLibrarySupported() { |
| 48 | return (IsUseLibHandwritingEnabled() || IsUseLibHandwritingDlcEnabled()) && |
| 49 | !IsAsan(); |
| 50 | } |
| 51 | |
| 52 | // Returns whether HandwritingLibrary is supported for unit tests. |
| 53 | static constexpr bool IsHandwritingLibraryUnitTestSupported() { |
| 54 | return IsUseLibHandwritingEnabled() && !IsAsan(); |
| 55 | } |
| 56 | |
Charles Zhao | 6d467e6 | 2020-08-31 10:02:03 +1000 | [diff] [blame] | 57 | // Returns bool of use.ondevice_handwriting. |
| 58 | static constexpr bool IsUseLibHandwritingEnabled() { |
| 59 | return USE_ONDEVICE_HANDWRITING; |
| 60 | } |
| 61 | |
| 62 | // Returns bool of use.ondevice_handwriting_dlc. |
| 63 | static constexpr bool IsUseLibHandwritingDlcEnabled() { |
| 64 | return USE_ONDEVICE_HANDWRITING_DLC; |
| 65 | } |
| 66 | |
| 67 | // Gets the singleton HandwritingLibrary. The singleton is initialized with |
| 68 | // `model_path` on the first call to GetInstance; for the rest of the calls, |
| 69 | // the `model_path` is ignored, and the existing singleton is returned. |
| 70 | // The `model_path` should be either a path on rootfs or a path returned by |
| 71 | // DlcService. |
| 72 | static HandwritingLibrary* GetInstance( |
| 73 | const std::string& model_path = kHandwritingDefaultModelDir); |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 74 | |
alanlxl | 867da6c | 2021-04-19 10:36:35 +1000 | [diff] [blame] | 75 | // Sets a fake impl of HandwritingLibrary which doesn't dlopen the library. |
| 76 | // This is only used in fuzzer test. |
| 77 | static void UseFakeHandwritingLibraryForTesting( |
| 78 | HandwritingLibrary* fake_handwriting_library); |
| 79 | |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 80 | // Get whether the library is successfully initialized. |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 81 | // Initially, the status is `Status::kUninitialized` (this value should never |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 82 | // be returned). |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 83 | // If libhandwriting.so can not be loaded, return `kLoadLibraryFailed`. This |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 84 | // usually means on-device handwriting is not supported. |
| 85 | // If the functions can not be successfully looked up, return |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 86 | // `kFunctionLookupFailed`. |
| 87 | // Return `Status::kOk` if everything works fine. |
alanlxl | 867da6c | 2021-04-19 10:36:35 +1000 | [diff] [blame] | 88 | virtual Status GetStatus() const = 0; |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 89 | |
| 90 | // The following public member functions define the interface functions of |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 91 | // the libhandwriting.so library. Function `InitHandwritingRecognizerLibrary` |
| 92 | // and `DeleteHandwritingResultData` do not need interfaces because the client |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 93 | // won't call it. |
| 94 | |
| 95 | // Creates and returns a handwriting recognizer which is needed for using the |
| 96 | // other interface. The memory is owned by the user and should be deleted |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 97 | // using `DestroyHandwritingRecognizer` after usage. |
alanlxl | 867da6c | 2021-04-19 10:36:35 +1000 | [diff] [blame] | 98 | virtual HandwritingRecognizer CreateHandwritingRecognizer() const = 0; |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 99 | // Load the models with `spec` stores the language, the path to the data files |
| 100 | // of the model (machine learning models, configurations etc.). |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 101 | // Returns true if HandwritingRecognizer is correctly loaded and |
| 102 | // initialized. Returns false otherwise. |
alanlxl | 867da6c | 2021-04-19 10:36:35 +1000 | [diff] [blame] | 103 | virtual bool LoadHandwritingRecognizer(HandwritingRecognizer recognizer, |
| 104 | const std::string& language) const = 0; |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 105 | // Sends the specified `request` to `recognizer`, if succeeds, `result` (which |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 106 | // should not be null) is populated with the recognition result. |
| 107 | // Returns true if succeeds, otherwise returns false. |
alanlxl | 867da6c | 2021-04-19 10:36:35 +1000 | [diff] [blame] | 108 | virtual bool RecognizeHandwriting( |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 109 | HandwritingRecognizer recognizer, |
| 110 | const chrome_knowledge::HandwritingRecognizerRequest& request, |
alanlxl | 867da6c | 2021-04-19 10:36:35 +1000 | [diff] [blame] | 111 | chrome_knowledge::HandwritingRecognizerResult* result) const = 0; |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 112 | // Destroys the handwriting recognizer created by |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 113 | // `CreateHandwritingRecognizer`. Must be called if the handwriting recognizer |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 114 | // will not be used anymore, otherwise there will be memory leak. |
alanlxl | 867da6c | 2021-04-19 10:36:35 +1000 | [diff] [blame] | 115 | virtual void DestroyHandwritingRecognizer( |
| 116 | HandwritingRecognizer recognizer) const = 0; |
| 117 | |
| 118 | protected: |
| 119 | HandwritingLibrary() = default; |
| 120 | virtual ~HandwritingLibrary() = default; |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | friend class base::NoDestructor<HandwritingLibrary>; |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame] | 124 | FRIEND_TEST(HandwritingLibraryTest, CanLoadLibrary); |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 125 | |
Charles Zhao | 6d467e6 | 2020-08-31 10:02:03 +1000 | [diff] [blame] | 126 | // Currently HandwritingLibrary is supported only when the "sanitizer" is not |
| 127 | // enabled (see https://crbug.com/1082632). |
| 128 | static constexpr bool IsAsan() { return __has_feature(address_sanitizer); } |
Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +1000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace ml |
| 132 | |
| 133 | #endif // ML_HANDWRITING_H_ |