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 #include "minikin/MinikinPaint.h"
25
26 #include "FontTestUtils.h"
27 #include "MinikinInternal.h"
28 #include "UnicodeUtils.h"
29
30 namespace minikin {
31
32 const char* SYSTEM_FONT_PATH = "/system/fonts/";
33 const char* SYSTEM_FONT_XML = "/system/etc/fonts.xml";
34
BM_FontCollection_construct(benchmark::State & state)35 static void BM_FontCollection_construct(benchmark::State& state) {
36 std::vector<std::shared_ptr<FontFamily>> families =
37 getFontFamilies(SYSTEM_FONT_PATH, SYSTEM_FONT_XML);
38 while (state.KeepRunning()) {
39 FontCollection::create(families);
40 }
41 }
42
43 BENCHMARK(BM_FontCollection_construct);
44
BM_FontCollection_hasVariationSelector(benchmark::State & state)45 static void BM_FontCollection_hasVariationSelector(benchmark::State& state) {
46 auto collection = FontCollection::create(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 = FontCollection::create(getFontFamilies(SYSTEM_FONT_PATH, SYSTEM_FONT_XML));
83
84 size_t testIndex = state.range(0);
85 state.SetLabel("Itemize: " + ITEMIZE_TEST_CASES[testIndex].labelText);
86
87 uint16_t buffer[64];
88 size_t utf16_length = 0;
89 ParseUnicode(buffer, 64, ITEMIZE_TEST_CASES[testIndex].itemizeText.c_str(), &utf16_length,
90 nullptr);
91 std::vector<FontCollection::Run> result;
92 MinikinPaint paint(collection);
93 paint.localeListId = registerLocaleList(ITEMIZE_TEST_CASES[testIndex].languageTag);
94
95 while (state.KeepRunning()) {
96 result.clear();
97 collection->itemize(U16StringPiece(buffer, utf16_length), paint.fontStyle,
98 paint.localeListId, paint.familyVariant);
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