Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
CmapBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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 "bench/Benchmark.h"
10#include "include/core/SkFont.h"
12#include "src/base/SkRandom.h"
13#include "src/base/SkUTF.h"
14#include "src/core/SkFontPriv.h"
17
18enum {
19 NGLYPHS = 100
20};
21
22namespace {
23struct Rec {
24 const SkCharToGlyphCache& fCache;
25 int fLoops;
26 const SkFont& fFont;
27 const SkUnichar* fText;
28 int fCount;
29};
30} // namespace
31
32typedef void (*TypefaceProc)(const Rec& r);
33
34static void textToGlyphs_proc(const Rec& r) {
35 uint16_t glyphs[NGLYPHS];
36 SkASSERT(r.fCount <= NGLYPHS);
37
38 for (int i = 0; i < r.fLoops; ++i) {
39 r.fFont.textToGlyphs(r.fText, r.fCount*4, SkTextEncoding::kUTF32, glyphs, NGLYPHS);
40 }
41}
42
43static void charsToGlyphs_proc(const Rec& r) {
44 uint16_t glyphs[NGLYPHS];
45 SkASSERT(r.fCount <= NGLYPHS);
46
47 SkTypeface* face = r.fFont.getTypeface();
48 for (int i = 0; i < r.fLoops; ++i) {
49 face->unicharsToGlyphs(r.fText, r.fCount, glyphs);
50 }
51}
52
53static void addcache_proc(const Rec& r) {
54 for (int loop = 0; loop < r.fLoops; ++loop) {
56 for (int i = 0; i < r.fCount; ++i) {
57 cache.addCharAndGlyph(r.fText[i], i);
58 }
59 }
60}
61
62static void findcache_proc(const Rec& r) {
63 for (int loop = 0; loop < r.fLoops; ++loop) {
64 for (int i = 0; i < r.fCount; ++i) {
65 r.fCache.findGlyphIndex(r.fText[i]);
66 }
67 }
68}
69
70class CMAPBench : public Benchmark {
71 TypefaceProc fProc;
72 SkString fName;
73 SkUnichar fText[NGLYPHS];
74 SkFont fFont;
75 SkCharToGlyphCache fCache;
76 int fCount;
77
78public:
79 CMAPBench(TypefaceProc proc, const char name[], int count) {
81
82 fProc = proc;
83 fName.printf("%s_%d", name, count);
84 fCount = count;
85
86 SkRandom rand;
87 for (int i = 0; i < count; ++i) {
88 fText[i] = rand.nextU() & 0xFFFF;
89 fCache.addCharAndGlyph(fText[i], i);
90 }
92 }
93
94 bool isSuitableFor(Backend backend) override {
96 }
97
98protected:
99 const char* onGetName() override {
100 return fName.c_str();
101 }
102
103 void onDraw(int loops, SkCanvas* canvas) override {
104 fProc({fCache, loops, fFont, fText, fCount});
105 }
106
107private:
108
109 using INHERITED = Benchmark;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114constexpr int SMALL = 10;
115
116DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", SMALL); )
117DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", SMALL); )
118DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", SMALL); )
119DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", SMALL); )
120
121constexpr int BIG = 100;
122
123DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", BIG); )
124DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", BIG); )
125DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", BIG); )
126DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", BIG); )
#define DEF_BENCH(code)
Definition Benchmark.h:20
static void charsToGlyphs_proc(const Rec &r)
Definition CmapBench.cpp:43
constexpr int BIG
static void addcache_proc(const Rec &r)
Definition CmapBench.cpp:53
void(* TypefaceProc)(const Rec &r)
Definition CmapBench.cpp:32
static void textToGlyphs_proc(const Rec &r)
Definition CmapBench.cpp:34
constexpr int SMALL
static void findcache_proc(const Rec &r)
Definition CmapBench.cpp:62
@ NGLYPHS
Definition CmapBench.cpp:19
const char * backend
const char * fName
uint16_t glyphs[5]
int count
IsFiniteProc fProc
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kUTF32
uses four byte words to represent all of Unicode
int32_t SkUnichar
Definition SkTypes.h:175
bool isSuitableFor(Backend backend) override
Definition CmapBench.cpp:94
void onDraw(int loops, SkCanvas *canvas) override
CMAPBench(TypefaceProc proc, const char name[], int count)
Definition CmapBench.cpp:79
const char * onGetName() override
Definition CmapBench.cpp:99
void addCharAndGlyph(SkUnichar unichar, SkGlyphID glyph)
void setTypeface(sk_sp< SkTypeface > tf)
Definition SkFont.cpp:90
uint32_t nextU()
Definition SkRandom.h:42
void unicharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const
const char * name
Definition fuchsia.cc:50
sk_sp< SkTypeface > DefaultPortableTypeface()
int fCount