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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/core/SkStrike.h" |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "bench/Benchmark.h" |
| 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/core/SkGraphics.h" |
| 14 | #include "include/core/SkTypeface.h" |
| 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" |
| 18 | #include "tools/ToolUtils.h" |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 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 | 36a54c1 | 2019-06-06 10:50:56 -0400 | [diff] [blame] | 24 | auto strikeSpec = SkStrikeSpec::MakeMask( |
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 Derby | 55a85fc | 2019-07-03 14:46:05 -0400 | [diff] [blame] | 27 | SkPackedGlyphID glyphs['z']; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 28 | for (int c = ' '; c < 'z'; c++) { |
Herb Derby | 55a85fc | 2019-07-03 14:46:05 -0400 | [diff] [blame] | 29 | glyphs[c] = SkPackedGlyphID{font->unicharToGlyph(c)}; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 30 | } |
Herb Derby | 55a85fc | 2019-07-03 14:46:05 -0400 | [diff] [blame] | 31 | constexpr size_t glyphCount = 'z' - ' '; |
| 32 | SkSpan<const SkPackedGlyphID> glyphIDs{&glyphs[SkTo<int>(' ')], glyphCount}; |
| 33 | SkBulkGlyphMetricsAndImages images{strikeSpec}; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 34 | for (int lookups = 0; lookups < 10; lookups++) { |
Herb Derby | 55a85fc | 2019-07-03 14:46:05 -0400 | [diff] [blame] | 35 | (void)images.glyphs(glyphIDs); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 36 | } |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | class SkGlyphCacheBasic : public Benchmark { |
| 41 | public: |
| 42 | explicit SkGlyphCacheBasic(size_t cacheSize) : fCacheSize(cacheSize) { } |
| 43 | |
| 44 | protected: |
| 45 | const char* onGetName() override { |
| 46 | fName.printf("SkGlyphCacheBasic%dK", (int)(fCacheSize >> 10)); |
| 47 | return fName.c_str(); |
| 48 | } |
| 49 | |
| 50 | bool isSuitableFor(Backend backend) override { |
| 51 | return backend == kNonRendering_Backend; |
| 52 | } |
| 53 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 54 | void onDraw(int loops, SkCanvas*) override { |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 55 | size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit(); |
| 56 | SkGraphics::SetFontCacheLimit(fCacheSize); |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 57 | SkFont font; |
| 58 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 59 | font.setSubpixel(true); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 60 | font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic())); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 61 | |
| 62 | for (int work = 0; work < loops; work++) { |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 63 | do_font_stuff(&font); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 64 | } |
| 65 | SkGraphics::SetFontCacheLimit(oldCacheLimitSize); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | typedef Benchmark INHERITED; |
| 70 | const size_t fCacheSize; |
| 71 | SkString fName; |
| 72 | }; |
| 73 | |
| 74 | class SkGlyphCacheStressTest : public Benchmark { |
| 75 | public: |
| 76 | explicit SkGlyphCacheStressTest(int cacheSize) : fCacheSize(cacheSize) { } |
| 77 | |
| 78 | protected: |
| 79 | const char* onGetName() override { |
| 80 | fName.printf("SkGlyphCacheStressTest%dK", (int)(fCacheSize >> 10)); |
| 81 | return fName.c_str(); |
| 82 | } |
| 83 | |
| 84 | bool isSuitableFor(Backend backend) override { |
| 85 | return backend == kNonRendering_Backend; |
| 86 | } |
| 87 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 88 | void onDraw(int loops, SkCanvas*) override { |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 89 | size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit(); |
| 90 | SkGraphics::SetFontCacheLimit(fCacheSize); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 91 | sk_sp<SkTypeface> typefaces[] = { |
| 92 | ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic()), |
| 93 | ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Italic())}; |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 94 | |
| 95 | for (int work = 0; work < loops; work++) { |
mtklein | 279c786 | 2016-01-04 19:13:19 -0800 | [diff] [blame] | 96 | SkTaskGroup().batch(16, [&](int threadIndex) { |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 97 | SkFont font; |
| 98 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 99 | font.setSubpixel(true); |
| 100 | font.setTypeface(typefaces[threadIndex % 2]); |
| 101 | do_font_stuff(&font); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 102 | }); |
| 103 | } |
| 104 | SkGraphics::SetFontCacheLimit(oldCacheLimitSize); |
herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private: |
| 108 | typedef Benchmark INHERITED; |
| 109 | const size_t fCacheSize; |
| 110 | SkString fName; |
| 111 | }; |
| 112 | |
| 113 | DEF_BENCH( return new SkGlyphCacheBasic(256 * 1024); ) |
| 114 | DEF_BENCH( return new SkGlyphCacheBasic(32 * 1024 * 1024); ) |
| 115 | DEF_BENCH( return new SkGlyphCacheStressTest(256 * 1024); ) |
| 116 | DEF_BENCH( return new SkGlyphCacheStressTest(32 * 1024 * 1024); ) |