blob: 5d8966e0f7895f20e051429c50304fa3f9fedf95 [file] [log] [blame]
reed@google.com8af03712013-06-11 19:18:44 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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/SkChecksum.h"
15#include "include/private/SkTemplates.h"
reed@google.com8af03712013-06-11 19:18:44 +000016
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "bench/gUniqueGlyphIDs.h"
Hal Canary8a001442018-09-19 11:31:27 -040018
reed@google.comc2652302013-06-12 15:50:26 +000019#define gUniqueGlyphIDs_Sentinel 0xFFFF
20
21static int count_glyphs(const uint16_t start[]) {
22 const uint16_t* curr = start;
23 while (*curr != gUniqueGlyphIDs_Sentinel) {
24 curr += 1;
25 }
robertphillips@google.com8b169312013-10-15 17:47:36 +000026 return static_cast<int>(curr - start);
reed@google.comc2652302013-06-12 15:50:26 +000027}
reed@google.com8af03712013-06-11 19:18:44 +000028
tfarinaf168b862014-06-19 12:32:29 -070029class FontCacheBench : public Benchmark {
reed@google.comc2652302013-06-12 15:50:26 +000030public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000031 FontCacheBench() {}
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000032
reed@google.com8af03712013-06-11 19:18:44 +000033protected:
mtklein36352bf2015-03-25 18:17:31 -070034 const char* onGetName() override {
reed@google.com8af03712013-06-11 19:18:44 +000035 return "fontcache";
36 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000037
mtkleina1ebeb22015-10-01 09:43:39 -070038 void onDraw(int loops, SkCanvas* canvas) override {
Mike Reed94cca602018-12-02 16:04:27 -050039 SkFont font;
40 font.setEdging(SkFont::Edging::kAntiAlias);
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000041
reed@google.com2fef6d22013-06-11 20:25:53 +000042 const uint16_t* array = gUniqueGlyphIDs;
reed@google.comc2652302013-06-12 15:50:26 +000043 while (*array != gUniqueGlyphIDs_Sentinel) {
robertphillips@google.com8b169312013-10-15 17:47:36 +000044 int count = count_glyphs(array);
commit-bot@chromium.org33614712013-12-03 18:17:16 +000045 for (int i = 0; i < loops; ++i) {
Ben Wagner51e15a62019-05-07 15:38:46 -040046 (void)font.measureText(array, count * sizeof(uint16_t), SkTextEncoding::kGlyphID);
reed@google.com2fef6d22013-06-11 20:25:53 +000047 }
reed@google.comc2652302013-06-12 15:50:26 +000048 array += count + 1; // skip the sentinel
reed@google.com8af03712013-06-11 19:18:44 +000049 }
50 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000051
reed@google.comc2652302013-06-12 15:50:26 +000052private:
John Stiles7571f9e2020-09-02 22:42:33 -040053 using INHERITED = Benchmark;
reed@google.comc2652302013-06-12 15:50:26 +000054};
reed@google.com8af03712013-06-11 19:18:44 +000055
reed@google.comc2652302013-06-12 15:50:26 +000056///////////////////////////////////////////////////////////////////////////////
57
58static uint32_t rotr(uint32_t value, unsigned bits) {
59 return (value >> bits) | (value << (32 - bits));
60}
61
62typedef uint32_t (*HasherProc)(uint32_t);
63
64static uint32_t hasher0(uint32_t value) {
65 value = value ^ (value >> 16);
66 return value ^ (value >> 8);
67}
68
reed@google.comc2652302013-06-12 15:50:26 +000069static const struct {
70 const char* fName;
71 HasherProc fHasher;
72} gRec[] = {
73 { "hasher0", hasher0 },
mtklein67a32712014-07-10 06:03:46 -070074 { "hasher2", SkChecksum::Mix },
reed@google.comc2652302013-06-12 15:50:26 +000075};
76
77#define kMaxHashBits 12
78#define kMaxHashCount (1 << kMaxHashBits)
79
80static int count_collisions(const uint16_t array[], int count, HasherProc proc,
81 unsigned hashMask) {
82 char table[kMaxHashCount];
83 sk_bzero(table, sizeof(table));
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000084
reed@google.comc2652302013-06-12 15:50:26 +000085 int collisions = 0;
86 for (int i = 0; i < count; ++i) {
87 int index = proc(array[i]) & hashMask;
88 collisions += table[index];
89 table[index] = 1;
90 }
91 return collisions;
92}
93
94static void dump_array(const uint16_t array[], int count) {
95 for (int i = 0; i < count; ++i) {
96 SkDebugf(" %d,", array[i]);
97 }
98 SkDebugf("\n");
99}
100
tfarinaf168b862014-06-19 12:32:29 -0700101class FontCacheEfficiency : public Benchmark {
reed@google.comc2652302013-06-12 15:50:26 +0000102public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000103 FontCacheEfficiency() {
halcanary96fcdcc2015-08-27 07:41:13 -0700104 if (false) dump_array(nullptr, 0);
reed@google.comc2652302013-06-12 15:50:26 +0000105 if (false) rotr(0, 0);
106 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000107
reed@google.comc2652302013-06-12 15:50:26 +0000108protected:
mtklein36352bf2015-03-25 18:17:31 -0700109 const char* onGetName() override {
reed@google.comc2652302013-06-12 15:50:26 +0000110 return "fontefficiency";
111 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000112
mtkleina1ebeb22015-10-01 09:43:39 -0700113 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.comc2652302013-06-12 15:50:26 +0000114 static bool gDone;
115 if (gDone) {
116 return;
117 }
118 gDone = true;
119
120 for (int hashBits = 6; hashBits <= 12; hashBits += 1) {
121 int hashMask = ((1 << hashBits) - 1);
122 for (int limit = 32; limit <= 1024; limit <<= 1) {
123 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
124 int collisions = 0;
125 int glyphs = 0;
126 const uint16_t* array = gUniqueGlyphIDs;
127 while (*array != gUniqueGlyphIDs_Sentinel) {
Brian Osman7f364052020-02-06 11:25:43 -0500128 int count = std::min(count_glyphs(array), limit);
reed@google.comc2652302013-06-12 15:50:26 +0000129 collisions += count_collisions(array, count, gRec[i].fHasher, hashMask);
130 glyphs += count;
131 array += count + 1; // skip the sentinel
132 }
133 SkDebugf("hashBits [%d] limit [%d] collisions [%d / %d = %1.2g%%] using %s\n", hashBits, limit, collisions, glyphs,
134 collisions * 100.0 / glyphs, gRec[i].fName);
135 }
136 }
137 }
138 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000139
reed@google.com8af03712013-06-11 19:18:44 +0000140private:
John Stiles7571f9e2020-09-02 22:42:33 -0400141 using INHERITED = Benchmark;
reed@google.com8af03712013-06-11 19:18:44 +0000142};
mtklein@google.com410e6e82013-09-13 19:52:27 +0000143DEF_BENCH( return new FontCacheBench(); )
reed@google.comc2652302013-06-12 15:50:26 +0000144
145// undefine this to run the efficiency test
mtklein@google.com410e6e82013-09-13 19:52:27 +0000146//DEF_BENCH( return new FontCacheEfficiency(); )
Mike Reed07f93f22018-11-14 08:52:54 -0800147
148///////////////////////////////////////////////////////////////////////////////
149
150class FontPathBench : public Benchmark {
151 SkFont fFont;
152 uint16_t fGlyphs[100];
153 SkString fName;
154 const bool fOneAtATime;
155
156public:
157 FontPathBench(bool oneAtATime) : fOneAtATime(oneAtATime) {
158 fName.printf("font-path-%s", oneAtATime ? "loop" : "batch");
159 }
160
161protected:
162 const char* onGetName() override {
163 return fName.c_str();
164 }
165
166 bool isSuitableFor(Backend backend) override {
167 return backend == kNonRendering_Backend;
168 }
169
170 void onDelayedSetup() override {
171 fFont.setSize(32);
172 for (size_t i = 0; i < SK_ARRAY_COUNT(fGlyphs); ++i) {
173 fGlyphs[i] = i;
174 }
175 }
176
177 void onDraw(int loops, SkCanvas* canvas) override {
178 SkPath path;
John Stiles73339ad2021-08-12 22:27:39 -0400179 for (int loop = 0; loop < loops; ++loop) {
Mike Reed07f93f22018-11-14 08:52:54 -0800180 if (fOneAtATime) {
181 for (size_t i = 0; i < SK_ARRAY_COUNT(fGlyphs); ++i) {
182 fFont.getPath(fGlyphs[i], &path);
183 }
184 } else {
185 fFont.getPaths(fGlyphs, SK_ARRAY_COUNT(fGlyphs),
Mike Reed28715092018-11-24 22:55:54 -0500186 [](const SkPath* src, const SkMatrix& mx, void* ctx) {
Mike Reed07f93f22018-11-14 08:52:54 -0800187 if (src) {
Mike Reed28715092018-11-24 22:55:54 -0500188 src->transform(mx, static_cast<SkPath*>(ctx));
Mike Reed07f93f22018-11-14 08:52:54 -0800189 }
190 }, &path);
191 }
192 }
193 }
194
195private:
John Stiles7571f9e2020-09-02 22:42:33 -0400196 using INHERITED = Benchmark;
Mike Reed07f93f22018-11-14 08:52:54 -0800197};
198DEF_BENCH( return new FontPathBench(true); )
199DEF_BENCH( return new FontPathBench(false); )