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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
mtklein | 67a3271 | 2014-07-10 06:03:46 -0700 | [diff] [blame] | 10 | #include "SkChecksum.h" |
Mike Reed | 07f93f2 | 2018-11-14 08:52:54 -0800 | [diff] [blame^] | 11 | #include "SkFont.h" |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 12 | #include "SkPaint.h" |
Mike Reed | 07f93f2 | 2018-11-14 08:52:54 -0800 | [diff] [blame^] | 13 | #include "SkPath.h" |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 14 | #include "SkString.h" |
| 15 | #include "SkTemplates.h" |
| 16 | |
reed@google.com | 664621a | 2013-06-11 19:24:08 +0000 | [diff] [blame] | 17 | #include "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 { |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 39 | SkPaint paint; |
| 40 | this->setupPaint(&paint); |
| 41 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 42 | |
reed@google.com | 2fef6d2 | 2013-06-11 20:25:53 +0000 | [diff] [blame] | 43 | const uint16_t* array = gUniqueGlyphIDs; |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 44 | while (*array != gUniqueGlyphIDs_Sentinel) { |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 45 | int count = count_glyphs(array); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 46 | for (int i = 0; i < loops; ++i) { |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 47 | paint.measureText(array, count * sizeof(uint16_t)); |
reed@google.com | 2fef6d2 | 2013-06-11 20:25:53 +0000 | [diff] [blame] | 48 | } |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 49 | array += count + 1; // skip the sentinel |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 52 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 53 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 54 | typedef Benchmark INHERITED; |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 55 | }; |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 56 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | |
| 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 | |
| 65 | static uint32_t hasher0(uint32_t value) { |
| 66 | value = value ^ (value >> 16); |
| 67 | return value ^ (value >> 8); |
| 68 | } |
| 69 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 70 | static const struct { |
| 71 | const char* fName; |
| 72 | HasherProc fHasher; |
| 73 | } gRec[] = { |
| 74 | { "hasher0", hasher0 }, |
mtklein | 67a3271 | 2014-07-10 06:03:46 -0700 | [diff] [blame] | 75 | { "hasher2", SkChecksum::Mix }, |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | #define kMaxHashBits 12 |
| 79 | #define kMaxHashCount (1 << kMaxHashBits) |
| 80 | |
| 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)); |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 85 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 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 | |
| 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 102 | class FontCacheEfficiency : public Benchmark { |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 103 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 104 | FontCacheEfficiency() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 105 | if (false) dump_array(nullptr, 0); |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 106 | if (false) rotr(0, 0); |
| 107 | } |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 108 | |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 109 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 110 | const char* onGetName() override { |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 111 | return "fontefficiency"; |
| 112 | } |
skia.committer@gmail.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 113 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 114 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 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 < 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.com | a707fd5 | 2013-06-13 07:00:51 +0000 | [diff] [blame] | 140 | |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 141 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 142 | typedef Benchmark INHERITED; |
reed@google.com | 8af0371 | 2013-06-11 19:18:44 +0000 | [diff] [blame] | 143 | }; |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 144 | DEF_BENCH( return new FontCacheBench(); ) |
reed@google.com | c265230 | 2013-06-12 15:50:26 +0000 | [diff] [blame] | 145 | |
| 146 | // undefine this to run the efficiency test |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 147 | //DEF_BENCH( return new FontCacheEfficiency(); ) |
Mike Reed | 07f93f2 | 2018-11-14 08:52:54 -0800 | [diff] [blame^] | 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: |
| 158 | FontPathBench(bool oneAtATime) : fOneAtATime(oneAtATime) { |
| 159 | fName.printf("font-path-%s", oneAtATime ? "loop" : "batch"); |
| 160 | } |
| 161 | |
| 162 | protected: |
| 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), |
| 187 | [](uint16_t, const SkPath* src, void* ctx) { |
| 188 | if (src) { |
| 189 | *static_cast<SkPath*>(ctx) = *src; |
| 190 | } |
| 191 | }, &path); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | private: |
| 197 | typedef Benchmark INHERITED; |
| 198 | }; |
| 199 | DEF_BENCH( return new FontPathBench(true); ) |
| 200 | DEF_BENCH( return new FontPathBench(false); ) |