已合并
fix icu load error #2319
Amber创建于 6月3日
fix icu load error #2319
已合并
共 4 个文件变更+95-5
| @@ -86,6 +86,9 @@ if (skia_use_icu || skia_use_client_icu || skia_use_libgrapheme || | |||
| 86 | if (!skia_use_system_icu) { | 86 | if (!skia_use_system_icu) { |
| 87 | defines += [ "U_DISABLE_RENAMING=1" ] | 87 | defines += [ "U_DISABLE_RENAMING=1" ] |
| 88 | } | 88 | } |
| 89 | if (is_ohos) { | ||
| 90 | defines += [ "SK_BUILD_FOR_OHOS" ] | ||
| 91 | } | ||
| 89 | 92 | ||
| 90 | sources = skia_unicode_icu_bidi_sources | 93 | sources = skia_unicode_icu_bidi_sources |
| 91 | sources += skia_unicode_bidi_full_sources | 94 | sources += skia_unicode_bidi_full_sources |
| @@ -42,8 +42,9 @@ | |||
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | 44 | ||
| 45 | #if defined(SK_USING_THIRD_PARTY_ICU) && defined(SK_BUILD_FOR_WIN) | 45 | #if (defined(SK_USING_THIRD_PARTY_ICU) && defined(SK_BUILD_FOR_WIN)) || \ |
| 46 | #include "SkLoadICU.h" | 46 | defined(SK_BUILD_FOR_OHOS) |
| 47 | |||
| 47 | 48 | ||
| 48 | 49 | ||
| 49 | 50 | ||
| @@ -919,7 +920,8 @@ namespace SkUnicodes::ICU { | |||
| 919 | sk_sp<SkUnicode> Make() { | 920 | sk_sp<SkUnicode> Make() { |
| 920 | // We haven't yet created a way to encode the ICU data for assembly on Windows, | 921 | // We haven't yet created a way to encode the ICU data for assembly on Windows, |
| 921 | // so we use a helper library to load icudtl.dat from the harddrive. | 922 | // so we use a helper library to load icudtl.dat from the harddrive. |
| 922 | #if defined(SK_USING_THIRD_PARTY_ICU) && defined(SK_BUILD_FOR_WIN) | 923 | #if (defined(SK_USING_THIRD_PARTY_ICU) && defined(SK_BUILD_FOR_WIN)) || \ |
| 924 | defined(SK_BUILD_FOR_OHOS) | ||
| 923 | if (!SkLoadICU()) { | 925 | if (!SkLoadICU()) { |
| 924 | static SkOnce once; | 926 | static SkOnce once; |
| 925 | once([] { SkDEBUGF("SkLoadICU() failed!\n"); }); | 927 | once([] { SkDEBUGF("SkLoadICU() failed!\n"); }); |
| @@ -119,3 +119,87 @@ bool SkLoadICU() { | |||
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | 121 | ||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | static const char* kICUDataPaths[] = { | ||
| 138 | "/usr/icu/icudt74l.dat", | ||
| 139 | "/system/usr/icu/icudt74l.dat", | ||
| 140 | }; | ||
| 141 | |||
| 142 | static void* posix_mmap(const char* dataFile) { | ||
| 143 | if (!dataFile) { | ||
| 144 | return nullptr; | ||
| 145 | } | ||
| 146 | int fd = open(dataFile, O_RDONLY); | ||
| 147 | if (fd < 0) { | ||
| 148 | fprintf(stderr, "SkIcuLoader: datafile missing: %s.\n", dataFile); | ||
| 149 | return nullptr; | ||
| 150 | } | ||
| 151 | struct stat st; | ||
| 152 | if (fstat(fd, &st) < 0) { | ||
| 153 | fprintf(stderr, "SkIcuLoader: datafile stat error.\n"); | ||
| 154 | close(fd); | ||
| 155 | return nullptr; | ||
| 156 | } | ||
| 157 | void* addr = mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0); | ||
| 158 | close(fd); | ||
| 159 | if (addr == MAP_FAILED) { | ||
| 160 | fprintf(stderr, "SkIcuLoader: datafile mmap error.\n"); | ||
| 161 | return nullptr; | ||
| 162 | } | ||
| 163 | return addr; | ||
| 164 | } | ||
| 165 | |||
| 166 | static bool init_icu(void* addr) { | ||
| 167 | UErrorCode err = U_ZERO_ERROR; | ||
| 168 | udata_setCommonData(addr, &err); | ||
| 169 | if (err != U_ZERO_ERROR && err != U_USING_DEFAULT_WARNING) { | ||
| 170 | fprintf(stderr, "udata_setCommonData() returned %d.\n", (int)err); | ||
| 171 | return false; | ||
| 172 | } | ||
| 173 | err = U_ZERO_ERROR; | ||
| 174 | udata_setFileAccess(UDATA_ONLY_PACKAGES, &err); | ||
| 175 | if (err != U_ZERO_ERROR) { | ||
| 176 | fprintf(stderr, "udata_setFileAccess() returned %d.\n", (int)err); | ||
| 177 | return false; | ||
| 178 | } | ||
| 179 | return true; | ||
| 180 | } | ||
| 181 | |||
| 182 | static bool load_from(const char* path) { | ||
| 183 | if (void* addr = posix_mmap(path)) { | ||
| 184 | if (init_icu(addr)) { | ||
| 185 | return true; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | return false; | ||
| 189 | } | ||
| 190 | |||
| 191 | bool SkLoadICU() { | ||
| 192 | static bool good = false; | ||
| 193 | static std::once_flag flag; | ||
| 194 | std::call_once(flag, []() { | ||
| 195 | for (auto path : kICUDataPaths) { | ||
| 196 | if (load_from(path)) { | ||
| 197 | good = true; | ||
| 198 | return; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | }); | ||
| 202 | return good; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| @@ -7,10 +7,11 @@ | |||
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | #if defined(_WIN32) && defined(SK_USING_THIRD_PARTY_ICU) | 10 | #if (defined(_WIN32) && defined(SK_USING_THIRD_PARTY_ICU)) || \ |
| 11 | defined(SK_BUILD_FOR_OHOS) | ||
| 11 | bool SkLoadICU(); | 12 | bool SkLoadICU(); |
| 12 | 13 | ||
| 13 | static inline bool SkLoadICU() { return true; } | 14 | static inline bool SkLoadICU() { return true; } |
| 14 | #endif // defined(_WIN32) && defined(SK_USING_THIRD_PARTY_ICU) | 15 | #endif |
| 15 | 16 | ||
| 16 | 17 | ||
这个文件map了之后就不会卸载了?符合预期吗