blob: beae47a56df2819488e34347e2a7b7b72232d41a [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.com8af03712013-06-11 19:18:44 +00009#include "SkCanvas.h"
mtklein67a32712014-07-10 06:03:46 -070010#include "SkChecksum.h"
Mike Reed07f93f22018-11-14 08:52:54 -080011#include "SkFont.h"
reed@google.com8af03712013-06-11 19:18:44 +000012#include "SkPaint.h"
Mike Reed07f93f22018-11-14 08:52:54 -080013#include "SkPath.h"
reed@google.com8af03712013-06-11 19:18:44 +000014#include "SkString.h"
15#include "SkTemplates.h"
16
reed@google.com664621a2013-06-11 19:24:08 +000017#include "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 {
reed@google.com8af03712013-06-11 19:18:44 +000039 SkPaint paint;
40 this->setupPaint(&paint);
41 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000042
reed@google.com2fef6d22013-06-11 20:25:53 +000043 const uint16_t* array = gUniqueGlyphIDs;
reed@google.comc2652302013-06-12 15:50:26 +000044 while (*array != gUniqueGlyphIDs_Sentinel) {
robertphillips@google.com8b169312013-10-15 17:47:36 +000045 int count = count_glyphs(array);
commit-bot@chromium.org33614712013-12-03 18:17:16 +000046 for (int i = 0; i < loops; ++i) {
reed@google.comc2652302013-06-12 15:50:26 +000047 paint.measureText(array, count * sizeof(uint16_t));
reed@google.com2fef6d22013-06-11 20:25:53 +000048 }
reed@google.comc2652302013-06-12 15:50:26 +000049 array += count + 1; // skip the sentinel
reed@google.com8af03712013-06-11 19:18:44 +000050 }
51 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000052
reed@google.comc2652302013-06-12 15:50:26 +000053private:
tfarinaf168b862014-06-19 12:32:29 -070054 typedef Benchmark INHERITED;
reed@google.comc2652302013-06-12 15:50:26 +000055};
reed@google.com8af03712013-06-11 19:18:44 +000056
reed@google.comc2652302013-06-12 15:50:26 +000057///////////////////////////////////////////////////////////////////////////////
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
reed@google.comc2652302013-06-12 15:50:26 +000070static const struct {
71 const char* fName;
72 HasherProc fHasher;
73} gRec[] = {
74 { "hasher0", hasher0 },
mtklein67a32712014-07-10 06:03:46 -070075 { "hasher2", SkChecksum::Mix },
reed@google.comc2652302013-06-12 15:50:26 +000076};
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));
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +000085
reed@google.comc2652302013-06-12 15:50:26 +000086 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
tfarinaf168b862014-06-19 12:32:29 -0700102class FontCacheEfficiency : public Benchmark {
reed@google.comc2652302013-06-12 15:50:26 +0000103public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000104 FontCacheEfficiency() {
halcanary96fcdcc2015-08-27 07:41:13 -0700105 if (false) dump_array(nullptr, 0);
reed@google.comc2652302013-06-12 15:50:26 +0000106 if (false) rotr(0, 0);
107 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000108
reed@google.comc2652302013-06-12 15:50:26 +0000109protected:
mtklein36352bf2015-03-25 18:17:31 -0700110 const char* onGetName() override {
reed@google.comc2652302013-06-12 15:50:26 +0000111 return "fontefficiency";
112 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000113
mtkleina1ebeb22015-10-01 09:43:39 -0700114 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.comc2652302013-06-12 15:50:26 +0000115 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 < SK_ARRAY_COUNT(gRec); ++i) {
125 int collisions = 0;
126 int glyphs = 0;
127 const uint16_t* array = gUniqueGlyphIDs;
128 while (*array != gUniqueGlyphIDs_Sentinel) {
129 int count = SkMin32(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 }
skia.committer@gmail.coma707fd52013-06-13 07:00:51 +0000140
reed@google.com8af03712013-06-11 19:18:44 +0000141private:
tfarinaf168b862014-06-19 12:32:29 -0700142 typedef Benchmark INHERITED;
reed@google.com8af03712013-06-11 19:18:44 +0000143};
mtklein@google.com410e6e82013-09-13 19:52:27 +0000144DEF_BENCH( return new FontCacheBench(); )
reed@google.comc2652302013-06-12 15:50:26 +0000145
146// undefine this to run the efficiency test
mtklein@google.com410e6e82013-09-13 19:52:27 +0000147//DEF_BENCH( return new FontCacheEfficiency(); )
Mike Reed07f93f22018-11-14 08:52:54 -0800148
149///////////////////////////////////////////////////////////////////////////////
150
151class FontPathBench : public Benchmark {
152 SkFont fFont;
153 uint16_t fGlyphs[100];
154 SkString fName;
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 == kNonRendering_Backend;
169 }
170
171 void onDelayedSetup() override {
172 fFont.setSize(32);
173 for (size_t i = 0; i < SK_ARRAY_COUNT(fGlyphs); ++i) {
174 fGlyphs[i] = i;
175 }
176 }
177
178 void onDraw(int loops, SkCanvas* canvas) override {
179 SkPath path;
180 for (int i = 0; i < loops; ++i) {
181 if (fOneAtATime) {
182 for (size_t i = 0; i < SK_ARRAY_COUNT(fGlyphs); ++i) {
183 fFont.getPath(fGlyphs[i], &path);
184 }
185 } else {
186 fFont.getPaths(fGlyphs, SK_ARRAY_COUNT(fGlyphs),
Mike Reed28715092018-11-24 22:55:54 -0500187 [](const SkPath* src, const SkMatrix& mx, void* ctx) {
Mike Reed07f93f22018-11-14 08:52:54 -0800188 if (src) {
Mike Reed28715092018-11-24 22:55:54 -0500189 src->transform(mx, static_cast<SkPath*>(ctx));
Mike Reed07f93f22018-11-14 08:52:54 -0800190 }
191 }, &path);
192 }
193 }
194 }
195
196private:
197 typedef Benchmark INHERITED;
198};
199DEF_BENCH( return new FontPathBench(true); )
200DEF_BENCH( return new FontPathBench(false); )