1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "minikin/FontCollection.h"
18
19 #include <memory>
20
21 #include <benchmark/benchmark.h>
22
23 #include "minikin/LocaleList.h"
24
25 #include "FontTestUtils.h"
26 #include "MinikinInternal.h"
27 #include "UnicodeUtils.h"
28
29 namespace minikin {
30
31 const char* SYSTEM_FONT_PATH = "/system/fonts/";
32 const char* SYSTEM_FONT_XML = "/system/etc/fonts.xml";
33
BM_FontCollection_construct(benchmark::State & state)34 static void BM_FontCollection_construct(benchmark::State& state) {
35 std::vector<std::shared_ptr<FontFamily>> families =
36 getFontFamilies(SYSTEM_FONT_PATH, SYSTEM_FONT_XML);
37 while (state.KeepRunning()) {
38 std::make_shared<FontCollection>(families);
39 }
40 }
41
42 BENCHMARK(BM_FontCollection_construct);
43
BM_FontCollection_hasVariationSelector(benchmark::State & state)44 static void BM_FontCollection_hasVariationSelector(benchmark::State& state) {
45 auto collection =
46 std::make_shared<FontCollection>(getFontFamilies(SYSTEM_FONT_PATH, SYSTEM_FONT_XML));
47
48 uint32_t baseCp = state.range(0);
49 uint32_t vsCp = state.range(1);
50
51 char titleBuffer[64];
52 snprintf(titleBuffer, 64, "hasVariationSelector U+%04X,U+%04X", baseCp, vsCp);
53 state.SetLabel(titleBuffer);
54
55 while (state.KeepRunning()) {
56 collection->hasVariationSelector(baseCp, vsCp);
57 }
58 }
59
60 // TODO: Rewrite with BENCHMARK_CAPTURE for better test name.
61 BENCHMARK(BM_FontCollection_hasVariationSelector)
62 ->ArgPair(0x2708, 0xFE0F)
63 ->ArgPair(0x2708, 0xFE0E)
64 ->ArgPair(0x3402, 0xE0100);
65
66 struct ItemizeTestCases {
67 std::string itemizeText;
68 std::string languageTag;
69 std::string labelText;
70 } ITEMIZE_TEST_CASES[] = {
71 {"'A' 'n' 'd' 'r' 'o' 'i' 'd'", "en", "English"},
72 {"U+4E16", "zh-Hans", "CJK Ideograph"},
73 {"U+4E16", "zh-Hans,zh-Hant,ja,en,es,pt,fr,de",
74 "CJK Ideograph with many language fallback"},
75 {"U+3402 U+E0100", "ja", "CJK Ideograph with variation selector"},
76 {"'A' 'n' U+0E1A U+0E31 U+0645 U+062D U+0648", "en", "Mixture of English, Thai and Arabic"},
77 {"U+2708 U+FE0E", "en", "Emoji with variation selector"},
78 {"U+0031 U+FE0F U+20E3", "en", "KEYCAP"},
79 };
80
BM_FontCollection_itemize(benchmark::State & state)81 static void BM_FontCollection_itemize(benchmark::State& state) {
82 auto collection =
83 std::make_shared<FontCollection>(getFontFamilies(SYSTEM_FONT_PATH, SYSTEM_FONT_XML));
84
85 size_t testIndex = state.range(0);
86 state.SetLabel("Itemize: " + ITEMIZE_TEST_CASES[testIndex].labelText);
87
88 uint16_t buffer[64];
89 size_t utf16_length = 0;
90 ParseUnicode(buffer, 64, ITEMIZE_TEST_CASES[testIndex].itemizeText.c_str(), &utf16_length,
91 nullptr);
92 std::vector<FontCollection::Run> result;
93 MinikinPaint paint(collection);
94 paint.localeListId = registerLocaleList(ITEMIZE_TEST_CASES[testIndex].languageTag);
95
96 while (state.KeepRunning()) {
97 result.clear();
98 collection->itemize(buffer, utf16_length, paint, &result);
99 }
100 }
101
102 // TODO: Rewrite with BENCHMARK_CAPTURE once it is available in Android.
103 BENCHMARK(BM_FontCollection_itemize)->Arg(0)->Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(5)->Arg(6);
104
105 } // namespace minikin
106