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         std::make_shared<FontCollection>(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 =
47             std::make_shared<FontCollection>(getFontFamilies(SYSTEM_FONT_PATH, SYSTEM_FONT_XML));
48 
49     uint32_t baseCp = state.range(0);
50     uint32_t vsCp = state.range(1);
51 
52     char titleBuffer[64];
53     snprintf(titleBuffer, 64, "hasVariationSelector U+%04X,U+%04X", baseCp, vsCp);
54     state.SetLabel(titleBuffer);
55 
56     while (state.KeepRunning()) {
57         collection->hasVariationSelector(baseCp, vsCp);
58     }
59 }
60 
61 // TODO: Rewrite with BENCHMARK_CAPTURE for better test name.
62 BENCHMARK(BM_FontCollection_hasVariationSelector)
63         ->ArgPair(0x2708, 0xFE0F)
64         ->ArgPair(0x2708, 0xFE0E)
65         ->ArgPair(0x3402, 0xE0100);
66 
67 struct ItemizeTestCases {
68     std::string itemizeText;
69     std::string languageTag;
70     std::string labelText;
71 } ITEMIZE_TEST_CASES[] = {
72         {"'A' 'n' 'd' 'r' 'o' 'i' 'd'", "en", "English"},
73         {"U+4E16", "zh-Hans", "CJK Ideograph"},
74         {"U+4E16", "zh-Hans,zh-Hant,ja,en,es,pt,fr,de",
75          "CJK Ideograph with many language fallback"},
76         {"U+3402 U+E0100", "ja", "CJK Ideograph with variation selector"},
77         {"'A' 'n' U+0E1A U+0E31 U+0645 U+062D U+0648", "en", "Mixture of English, Thai and Arabic"},
78         {"U+2708 U+FE0E", "en", "Emoji with variation selector"},
79         {"U+0031 U+FE0F U+20E3", "en", "KEYCAP"},
80 };
81 
BM_FontCollection_itemize(benchmark::State & state)82 static void BM_FontCollection_itemize(benchmark::State& state) {
83     auto collection =
84             std::make_shared<FontCollection>(getFontFamilies(SYSTEM_FONT_PATH, SYSTEM_FONT_XML));
85 
86     size_t testIndex = state.range(0);
87     state.SetLabel("Itemize: " + ITEMIZE_TEST_CASES[testIndex].labelText);
88 
89     uint16_t buffer[64];
90     size_t utf16_length = 0;
91     ParseUnicode(buffer, 64, ITEMIZE_TEST_CASES[testIndex].itemizeText.c_str(), &utf16_length,
92                  nullptr);
93     std::vector<FontCollection::Run> result;
94     MinikinPaint paint(collection);
95     paint.localeListId = registerLocaleList(ITEMIZE_TEST_CASES[testIndex].languageTag);
96 
97     while (state.KeepRunning()) {
98         result.clear();
99         collection->itemize(U16StringPiece(buffer, utf16_length), paint.fontStyle,
100                             paint.localeListId, paint.familyVariant);
101     }
102 }
103 
104 // TODO: Rewrite with BENCHMARK_CAPTURE once it is available in Android.
105 BENCHMARK(BM_FontCollection_itemize)->Arg(0)->Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(5)->Arg(6);
106 
107 }  // namespace minikin
108