Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
FontCacheBench.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 "include/core/SkPath.h"
15#include "src/core/SkChecksum.h"
17
19
20#define gUniqueGlyphIDs_Sentinel 0xFFFF
21
22static int count_glyphs(const uint16_t start[]) {
23 const uint16_t* curr = start;
24 while (*curr != gUniqueGlyphIDs_Sentinel) {
25 curr += 1;
26 }
27 return static_cast<int>(curr - start);
28}
29
30class FontCacheBench : public Benchmark {
31public:
33
34protected:
35 const char* onGetName() override {
36 return "fontcache";
37 }
38
39 void onDraw(int loops, SkCanvas* canvas) override {
41 font.setEdging(SkFont::Edging::kAntiAlias);
42
43 const uint16_t* array = gUniqueGlyphIDs;
44 while (*array != gUniqueGlyphIDs_Sentinel) {
45 int count = count_glyphs(array);
46 for (int i = 0; i < loops; ++i) {
47 (void)font.measureText(array, count * sizeof(uint16_t), SkTextEncoding::kGlyphID);
48 }
49 array += count + 1; // skip the sentinel
50 }
51 }
52
53private:
54 using INHERITED = Benchmark;
55};
56
57///////////////////////////////////////////////////////////////////////////////
58
59static uint32_t rotr(uint32_t value, unsigned bits) {
60 return (value >> bits) | (value << (32 - bits));
61}
62
63typedef uint32_t (*HasherProc)(uint32_t);
64
65static uint32_t hasher0(uint32_t value) {
66 value = value ^ (value >> 16);
67 return value ^ (value >> 8);
68}
69
70static const struct {
71 const char* fName;
73} gRec[] = {
74 { "hasher0", hasher0 },
75 { "hasher2", SkChecksum::Mix },
76};
77
78#define kMaxHashBits 12
79#define kMaxHashCount (1 << kMaxHashBits)
80
81static int count_collisions(const uint16_t array[], int count, HasherProc proc,
82 unsigned hashMask) {
83 char table[kMaxHashCount];
84 sk_bzero(table, sizeof(table));
85
86 int collisions = 0;
87 for (int i = 0; i < count; ++i) {
88 int index = proc(array[i]) & hashMask;
89 collisions += table[index];
90 table[index] = 1;
91 }
92 return collisions;
93}
94
95static void dump_array(const uint16_t array[], int count) {
96 for (int i = 0; i < count; ++i) {
97 SkDebugf(" %d,", array[i]);
98 }
99 SkDebugf("\n");
100}
101
103public:
105 if ((false)) dump_array(nullptr, 0);
106 if ((false)) rotr(0, 0);
107 }
108
109protected:
110 const char* onGetName() override {
111 return "fontefficiency";
112 }
113
114 void onDraw(int loops, SkCanvas* canvas) override {
115 static bool gDone;
116 if (gDone) {
117 return;
118 }
119 gDone = true;
120
121 for (int hashBits = 6; hashBits <= 12; hashBits += 1) {
122 int hashMask = ((1 << hashBits) - 1);
123 for (int limit = 32; limit <= 1024; limit <<= 1) {
124 for (size_t i = 0; i < std::size(gRec); ++i) {
125 int collisions = 0;
126 int glyphs = 0;
127 const uint16_t* array = gUniqueGlyphIDs;
128 while (*array != gUniqueGlyphIDs_Sentinel) {
129 int count = std::min(count_glyphs(array), limit);
130 collisions += count_collisions(array, count, gRec[i].fHasher, hashMask);
131 glyphs += count;
132 array += count + 1; // skip the sentinel
133 }
134 SkDebugf("hashBits [%d] limit [%d] collisions [%d / %d = %1.2g%%] using %s\n", hashBits, limit, collisions, glyphs,
135 collisions * 100.0 / glyphs, gRec[i].fName);
136 }
137 }
138 }
139 }
140
141private:
142 using INHERITED = Benchmark;
143};
144DEF_BENCH( return new FontCacheBench(); )
145
146// undefine this to run the efficiency test
147//DEF_BENCH( return new FontCacheEfficiency(); )
148
149///////////////////////////////////////////////////////////////////////////////
150
151class FontPathBench : public Benchmark {
152 SkFont fFont;
153 uint16_t fGlyphs[100];
155 const bool fOneAtATime;
156
157public:
158 FontPathBench(bool oneAtATime) : fOneAtATime(oneAtATime) {
159 fName.printf("font-path-%s", oneAtATime ? "loop" : "batch");
160 }
161
162protected:
163 const char* onGetName() override {
164 return fName.c_str();
165 }
166
167 bool isSuitableFor(Backend backend) override {
168 return backend == Backend::kNonRendering;
169 }
170
171 void onDelayedSetup() override {
172 fFont.setSize(32);
173 for (size_t i = 0; i < std::size(fGlyphs); ++i) {
174 fGlyphs[i] = i;
175 }
176 }
177
178 void onDraw(int loops, SkCanvas* canvas) override {
179 SkPath path;
180 for (int loop = 0; loop < loops; ++loop) {
181 if (fOneAtATime) {
182 for (size_t i = 0; i < std::size(fGlyphs); ++i) {
183 fFont.getPath(fGlyphs[i], &path);
184 }
185 } else {
186 fFont.getPaths(fGlyphs, std::size(fGlyphs),
187 [](const SkPath* src, const SkMatrix& mx, void* ctx) {
188 if (src) {
189 src->transform(mx, static_cast<SkPath*>(ctx));
190 }
191 }, &path);
192 }
193 }
194 }
195
196private:
197 using INHERITED = Benchmark;
198};
199DEF_BENCH( return new FontPathBench(true); )
200DEF_BENCH( return new FontPathBench(false); )
#define DEF_BENCH(code)
Definition Benchmark.h:20
const char * backend
HasherProc fHasher
static int count_glyphs(const uint16_t start[])
static int count_collisions(const uint16_t array[], int count, HasherProc proc, unsigned hashMask)
static void dump_array(const uint16_t array[], int count)
static uint32_t rotr(uint32_t value, unsigned bits)
uint32_t(* HasherProc)(uint32_t)
const char * fName
#define kMaxHashCount
static const struct @223 gRec[]
static uint32_t hasher0(uint32_t value)
#define gUniqueGlyphIDs_Sentinel
uint16_t glyphs[5]
int count
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
@ kGlyphID
uses two byte words to represent glyph indices
static void sk_bzero(void *buffer, size_t size)
Definition SkMalloc.h:105
#define INHERITED(method,...)
SI F table(const skcms_Curve *curve, F v)
const char * onGetName() override
void onDraw(int loops, SkCanvas *canvas) override
const char * onGetName() override
void onDraw(int loops, SkCanvas *canvas) override
FontPathBench(bool oneAtATime)
void onDraw(int loops, SkCanvas *canvas) override
const char * onGetName() override
bool isSuitableFor(Backend backend) override
void onDelayedSetup() override
bool getPath(SkGlyphID glyphID, SkPath *path) const
Definition SkFont.cpp:300
void setSize(SkScalar textSize)
Definition SkFont.cpp:129
void getPaths(const SkGlyphID glyphIDs[], int count, void(*glyphPathProc)(const SkPath *pathOrNull, const SkMatrix &mx, void *ctx), void *ctx) const
Definition SkFont.cpp:285
@ kAntiAlias
may have transparent pixels on glyph edges
uint8_t value
static const uint16_t gUniqueGlyphIDs[]
static uint32_t Mix(uint32_t hash)
Definition SkChecksum.h:30
SkFont DefaultFont()