xref: /aosp_15_r20/external/skia/bench/FontCacheBench.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkFont.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkString.h"
14 #include "include/private/base/SkTemplates.h"
15 #include "src/core/SkChecksum.h"
16 #include "tools/fonts/FontToolUtils.h"
17 
18 #include "bench/gUniqueGlyphIDs.h"
19 
20 #define gUniqueGlyphIDs_Sentinel    0xFFFF
21 
count_glyphs(const uint16_t start[])22 static 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 
30 class FontCacheBench : public Benchmark {
31 public:
FontCacheBench()32     FontCacheBench()  {}
33 
34 protected:
onGetName()35     const char* onGetName() override {
36         return "fontcache";
37     }
38 
onDraw(int loops,SkCanvas * canvas)39     void onDraw(int loops, SkCanvas* canvas) override {
40         SkFont font = ToolUtils::DefaultFont();
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 
53 private:
54     using INHERITED = Benchmark;
55 };
56 
57 ///////////////////////////////////////////////////////////////////////////////
58 
rotr(uint32_t value,unsigned bits)59 static uint32_t rotr(uint32_t value, unsigned bits) {
60     return (value >> bits) | (value << (32 - bits));
61 }
62 
63 typedef uint32_t (*HasherProc)(uint32_t);
64 
hasher0(uint32_t value)65 static uint32_t hasher0(uint32_t value) {
66     value = value ^ (value >> 16);
67     return value ^ (value >> 8);
68 }
69 
70 static const struct {
71     const char* fName;
72     HasherProc  fHasher;
73 } gRec[] = {
74     { "hasher0",  hasher0 },
75     { "hasher2",  SkChecksum::Mix },
76 };
77 
78 #define kMaxHashBits   12
79 #define kMaxHashCount  (1 << kMaxHashBits)
80 
count_collisions(const uint16_t array[],int count,HasherProc proc,unsigned hashMask)81 static 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 
dump_array(const uint16_t array[],int count)95 static 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 
102 class FontCacheEfficiency : public Benchmark {
103 public:
FontCacheEfficiency()104     FontCacheEfficiency()  {
105         if ((false)) dump_array(nullptr, 0);
106         if ((false)) rotr(0, 0);
107     }
108 
109 protected:
onGetName()110     const char* onGetName() override {
111         return "fontefficiency";
112     }
113 
onDraw(int loops,SkCanvas * canvas)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 
141 private:
142     using INHERITED = Benchmark;
143 };
144 DEF_BENCH( return new FontCacheBench(); )
145 
146 // undefine this to run the efficiency test
147 //DEF_BENCH( return new FontCacheEfficiency(); )
148 
149 ///////////////////////////////////////////////////////////////////////////////
150 
151 class FontPathBench : public Benchmark {
152     SkFont fFont;
153     uint16_t fGlyphs[100];
154     SkString fName;
155     const bool fOneAtATime;
156 
157 public:
FontPathBench(bool oneAtATime)158     FontPathBench(bool oneAtATime) : fOneAtATime(oneAtATime) {
159         fName.printf("font-path-%s", oneAtATime ? "loop" : "batch");
160     }
161 
162 protected:
onGetName()163     const char* onGetName() override {
164         return fName.c_str();
165     }
166 
isSuitableFor(Backend backend)167     bool isSuitableFor(Backend backend) override {
168         return backend == Backend::kNonRendering;
169     }
170 
onDelayedSetup()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 
onDraw(int loops,SkCanvas * canvas)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 
196 private:
197     using INHERITED = Benchmark;
198 };
199 DEF_BENCH( return new FontPathBench(true); )
200 DEF_BENCH( return new FontPathBench(false); )
201