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 | |
| 8 | |
| 9 | #include "SkGlyphCache.h" |
| 10 | |
| 11 | #include "Benchmark.h" |
| 12 | #include "SkCanvas.h" |
Herb Derby | 840c66c | 2018-04-16 16:42:08 -0400 | [diff] [blame] | 13 | #include "SkStrikeCache.h" |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 14 | #include "SkGraphics.h" |
| 15 | #include "SkTaskGroup.h" |
| 16 | #include "SkTypeface.h" |
| 17 | #include "sk_tool_utils.h" |
| 18 | |
| 19 | |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame^] | 20 | static void do_font_stuff(SkFont* font) { |
| 21 | SkPaint defaultPaint; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 22 | for (SkScalar i = 8; i < 64; i++) { |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame^] | 23 | font->setSize(i); |
Herb Derby | fa99690 | 2018-04-18 11:36:12 -0400 | [diff] [blame] | 24 | auto cache = SkStrikeCache::FindOrCreateStrikeExclusive( |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame^] | 25 | *font, defaultPaint, SkSurfaceProps(0, kUnknown_SkPixelGeometry), |
Herb Derby | c341500 | 2018-11-08 16:40:26 -0500 | [diff] [blame] | 26 | SkScalerContextFlags::kNone, SkMatrix::I()); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 27 | uint16_t glyphs['z']; |
| 28 | for (int c = ' '; c < 'z'; c++) { |
| 29 | glyphs[c] = cache->unicharToGlyph(c); |
| 30 | } |
| 31 | for (int lookups = 0; lookups < 10; lookups++) { |
| 32 | for (int c = ' '; c < 'z'; c++) { |
| 33 | const SkGlyph& g = cache->getGlyphIDMetrics(glyphs[c]); |
| 34 | cache->findImage(g); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | class SkGlyphCacheBasic : public Benchmark { |
| 42 | public: |
| 43 | explicit SkGlyphCacheBasic(size_t cacheSize) : fCacheSize(cacheSize) { } |
| 44 | |
| 45 | protected: |
| 46 | const char* onGetName() override { |
| 47 | fName.printf("SkGlyphCacheBasic%dK", (int)(fCacheSize >> 10)); |
| 48 | return fName.c_str(); |
| 49 | } |
| 50 | |
| 51 | bool isSuitableFor(Backend backend) override { |
| 52 | return backend == kNonRendering_Backend; |
| 53 | } |
| 54 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 55 | void onDraw(int loops, SkCanvas*) override { |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 56 | size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit(); |
| 57 | SkGraphics::SetFontCacheLimit(fCacheSize); |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame^] | 58 | SkFont font; |
| 59 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 60 | font.setSubpixel(true); |
| 61 | font.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic())); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 62 | |
| 63 | for (int work = 0; work < loops; work++) { |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame^] | 64 | do_font_stuff(&font); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 65 | } |
| 66 | SkGraphics::SetFontCacheLimit(oldCacheLimitSize); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | typedef Benchmark INHERITED; |
| 71 | const size_t fCacheSize; |
| 72 | SkString fName; |
| 73 | }; |
| 74 | |
| 75 | class SkGlyphCacheStressTest : public Benchmark { |
| 76 | public: |
| 77 | explicit SkGlyphCacheStressTest(int cacheSize) : fCacheSize(cacheSize) { } |
| 78 | |
| 79 | protected: |
| 80 | const char* onGetName() override { |
| 81 | fName.printf("SkGlyphCacheStressTest%dK", (int)(fCacheSize >> 10)); |
| 82 | return fName.c_str(); |
| 83 | } |
| 84 | |
| 85 | bool isSuitableFor(Backend backend) override { |
| 86 | return backend == kNonRendering_Backend; |
| 87 | } |
| 88 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 89 | void onDraw(int loops, SkCanvas*) override { |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 90 | size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit(); |
| 91 | SkGraphics::SetFontCacheLimit(fCacheSize); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 92 | sk_sp<SkTypeface> typefaces[] = |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 93 | {sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()), |
| 94 | sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Italic())}; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 95 | |
| 96 | for (int work = 0; work < loops; work++) { |
mtklein | 279c786 | 2016-01-04 19:13:19 -0800 | [diff] [blame] | 97 | SkTaskGroup().batch(16, [&](int threadIndex) { |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame^] | 98 | SkFont font; |
| 99 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 100 | font.setSubpixel(true); |
| 101 | font.setTypeface(typefaces[threadIndex % 2]); |
| 102 | do_font_stuff(&font); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 103 | }); |
| 104 | } |
| 105 | SkGraphics::SetFontCacheLimit(oldCacheLimitSize); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | private: |
| 109 | typedef Benchmark INHERITED; |
| 110 | const size_t fCacheSize; |
| 111 | SkString fName; |
| 112 | }; |
| 113 | |
| 114 | DEF_BENCH( return new SkGlyphCacheBasic(256 * 1024); ) |
| 115 | DEF_BENCH( return new SkGlyphCacheBasic(32 * 1024 * 1024); ) |
| 116 | DEF_BENCH( return new SkGlyphCacheStressTest(256 * 1024); ) |
| 117 | DEF_BENCH( return new SkGlyphCacheStressTest(32 * 1024 * 1024); ) |