reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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/SkChecksum.h" |
| 15 | #include "include/private/SkTemplates.h" |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 16 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "bench/gUniqueGlyphIDs.h" |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 18 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 19 | #define gUniqueGlyphIDs_Sentinel 0xFFFF |
| 20 | |
| 21 | static int count_glyphs(const uint16_t start[]) { |
| 22 | const uint16_t* curr = start; |
| 23 | while (*curr != gUniqueGlyphIDs_Sentinel) { |
| 24 | curr += 1; |
| 25 | } |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 26 | return static_cast<int>(curr - start); |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 27 | } |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 28 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 29 | class FontCacheBench : public Benchmark { |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 30 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 31 | FontCacheBench() {} |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 32 | |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 33 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | const char* onGetName() override { |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 35 | return "fontcache"; |
| 36 | } |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 37 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 38 | void onDraw(int loops, SkCanvas* canvas) override { |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 39 | SkFont font; |
| 40 | font.setEdging(SkFont::Edging::kAntiAlias); |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 41 | |
reed@google.com | 2fef6d2 | 2013-06-11 20:25:53 +0000 | [diff] [blame] | 42 | const uint16_t* array = gUniqueGlyphIDs; |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 43 | while (*array != gUniqueGlyphIDs_Sentinel) { |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 44 | int count = count_glyphs(array); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 45 | for (int i = 0; i < loops; ++i) { |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 46 | (void)font.measureText(array, count * sizeof(uint16_t), SkTextEncoding::kGlyphID); |
reed@google.com | 2fef6d2 | 2013-06-11 20:25:53 +0000 | [diff] [blame] | 47 | } |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 48 | array += count + 1; // skip the sentinel |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 51 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 52 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 53 | typedef Benchmark INHERITED; |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 54 | }; |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 55 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
| 58 | static uint32_t rotr(uint32_t value, unsigned bits) { |
| 59 | return (value >> bits) | (value << (32 - bits)); |
| 60 | } |
| 61 | |
| 62 | typedef uint32_t (*HasherProc)(uint32_t); |
| 63 | |
| 64 | static uint32_t hasher0(uint32_t value) { |
| 65 | value = value ^ (value >> 16); |
| 66 | return value ^ (value >> 8); |
| 67 | } |
| 68 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 69 | static const struct { |
| 70 | const char* fName; |
| 71 | HasherProc fHasher; |
| 72 | } gRec[] = { |
| 73 | { "hasher0", hasher0 }, |
mtklein | 67a3271 | 2014-07-10 06:03:46 -0700 | [diff] [blame] | 74 | { "hasher2", SkChecksum::Mix }, |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | #define kMaxHashBits 12 |
| 78 | #define kMaxHashCount (1 << kMaxHashBits) |
| 79 | |
| 80 | static 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.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 84 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 85 | 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 | |
| 94 | static 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 101 | class FontCacheEfficiency : public Benchmark { |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 102 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 103 | FontCacheEfficiency() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 104 | if (false) dump_array(nullptr, 0); |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 105 | if (false) rotr(0, 0); |
| 106 | } |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 107 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 108 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 109 | const char* onGetName() override { |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 110 | return "fontefficiency"; |
| 111 | } |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 112 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 113 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 114 | 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) { |
| 128 | int count = SkMin32(count_glyphs(array), limit); |
| 129 | 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.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 139 | |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 140 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 141 | typedef Benchmark INHERITED; |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 142 | }; |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 143 | DEF_BENCH( return new FontCacheBench(); ) |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 144 | |
| 145 | // undefine this to run the efficiency test |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 146 | //DEF_BENCH( return new FontCacheEfficiency(); ) |
Mike Reed | 07f93f2 | 2018-11-14 08:52:54 -0800 | [diff] [blame] | 147 | |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
| 149 | |
| 150 | class FontPathBench : public Benchmark { |
| 151 | SkFont fFont; |
| 152 | uint16_t fGlyphs[100]; |
| 153 | SkString fName; |
| 154 | const bool fOneAtATime; |
| 155 | |
| 156 | public: |
| 157 | FontPathBench(bool oneAtATime) : fOneAtATime(oneAtATime) { |
| 158 | fName.printf("font-path-%s", oneAtATime ? "loop" : "batch"); |
| 159 | } |
| 160 | |
| 161 | protected: |
| 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; |
| 179 | for (int i = 0; i < loops; ++i) { |
| 180 | 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 Reed | 2871509 | 2018-11-24 22:55:54 -0500 | [diff] [blame] | 186 | [](const SkPath* src, const SkMatrix& mx, void* ctx) { |
Mike Reed | 07f93f2 | 2018-11-14 08:52:54 -0800 | [diff] [blame] | 187 | if (src) { |
Mike Reed | 2871509 | 2018-11-24 22:55:54 -0500 | [diff] [blame] | 188 | src->transform(mx, static_cast<SkPath*>(ctx)); |
Mike Reed | 07f93f2 | 2018-11-14 08:52:54 -0800 | [diff] [blame] | 189 | } |
| 190 | }, &path); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | typedef Benchmark INHERITED; |
| 197 | }; |
| 198 | DEF_BENCH( return new FontPathBench(true); ) |
| 199 | DEF_BENCH( return new FontPathBench(false); ) |