| 1 |
/* |
| 2 |
* Copyright 2018 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <vector> |
| 9 |
|
| 10 |
#include "bench/Benchmark.h" |
| 11 |
#include "include/core/SkFont.h" |
| 12 |
#include "include/core/SkFontTypes.h" |
| 13 |
#include "include/core/SkTypeface.h" |
| 14 |
#include "src/base/SkRandom.h" |
| 15 |
#include "src/base/SkUTF.h" |
| 16 |
#include "src/base/SkUtils.h" |
| 17 |
#include "tools/fonts/FontToolUtils.h" |
| 18 |
|
| 19 |
// From Project Guttenberg. This is UTF-8 text. |
| 20 |
static const char* atext[] = { |
| 21 |
"Call me Ishmael. Some years ago--never mind how", |
| 22 |
"long precisely --having little or no money in my purse, and nothing particular", |
| 23 |
"to interest me on shore, I thought I would sail about a little and see the", |
| 24 |
"watery part of the world. It is a way I have of driving off the spleen, and", |
| 25 |
"regulating the circulation. Whenever I find myself growing grim about the", |
| 26 |
"mouth; whenever it is a damp, drizzly November in my soul; whenever I find", |
| 27 |
"myself involuntarily pausing before coffin warehouses, and bringing up the", |
| 28 |
"rear of every funeral I meet; and especially whenever my hypos get such an", |
| 29 |
"upper hand of me, that it requires a strong moral principle to prevent me", |
| 30 |
"from deliberately stepping into the street, and methodically knocking", |
| 31 |
"people's hats off--then, I account it high time to get to sea as soon as I can.", |
| 32 |
"", |
| 33 |
"This is my substitute for pistol and ball. With a philosophical flourish", |
| 34 |
"Cato throws himself upon his sword; I quietly take to the ship. There is", |
| 35 |
"nothing surprising in this. If they but knew it, almost all men in their", |
| 36 |
"degree, some time or other, cherish very nearly the same feelings towards the", |
| 37 |
"ocean with me. There now is your insular city of the Manhattoes, belted round", |
| 38 |
"by wharves as Indian isles by coral reefs--commerce surrounds it with her surf.", |
| 39 |
"", |
| 40 |
"Right and left, the streets take you waterward. Its extreme down-town is", |
| 41 |
"the battery, where that noble mole is washed by waves, and cooled by", |
| 42 |
"breezes, which a few hours previous were out of sight of land. Look at the", |
| 43 |
"crowds of water-gazers there. Circumambulate the city of a dreamy Sabbath", |
| 44 |
"afternoon. Go from Corlears Hook to Coenties Slip, and from thence, by", |
| 45 |
"Whitehall northward. What do you see?--Posted like silent sentinels all", |
| 46 |
"around the town, stand thousands upon thousands of mortal men fixed in ocean", |
| 47 |
"reveries. Some leaning against the spiles; some seated upon the pier-heads;", |
| 48 |
"some looking over the bulwarks glasses!", |
| 49 |
"of ships from China; some high aloft in the rigging, as if striving to get a", |
| 50 |
"still better seaward peep. But these are all landsmen; of week days pent up", |
| 51 |
"in lath and plaster--tied to counters, nailed to benches, clinched to desks.", |
| 52 |
"How then is this? Are the green fields gone? What do they here? But look!", |
| 53 |
"here come more crowds, pacing straight for the water, and seemingly bound for", |
| 54 |
"a dive. Strange! Nothing will content them but the extremest limit of the", |
| 55 |
"land; loitering under the shady lee of yonder warehouses will not suffice.", |
| 56 |
"No. They must get just as nigh the water as they possibly can without falling", |
| 57 |
"in. And there they stand--miles of them--leagues. Inlanders all, they come", |
| 58 |
"from lanes and alleys, streets and avenues, --north, east, south, and west.", |
| 59 |
"Yet here they all unite. Tell me, does the magnetic virtue of the needles of", |
| 60 |
"the compasses of all those ships attract them thither? Once more. Say, you", |
| 61 |
"are in the country; in some high land of lakes. Take almost any path you", |
| 62 |
"please, and ten to one it carries you down in a dale, and leaves you there", |
| 63 |
"by a pool in the stream. There is magic in it. Let the most absent-minded", |
| 64 |
"of men be plunged in his deepest reveries--stand that man on his legs, set his", |
| 65 |
"feet a-going, and he will infallibly lead you to water, if water there be in", |
| 66 |
"all that region. Should you ever be athirst in the great American desert,", |
| 67 |
"try this experiment, if your caravan happen to be supplied with a metaphysical", |
| 68 |
"professor. Yes, as every one knows, meditation and water are wedded for ever.", |
| 69 |
"", |
| 70 |
"But here is an artist. He desires to paint you the dreamiest, shadiest,", |
| 71 |
"quietest, most enchanting bit of romantic landscape in all the valley of the", |
| 72 |
"Saco. What is the chief element he employs? There stand his trees, each with", |
| 73 |
"a hollow trunk, as if a hermit and a crucifix were within; and here sleeps", |
| 74 |
"his meadow, and there sleep his cattle; and up from yonder cottage goes a", |
| 75 |
"sleepy smoke. Deep into distant woodlands winds a mazy way, reaching to", |
| 76 |
"overlapping spurs of mountains bathed in their hill-side blue. But though", |
| 77 |
"the picture lies thus tranced, and though this pine-tree shakes down its sighs", |
| 78 |
"like leaves upon this shepherd's head, yet all were vain, unless the", |
| 79 |
"shepherd's eye were fixed upon the magic stream before him. Go visit the", |
| 80 |
"Prairies in June,", |
| 81 |
"", |
| 82 |
"when for scores on scores of miles you wade knee-deep among Tiger-lilies--what", |
| 83 |
"is the one charm wanting? --Water --there is not a drop of water there! Were", |
| 84 |
"Niagara but a cataract of sand, would you travel your thousand miles to see", |
| 85 |
"it? Why did the poor poet of Tennessee, upon suddenly receiving two handfuls", |
| 86 |
"of silver, deliberate whether to buy him a coat, which he sadly needed, or", |
| 87 |
"invest his money in a pedestrian trip to Rockaway Beach? Why is almost every", |
| 88 |
"robust healthy boy with a robust healthy soul in him, at some time or other", |
| 89 |
"crazy to go to sea? Why upon your first voyage as a passenger, did you", |
| 90 |
"yourself feel such a mystical vibration, when first told that you and your", |
| 91 |
"ship were now out of sight of land? Why did the old Persians hold the sea", |
| 92 |
"holy? Why did the Greeks give it a separate deity, and own brother of Jove?", |
| 93 |
"Surely all this is not without meaning. And still deeper the meaning of that", |
| 94 |
"story of Narcissus, who because he could not grasp the tormenting, mild image", |
| 95 |
"he saw in the fountain, plunged into it and was drowned. But that same", |
| 96 |
"image, we ourselves see in all rivers and oceans. It is the image of the", |
| 97 |
"ungraspable phantom of life; and this is the key to it all. Now, when I say", |
| 98 |
"that I am in the habit of going to sea whenever I begin to grow hazy about the", |
| 99 |
"eyes, and begin to be over conscious of my lungs, I do not mean to have it", |
| 100 |
"inferred that I ever go to sea as a passenger. For to go as a passenger you", |
| 101 |
"must needs have a purse, and a purse is but a rag unless you have something", |
| 102 |
"in it. Besides, passengers get sea-sick --grow quarrelsome --don't sleep of", |
| 103 |
"nights --do not enjoy themselves much, as a general thing; --no, I never go as a", |
| 104 |
"passenger; nor, though I am something of a salt, do I ever go to sea as a", |
| 105 |
"Commodore, or a Captain, or a Cook. I abandon the glory and distinction of", |
| 106 |
"such offices to those who like them. For my part, I abominate all honorable", |
| 107 |
"respectable toils, trials, and tribulations of every kind whatsoever. It is", |
| 108 |
"quite as much as I can do to take care of myself, without taking care of", |
| 109 |
"ships, barques, brigs, schooners, and what not. And as for going as cook, --", |
| 110 |
"though I confess there is considerable glory in that, a cook being a sort of", |
| 111 |
"officer on ship-board --yet, somehow, I never fancied broiling fowls; --though", |
| 112 |
"once broiled, judiciously buttered, and judgmatically salted and peppered,", |
| 113 |
"there is no one who will", |
| 114 |
"", |
| 115 |
"speak more respectfully, not to say reverentially, of a broiled fowl than I", |
| 116 |
"will. It is out of the idolatrous dotings of the old Egyptians upon broiled", |
| 117 |
"ibis and roasted river horse, that you see the mummies of those creatures in", |
| 118 |
"their huge bake-houses the pyramids. No, when I go to sea, I go as a simple", |
| 119 |
"sailor, right before the mast, plumb down into the forecastle, aloft there", |
| 120 |
"to the royal mast-head. True, they rather order me about some, and make me", |
| 121 |
"jump from spar to spar, like a grasshopper in a May meadow. And at first,", |
| 122 |
"this sort of thing is unpleasant enough. It touches one's sense of honor,", |
| 123 |
"particularly if you come of an old established family in the land, the van", |
| 124 |
"Rensselaers, or Randolphs, or Hardicanutes. And more than all, if just", |
| 125 |
"previous to putting your hand into the tar-pot, you have been lording it as a", |
| 126 |
"country schoolmaster, making the tallest boys stand in awe of you. The", |
| 127 |
"transition is a keen one, I assure you, from the schoolmaster to a sailor,", |
| 128 |
"and requires a strong decoction of Seneca and the Stoics to enable you to grin", |
| 129 |
"and bear it. But even this wears off in time. What of it, if some old hunks", |
| 130 |
"of a sea-captain orders me to get a broom and sweep down the decks? What does", |
| 131 |
"that indignity amount to, weighed, I mean, in the scales of the New", |
| 132 |
"Testament? Do you think the archangel Gabriel thinks anything the less of me,", |
| 133 |
"because I promptly and respectfully obey that old hunks in that particular", |
| 134 |
"instance? Who aint a slave? Tell me that. Well, then, however the old", |
| 135 |
"sea-captains may order me about--however they may thump and punch me about, I", |
| 136 |
"have the satisfaction of knowing that it is all right; that everybody else", |
| 137 |
"is one way or other served in much the same way -- either in a physical or", |
| 138 |
"metaphysical point of view, that is; and so the universal thump is passed", |
| 139 |
"round, and all hands should rub each other's shoulder-blades, and be", |
| 140 |
"content. Again, I always go to sea as a sailor, because they make a point of", |
| 141 |
"paying me for my trouble, whereas they never pay passengers a single penny", |
| 142 |
"that I ever heard of. On the contrary, passengers themselves must pay. And", |
| 143 |
"there is all the difference in the world between paying and being paid. The", |
| 144 |
"act of paying is perhaps the most uncomfortable infliction that the two", |
| 145 |
"orchard thieves entailed upon us." |
| 146 |
}; |
| 147 |
|
| 148 |
static const char* ctext[] = { |
| 149 |
"第一回", |
| 150 |
"胡秀才告狀鳴冤 施賢臣得夢訪案", |
| 151 |
"", |
| 152 |
" 話說江都縣有一秀才,姓胡,名登舉。他的父母為人所殺,頭顱不見。胡登舉合家", |
| 153 |
"嚇得膽裂魂飛,慌忙出門,去稟縣主。", |
| 154 |
"", |
| 155 |
" 跑到縣衙,正遇升堂,就進去喊冤。走至堂上,打了一躬,手舉呈詞,口稱:「父", |
| 156 |
"師在上,門生禍從天降。叩稟老父師,即賜嚴拿。」說著,將呈詞遞上。書吏接過,鋪", |
| 157 |
"在公案。施公靜心細閱。上寫:具呈生員胡登舉,祖居江都縣。生父曾作翰林,告老家", |
| 158 |
"居,廣行善事,憐恤窮苦,並無苛刻待人之事。不意於某日夜間,生父母閉戶安眠。至", |
| 159 |
"天曉,生往請安,父母俱不言語。生情急,踢開門戶,見父母屍身俱在牀上,兩個人頭", |
| 160 |
",並沒蹤影。生忝居學校,父母如此死法,何以身列校庠對雙親而無愧乎?為此具呈,", |
| 161 |
"嚎叩老父師大人恩准,速賜拿獲兇手,庶生冤仇得雪。感戴無既。沾仁。上呈。", |
| 162 |
"", |
| 163 |
" 施公看罷,不由點頭,暗暗吃驚,想道:「夤夜入院,非奸即盜。胡翰林夫婦年老", |
| 164 |
"被殺,而不竊去財物,且將人頭拿去,其中情由,顯係仇謀。此宗無題文章,令人如何", |
| 165 |
"做法?」為難良久,說道:「即委捕廳四老爺,前去驗屍。你只管入殮,自有頭緒結斷", |
| 166 |
"。」胡秀才一聽,只得含淚下堂,出衙回家,伺候驗屍。", |
| 167 |
"", |
| 168 |
" 且說施公吩咐速去知會四衙,往胡家驗屍呈報,把呈詞收入袖內,吩咐退堂。進內", |
| 169 |
"書房坐下,長隨送茶畢,用過了飯,把呈詞取出,鋪在案上翻閱。低頭細想,此案難結", |
| 170 |
"。欠身伸手,在書架上拿了古書一部,係《拍案稱奇》,放在桌上要看;對證此案,即", |
| 171 |
"日好斷這沒頭之事。將《拍案稱奇》,自頭至尾看完,又取了一部,係海瑞參拿嚴嵩的", |
| 172 |
"故事。不覺困倦,放下書本,伏於書案之上,朦朧打睡。夢中看見外邊牆頭之下,有群", |
| 173 |
"黃雀兒九隻,點頭搖尾,唧哩喳啦,不住亂叫。施公一見,心中甚驚。又聽見地上哼哼", |
| 174 |
"唧唧的豬叫;原來是油光兒的七個小豬兒,望著賢臣亂叫。施公夢中稱奇,方要去細看", |
| 175 |
",那九隻黃雀兒,一齊飛下牆來,與地下七個小豬兒,點頭亂噪。那七個小豬兒,站起", |
| 176 |
"身來,望黃雀拱抓,口內哼哼亂叫。雀噪豬叫,偶然起了一陣怪風,把豬雀都裹了去了", |
| 177 |
"。施公夢中一聲驚覺,大叫說:「奇怪的事!」施安在旁邊站立,見主人如此驚叫,不", |
| 178 |
"知何故,連忙叫:「老爺醒來!醒來!」施公聽言,抬頭睜眼,沉吟多時。想夢中之事", |
| 179 |
",說:「奇哉!怪哉!」就問施安這天有多時了。施安答道:「日色西沉了。」施公點", |
| 180 |
"頭,又問:「方才你可見些什麼東西沒有?」施安說:「並沒見什麼東西,倒有一陣風", |
| 181 |
"刮過牆去。」施公聞言,心中細想,這九隻黃雀、七個小豬奇怪,想來內有曲情。將書", |
| 182 |
"擱在架上,前思後想,一夜未睡。直到天明,淨面整衣,吩咐傳梆升堂。坐下,抽籤叫", |
| 183 |
"快頭英公然、張子仁上來。二人走至堂上,跪下叩頭。施公就將昨日夢見九隻黃雀、七", |
| 184 |
"個小豬為題出簽差人,說:「限你二人五日之期,將九黃、七豬拿來,如若遲延,重責", |
| 185 |
"不饒。」將簽遞於二人。二人跪趴半步,口稱:「老爺容稟:小的們請個示來。", |
| 186 |
"", |
| 187 |
" 這九黃、七豬,是兩個人名,還是兩個物名,現在何處?求老爺吩咐明白,小的們", |
| 188 |
"好去訪拿。」言罷叩頭。施公一聽,說道:「無用奴才,連個九黃、七豬都不知道,還", |
| 189 |
"在本縣應役麼?分明偷閒躲懶,安心抗差玩法。」吩咐:「給我拉下去打!」兩邊發喊", |
| 190 |
"按倒,每人打了十五板。二人跪下叩頭,復又討示,叫聲:「老爺,究竟吩咐明白,待", |
| 191 |
"小的們好去拿人。」施公聞言,心中不由大怒,說:「好大膽的奴才!本縣深知你二人", |
| 192 |
"久慣應役,極會搪塞,如敢再行囉唣,定加重責!」二人聞言,萬分無奈,站起退下去", |
| 193 |
",訪拿九黃、七豬而去。施公也隨退堂。", |
| 194 |
"", |
| 195 |
" 施公一連五日,假裝有恙,並未升堂。到了第六日,一早吩咐點鼓升堂,坐下。衙", |
| 196 |
"役人等伺候。只見一人走至公堂案下,手捧呈詞,口稱:「父師,門生胡登舉父母被殺", |
| 197 |
"之冤,求父師明鑒。倘遲久不獲,兇犯走脫難捉。且生員讀書一場,豈不有愧?如門生", |
| 198 |
"另去投呈伸冤,老父台那時休怨!」言罷一躬,將呈遞上。施公帶笑道:「賢契不必急", |
| 199 |
"躁。本縣已經差人明捕暗訪,專拿形跡可疑之人,審得自然替你申冤。」胡登舉無奈,", |
| 200 |
"說道:「父台!速替門生伸冤,感恩不盡!」施公說:「賢契請回,催呈留下。」胡登", |
| 201 |
"舉打躬下堂,出衙回家。且說施公為難多會,方要提胡宅管家的審問,只見公差英公然", |
| 202 |
"、張子仁上堂,跪下回稟:「小的二人,並訪不著九黃、七豬,求老爺寬限。」", |
| 203 |
"", |
| 204 |
" 施公聞言,激惱成怒,喝叫左右拉下,每人打十五大板。不容分說,只打的哀求不", |
| 205 |
"止,鮮血直流。打完提褲,戰戰兢兢,跪在地下,口尊:「老爺,叩討明示,以便好去", |
| 206 |
"捉人。」施公聞言無奈,硬著心腸說道:「再寬你們三日限期,如其再不捉拿兇犯,定", |
| 207 |
"行處死!」二差聞言,篩糠打戰,只是磕頭,如雞食碎米一般。施公又說:「你們不必", |
| 208 |
"多說,快快去捕要緊。」施公想二役兩次受刑,亦覺心中不忍,退堂進內。可憐二人還", |
| 209 |
"在下面叩頭,大叫:「老爺,可憐小的們性命罷!」言畢,又是咚咚的叩頭。縣堂上未", |
| 210 |
"散的三班六房之人,見二人這樣,個個兔死狐悲,歎惜不止,一齊說:「罷呀!起來罷", |
| 211 |
"!老爺進去了,還求那個?」二人聞言,抬頭不看見老爺,忍氣站起,腿帶棒傷,身形", |
| 212 |
"晃亂。旁邊上來四個人,用手挽架下堂。"}; |
| 213 |
|
| 214 |
|
| 215 |
class UtfToGlyph : public Benchmark { |
| 216 |
public: |
| 217 |
UtfToGlyph(SkTextEncoding encoding, const char* (*text), int lineCount, const char* name) |
| 218 |
: fEncoding{encoding} |
| 219 |
, fText{text} |
| 220 |
, fLineCount{lineCount} |
| 221 |
, fName{name} { } |
| 222 |
|
| 223 |
protected: |
| 224 |
const char* onGetName() override { |
| 225 |
return fName; |
| 226 |
} |
| 227 |
|
| 228 |
bool isSuitableFor(Backend backend) override { |
| 229 |
return backend == Backend::kNonRendering; |
| 230 |
} |
| 231 |
|
| 232 |
void onDelayedSetup() override { |
| 233 |
int maxGlyphs = 0; |
| 234 |
for (int i = 0; i < fLineCount; i++) { |
| 235 |
fLines.emplace_back(this->convertLine(i)); |
| 236 |
maxGlyphs = std::max(maxGlyphs, fLines.back()->glyphCount); |
| 237 |
} |
| 238 |
fGlyphIds.insert(fGlyphIds.begin(), maxGlyphs, 0); |
| 239 |
fTypeface = ToolUtils::CreateTestTypeface("monospace", SkFontStyle()); |
| 240 |
} |
| 241 |
|
| 242 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 243 |
SkFont font(fTypeface); |
| 244 |
// Do more loops to reduce variance. |
| 245 |
for (int i = 0; i < loops * 3; ++i) { |
| 246 |
for (auto& line : fLines) { |
| 247 |
font.textToGlyphs(line->utf.data(), line->utf.size(), fEncoding, |
| 248 |
{fGlyphIds.data(), (size_t)line->glyphCount}); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
private: |
| 254 |
struct Line { |
| 255 |
int glyphCount = 0; |
| 256 |
std::vector<char> utf; |
| 257 |
}; |
| 258 |
|
| 259 |
std::unique_ptr<Line> convertLine(int lineIndex) { |
| 260 |
std::unique_ptr<Line> result = std::make_unique<Line>(); |
| 261 |
|
| 262 |
const char* cursor = fText[lineIndex]; |
| 263 |
size_t len = strlen(cursor); |
| 264 |
const char* end = cursor + len; |
| 265 |
result->glyphCount = SkUTF::CountUTF8(cursor, len); |
| 266 |
std::vector<char>& bytes = result->utf; |
| 267 |
while (cursor < end) { |
| 268 |
SkUnichar u = SkUTF::NextUTF8(&cursor, end); |
| 269 |
|
| 270 |
switch (fEncoding) { |
| 271 |
case SkTextEncoding::kUTF8: { |
| 272 |
char buffer[SkUTF::kMaxBytesInUTF8Sequence]; |
| 273 |
size_t count = SkUTF::ToUTF8(u, buffer); |
| 274 |
result->utf.insert(bytes.end(), buffer, buffer + count); |
| 275 |
break; |
| 276 |
} |
| 277 |
case SkTextEncoding::kUTF16: { |
| 278 |
uint16_t buffer[2]; |
| 279 |
size_t count = SkUTF::ToUTF16(u, buffer); |
| 280 |
result->utf.insert(bytes.end(), (char *)buffer, (char *)buffer + count * 2); |
| 281 |
break; |
| 282 |
} |
| 283 |
case SkTextEncoding::kUTF32: { |
| 284 |
result->utf.insert(bytes.end(), (char*) &u, (char*) &u + 4); |
| 285 |
break; |
| 286 |
} |
| 287 |
default: |
| 288 |
SK_ABORT("Bad encoding"); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
return result; |
| 293 |
} |
| 294 |
|
| 295 |
SkTextEncoding fEncoding; |
| 296 |
std::vector<std::unique_ptr<Line>> fLines; |
| 297 |
std::vector<SkGlyphID> fGlyphIds; |
| 298 |
sk_sp<SkTypeface> fTypeface; |
| 299 |
const char** fText; |
| 300 |
int fLineCount; |
| 301 |
const char* fName; |
| 302 |
}; |
| 303 |
|
| 304 |
DEF_BENCH(return new UtfToGlyph(SkTextEncoding::kUTF32, ctext, std::size(ctext), |
| 305 |
"SkTypefaceUTF32ToGlyphCN");) |
| 306 |
DEF_BENCH(return new UtfToGlyph(SkTextEncoding::kUTF16, ctext, std::size(ctext), |
| 307 |
"SkTypefaceUTF16ToGlyphCN");) |
| 308 |
DEF_BENCH(return new UtfToGlyph(SkTextEncoding::kUTF8, ctext, std::size(ctext), |
| 309 |
"SkTypefaceUTF8ToGlyphCN");) |
| 310 |
|
| 311 |
|
| 312 |
DEF_BENCH(return new UtfToGlyph(SkTextEncoding::kUTF32, atext, std::size(atext), |
| 313 |
"SkTypefaceUTF32ToGlyphAscii");) |
| 314 |
DEF_BENCH(return new UtfToGlyph(SkTextEncoding::kUTF16, atext, std::size(atext), |
| 315 |
"SkTypefaceUTF16ToGlyphAscii");) |
| 316 |
DEF_BENCH(return new UtfToGlyph(SkTextEncoding::kUTF8, atext, std::size(atext), |
| 317 |
"SkTypefaceUTF8ToGlyphAscii");) |
| 318 |
|
| 319 |
class FontGetBounds : public Benchmark { |
| 320 |
sk_sp<SkTypeface> fTypeface; |
| 321 |
std::vector<SkGlyphID> fGlyphs; // input |
| 322 |
std::vector<SkRect> fBounds; // output |
| 323 |
public: |
| 324 |
FontGetBounds() {} |
| 325 |
|
| 326 |
protected: |
| 327 |
const char* onGetName() override { |
| 328 |
return "Font_getBounds"; |
| 329 |
} |
| 330 |
|
| 331 |
bool isSuitableFor(Backend backend) override { |
| 332 |
return backend == Backend::kNonRendering; |
| 333 |
} |
| 334 |
|
| 335 |
void onDelayedSetup() override { |
| 336 |
// Just need a font with a reasonable number of glyphs for this test |
| 337 |
fTypeface = ToolUtils::CreateTypefaceFromResource("fonts/Roboto-Regular.ttf"); |
| 338 |
|
| 339 |
// simulate a big text hunk that SkParagraph might have to visit |
| 340 |
constexpr size_t N = 8192; |
| 341 |
fGlyphs.resize(N); |
| 342 |
fBounds.resize(N); |
| 343 |
|
| 344 |
SkRandom rand; |
| 345 |
const size_t numGlyphs = fTypeface->countGlyphs(); |
| 346 |
for (auto& glyph : fGlyphs) { |
| 347 |
glyph = rand.nextU() % numGlyphs; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 352 |
SkFont font(fTypeface); |
| 353 |
// Do more loops to reduce variance. |
| 354 |
for (int i = 0; i < loops * 8; ++i) { |
| 355 |
font.getBounds(fGlyphs, fBounds, nullptr); |
| 356 |
} |
| 357 |
} |
| 358 |
}; |
| 359 |
|
| 360 |
DEF_BENCH(return new FontGetBounds;) |