| 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" | 
 | 13 | #include "SkGlyphCache_Globals.h" | 
 | 14 | #include "SkGraphics.h" | 
 | 15 | #include "SkTaskGroup.h" | 
 | 16 | #include "SkTypeface.h" | 
 | 17 | #include "sk_tool_utils.h" | 
 | 18 |  | 
 | 19 |  | 
 | 20 | static void do_font_stuff(SkPaint* paint) { | 
 | 21 |     for (SkScalar i = 8; i < 64; i++) { | 
 | 22 |         paint->setTextSize(i); | 
 | 23 |         SkAutoGlyphCacheNoGamma autoCache(*paint, nullptr, nullptr); | 
 | 24 |         SkGlyphCache* cache = autoCache.getCache(); | 
 | 25 |         uint16_t glyphs['z']; | 
 | 26 |         for (int c = ' '; c < 'z'; c++) { | 
 | 27 |             glyphs[c] = cache->unicharToGlyph(c); | 
 | 28 |         } | 
 | 29 |         for (int lookups = 0; lookups < 10; lookups++) { | 
 | 30 |             for (int c = ' '; c < 'z'; c++) { | 
 | 31 |                 const SkGlyph& g = cache->getGlyphIDMetrics(glyphs[c]); | 
 | 32 |                 cache->findImage(g); | 
 | 33 |             } | 
 | 34 |         } | 
 | 35 |  | 
 | 36 |     } | 
 | 37 | } | 
 | 38 |  | 
 | 39 | class SkGlyphCacheBasic : public Benchmark { | 
 | 40 | public: | 
 | 41 |     explicit SkGlyphCacheBasic(size_t cacheSize) : fCacheSize(cacheSize) { } | 
 | 42 |  | 
 | 43 | protected: | 
 | 44 |     const char* onGetName() override { | 
 | 45 |         fName.printf("SkGlyphCacheBasic%dK", (int)(fCacheSize >> 10)); | 
 | 46 |         return fName.c_str(); | 
 | 47 |     } | 
 | 48 |  | 
 | 49 |     bool isSuitableFor(Backend backend) override { | 
 | 50 |         return backend == kNonRendering_Backend; | 
 | 51 |     } | 
 | 52 |  | 
| mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 53 |     void onDraw(int loops, SkCanvas*) override { | 
| herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 54 |         size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit(); | 
 | 55 |         SkGraphics::SetFontCacheLimit(fCacheSize); | 
| herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 56 |         SkPaint paint; | 
 | 57 |         paint.setAntiAlias(true); | 
 | 58 |         paint.setSubpixelText(true); | 
| mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 59 |         paint.setTypeface(sk_tool_utils::create_portable_typeface( | 
 | 60 |                               "serif", SkFontStyle::FromOldStyle(SkTypeface::kItalic))); | 
| herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 61 |  | 
 | 62 |         for (int work = 0; work < loops; work++) { | 
 | 63 |             do_font_stuff(&paint); | 
 | 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); | 
| bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 91 |         sk_sp<SkTypeface> typefaces[] = | 
| mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 92 |             {sk_tool_utils::create_portable_typeface("serif", | 
 | 93 |                   SkFontStyle::FromOldStyle(SkTypeface::kItalic)), | 
 | 94 |              sk_tool_utils::create_portable_typeface("sans-serif", | 
 | 95 |                   SkFontStyle::FromOldStyle(SkTypeface::kItalic))}; | 
| 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) { | 
| herb | 1052f51 | 2015-09-18 12:09:43 -0700 | [diff] [blame] | 99 |                 SkPaint paint; | 
 | 100 |                 paint.setAntiAlias(true); | 
 | 101 |                 paint.setSubpixelText(true); | 
 | 102 |                 paint.setTypeface(typefaces[threadIndex % 2]); | 
 | 103 |                 do_font_stuff(&paint); | 
 | 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); ) |