herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "src/core/SkStrike.h" |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "bench/Benchmark.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkGraphics.h" |
| 13 | #include "include/core/SkTypeface.h" |
Hal Canary | e107faa | 2019-10-23 12:52:33 -0400 | [diff] [blame] | 14 | #include "src/core/SkRemoteGlyphCache.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/core/SkStrikeCache.h" |
Herb Derby | baf6478 | 2019-04-17 18:01:04 -0400 | [diff] [blame] | 16 | #include "src/core/SkStrikeSpec.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkTaskGroup.h" |
Hal Canary | e107faa | 2019-10-23 12:52:33 -0400 | [diff] [blame] | 18 | #include "src/core/SkTextBlobTrace.h" |
| 19 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "tools/ToolUtils.h" |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 21 | |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 22 | static void do_font_stuff(SkFont* font) { |
| 23 | SkPaint defaultPaint; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 24 | for (SkScalar i = 8; i < 64; i++) { |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 25 | font->setSize(i); |
Herb Derby | 36a54c1 | 2019-06-06 10:50:56 -0400 | [diff] [blame] | 26 | auto strikeSpec = SkStrikeSpec::MakeMask( |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 27 | *font, defaultPaint, SkSurfaceProps(0, kUnknown_SkPixelGeometry), |
Herb Derby | c341500 | 2018-11-08 16:40:26 -0500 | [diff] [blame] | 28 | SkScalerContextFlags::kNone, SkMatrix::I()); |
Herb Derby | 55a85fc | 2019-07-03 14:46:05 -0400 | [diff] [blame] | 29 | SkPackedGlyphID glyphs['z']; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 30 | for (int c = ' '; c < 'z'; c++) { |
Herb Derby | 55a85fc | 2019-07-03 14:46:05 -0400 | [diff] [blame] | 31 | glyphs[c] = SkPackedGlyphID{font->unicharToGlyph(c)}; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 32 | } |
Herb Derby | 55a85fc | 2019-07-03 14:46:05 -0400 | [diff] [blame] | 33 | constexpr size_t glyphCount = 'z' - ' '; |
| 34 | SkSpan<const SkPackedGlyphID> glyphIDs{&glyphs[SkTo<int>(' ')], glyphCount}; |
| 35 | SkBulkGlyphMetricsAndImages images{strikeSpec}; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 36 | for (int lookups = 0; lookups < 10; lookups++) { |
Herb Derby | 55a85fc | 2019-07-03 14:46:05 -0400 | [diff] [blame] | 37 | (void)images.glyphs(glyphIDs); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 38 | } |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | class SkGlyphCacheBasic : public Benchmark { |
| 43 | public: |
| 44 | explicit SkGlyphCacheBasic(size_t cacheSize) : fCacheSize(cacheSize) { } |
| 45 | |
| 46 | protected: |
| 47 | const char* onGetName() override { |
| 48 | fName.printf("SkGlyphCacheBasic%dK", (int)(fCacheSize >> 10)); |
| 49 | return fName.c_str(); |
| 50 | } |
| 51 | |
| 52 | bool isSuitableFor(Backend backend) override { |
| 53 | return backend == kNonRendering_Backend; |
| 54 | } |
| 55 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 56 | void onDraw(int loops, SkCanvas*) override { |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 57 | size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit(); |
| 58 | SkGraphics::SetFontCacheLimit(fCacheSize); |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 59 | SkFont font; |
| 60 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 61 | font.setSubpixel(true); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 62 | font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic())); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 63 | |
| 64 | for (int work = 0; work < loops; work++) { |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 65 | do_font_stuff(&font); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 66 | } |
| 67 | SkGraphics::SetFontCacheLimit(oldCacheLimitSize); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | typedef Benchmark INHERITED; |
| 72 | const size_t fCacheSize; |
| 73 | SkString fName; |
| 74 | }; |
| 75 | |
| 76 | class SkGlyphCacheStressTest : public Benchmark { |
| 77 | public: |
| 78 | explicit SkGlyphCacheStressTest(int cacheSize) : fCacheSize(cacheSize) { } |
| 79 | |
| 80 | protected: |
| 81 | const char* onGetName() override { |
| 82 | fName.printf("SkGlyphCacheStressTest%dK", (int)(fCacheSize >> 10)); |
| 83 | return fName.c_str(); |
| 84 | } |
| 85 | |
| 86 | bool isSuitableFor(Backend backend) override { |
| 87 | return backend == kNonRendering_Backend; |
| 88 | } |
| 89 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 90 | void onDraw(int loops, SkCanvas*) override { |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 91 | size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit(); |
| 92 | SkGraphics::SetFontCacheLimit(fCacheSize); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 93 | sk_sp<SkTypeface> typefaces[] = { |
| 94 | ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic()), |
| 95 | ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Italic())}; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 96 | |
| 97 | for (int work = 0; work < loops; work++) { |
mtklein | 279c786 | 2016-01-04 19:13:19 -0800 | [diff] [blame] | 98 | SkTaskGroup().batch(16, [&](int threadIndex) { |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 99 | SkFont font; |
| 100 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 101 | font.setSubpixel(true); |
| 102 | font.setTypeface(typefaces[threadIndex % 2]); |
| 103 | do_font_stuff(&font); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 104 | }); |
| 105 | } |
| 106 | SkGraphics::SetFontCacheLimit(oldCacheLimitSize); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private: |
| 110 | typedef Benchmark INHERITED; |
| 111 | const size_t fCacheSize; |
| 112 | SkString fName; |
| 113 | }; |
| 114 | |
| 115 | DEF_BENCH( return new SkGlyphCacheBasic(256 * 1024); ) |
| 116 | DEF_BENCH( return new SkGlyphCacheBasic(32 * 1024 * 1024); ) |
| 117 | DEF_BENCH( return new SkGlyphCacheStressTest(256 * 1024); ) |
| 118 | DEF_BENCH( return new SkGlyphCacheStressTest(32 * 1024 * 1024); ) |
Hal Canary | e107faa | 2019-10-23 12:52:33 -0400 | [diff] [blame] | 119 | |
| 120 | namespace { |
| 121 | class DiscardableManager : public SkStrikeServer::DiscardableHandleManager, |
| 122 | public SkStrikeClient::DiscardableHandleManager { |
| 123 | public: |
| 124 | DiscardableManager() { sk_bzero(&fCacheMissCount, sizeof(fCacheMissCount)); } |
| 125 | ~DiscardableManager() override = default; |
| 126 | |
| 127 | // Server implementation. |
| 128 | SkDiscardableHandleId createHandle() override { |
| 129 | SkAutoMutexExclusive l(fMutex); |
| 130 | |
| 131 | // Handles starts as locked. |
| 132 | fLockedHandles.add(++fNextHandleId); |
| 133 | return fNextHandleId; |
| 134 | } |
| 135 | bool lockHandle(SkDiscardableHandleId id) override { |
| 136 | SkAutoMutexExclusive l(fMutex); |
| 137 | |
| 138 | if (id <= fLastDeletedHandleId) return false; |
| 139 | fLockedHandles.add(id); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | // Client implementation. |
| 144 | bool deleteHandle(SkDiscardableHandleId id) override { |
| 145 | SkAutoMutexExclusive l(fMutex); |
| 146 | |
| 147 | return id <= fLastDeletedHandleId; |
| 148 | } |
| 149 | |
| 150 | void notifyCacheMiss(SkStrikeClient::CacheMissType type) override { |
| 151 | SkAutoMutexExclusive l(fMutex); |
| 152 | |
| 153 | fCacheMissCount[type]++; |
| 154 | } |
| 155 | bool isHandleDeleted(SkDiscardableHandleId id) override { |
| 156 | SkAutoMutexExclusive l(fMutex); |
| 157 | |
| 158 | return id <= fLastDeletedHandleId; |
| 159 | } |
| 160 | |
| 161 | void unlockAll() { |
| 162 | SkAutoMutexExclusive l(fMutex); |
| 163 | |
| 164 | fLockedHandles.reset(); |
| 165 | } |
| 166 | void unlockAndDeleteAll() { |
| 167 | SkAutoMutexExclusive l(fMutex); |
| 168 | |
| 169 | fLockedHandles.reset(); |
| 170 | fLastDeletedHandleId = fNextHandleId; |
| 171 | } |
| 172 | const SkTHashSet<SkDiscardableHandleId>& lockedHandles() const { |
| 173 | SkAutoMutexExclusive l(fMutex); |
| 174 | |
| 175 | return fLockedHandles; |
| 176 | } |
| 177 | SkDiscardableHandleId handleCount() { |
| 178 | SkAutoMutexExclusive l(fMutex); |
| 179 | |
| 180 | return fNextHandleId; |
| 181 | } |
| 182 | int cacheMissCount(uint32_t type) { |
| 183 | SkAutoMutexExclusive l(fMutex); |
| 184 | |
| 185 | return fCacheMissCount[type]; |
| 186 | } |
| 187 | bool hasCacheMiss() const { |
| 188 | SkAutoMutexExclusive l(fMutex); |
| 189 | |
| 190 | for (uint32_t i = 0; i <= SkStrikeClient::CacheMissType::kLast; ++i) { |
| 191 | if (fCacheMissCount[i] > 0) return true; |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | void resetCacheMissCounts() { |
| 196 | SkAutoMutexExclusive l(fMutex); |
| 197 | sk_bzero(&fCacheMissCount, sizeof(fCacheMissCount)); |
| 198 | } |
| 199 | |
| 200 | private: |
| 201 | // The tests below run in parallel on multiple threads and use the same |
| 202 | // process global SkStrikeCache. So the implementation needs to be |
| 203 | // thread-safe. |
| 204 | mutable SkMutex fMutex; |
| 205 | |
| 206 | SkDiscardableHandleId fNextHandleId = 0u; |
| 207 | SkDiscardableHandleId fLastDeletedHandleId = 0u; |
| 208 | SkTHashSet<SkDiscardableHandleId> fLockedHandles; |
| 209 | int fCacheMissCount[SkStrikeClient::CacheMissType::kLast + 1u]; |
| 210 | }; |
| 211 | |
Hal Canary | 5dfefa2 | 2019-10-24 13:52:17 -0400 | [diff] [blame] | 212 | class DiffCanvasBench : public Benchmark { |
| 213 | SkString fBenchName; |
| 214 | std::function<std::unique_ptr<SkStreamAsset>()> fDataProvider; |
Hal Canary | e107faa | 2019-10-23 12:52:33 -0400 | [diff] [blame] | 215 | std::vector<SkTextBlobTrace::Record> fTrace; |
| 216 | sk_sp<DiscardableManager> fDiscardableManager; |
| 217 | SkTLazy<SkStrikeServer> fServer; |
| 218 | |
| 219 | const char* onGetName() override { return fBenchName.c_str(); } |
| 220 | |
| 221 | bool isSuitableFor(Backend b) override { return b == kNonRendering_Backend; } |
| 222 | |
| 223 | void onDraw(int loops, SkCanvas*) override { |
| 224 | const SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 225 | SkTextBlobCacheDiffCanvas canvas{1024, 1024, props, fServer.get()}; |
| 226 | loops *= 100; |
| 227 | while (loops --> 0) { |
| 228 | for (const auto& record : fTrace) { |
| 229 | canvas.drawTextBlob( |
| 230 | record.blob.get(), record.offset.x(), record.offset.y(),record.paint); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void onDelayedSetup() override { |
Hal Canary | 5dfefa2 | 2019-10-24 13:52:17 -0400 | [diff] [blame] | 236 | auto stream = fDataProvider(); |
Hal Canary | e107faa | 2019-10-23 12:52:33 -0400 | [diff] [blame] | 237 | fDiscardableManager = sk_make_sp<DiscardableManager>(); |
| 238 | fServer.init(fDiscardableManager.get()); |
| 239 | fTrace = SkTextBlobTrace::CreateBlobTrace(stream.get()); |
| 240 | } |
| 241 | |
| 242 | public: |
Hal Canary | 5dfefa2 | 2019-10-24 13:52:17 -0400 | [diff] [blame] | 243 | DiffCanvasBench(SkString n, std::function<std::unique_ptr<SkStreamAsset>()> f) |
| 244 | : fBenchName(std::move(n)), fDataProvider(std::move(f)) {} |
Hal Canary | e107faa | 2019-10-23 12:52:33 -0400 | [diff] [blame] | 245 | }; |
| 246 | } // namespace |
| 247 | |
Hal Canary | 5dfefa2 | 2019-10-24 13:52:17 -0400 | [diff] [blame] | 248 | Benchmark* CreateDiffCanvasBench( |
| 249 | SkString name, std::function<std::unique_ptr<SkStreamAsset>()> dataSrc) { |
| 250 | return new DiffCanvasBench(std::move(name), std::move(dataSrc)); |
| 251 | } |
| 252 | |
| 253 | DEF_BENCH( return CreateDiffCanvasBench( |
| 254 | SkString("SkDiffBench-lorem_ipsum"), |
| 255 | [](){ return GetResourceAsStream("diff_canvas_traces/lorem_ipsum.trace"); })); |