blob: d26e9461a9ee6bd5a495b08262e10c3689ab4a6b [file] [log] [blame]
herb1052f512015-09-18 12:09:43 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/core/SkStrike.h"
herb1052f512015-09-18 12:09:43 -070010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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 Derbybaf64782019-04-17 18:01:04 -040016#include "src/core/SkStrikeSpec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkTaskGroup.h"
18#include "tools/ToolUtils.h"
herb1052f512015-09-18 12:09:43 -070019
Mike Reed32c60662018-11-28 10:28:07 -050020static void do_font_stuff(SkFont* font) {
21 SkPaint defaultPaint;
herb1052f512015-09-18 12:09:43 -070022 for (SkScalar i = 8; i < 64; i++) {
Mike Reed32c60662018-11-28 10:28:07 -050023 font->setSize(i);
Herb Derby36a54c12019-06-06 10:50:56 -040024 auto strikeSpec = SkStrikeSpec::MakeMask(
Mike Reed32c60662018-11-28 10:28:07 -050025 *font, defaultPaint, SkSurfaceProps(0, kUnknown_SkPixelGeometry),
Herb Derbyc3415002018-11-08 16:40:26 -050026 SkScalerContextFlags::kNone, SkMatrix::I());
Herb Derbybaf64782019-04-17 18:01:04 -040027 auto cache = strikeSpec.findOrCreateExclusiveStrike();
herb1052f512015-09-18 12:09:43 -070028 uint16_t glyphs['z'];
29 for (int c = ' '; c < 'z'; c++) {
Mike Reedfdb876d2019-02-01 09:55:20 -050030 glyphs[c] = font->unicharToGlyph(c);
herb1052f512015-09-18 12:09:43 -070031 }
32 for (int lookups = 0; lookups < 10; lookups++) {
33 for (int c = ' '; c < 'z'; c++) {
Herb Derby511dcfc2019-06-24 12:58:41 -040034 SkGlyph* g = cache->glyph(glyphs[c]);
35 cache->findImage(*g);
herb1052f512015-09-18 12:09:43 -070036 }
37 }
herb1052f512015-09-18 12:09:43 -070038 }
39}
40
41class SkGlyphCacheBasic : public Benchmark {
42public:
43 explicit SkGlyphCacheBasic(size_t cacheSize) : fCacheSize(cacheSize) { }
44
45protected:
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
mtkleina1ebeb22015-10-01 09:43:39 -070055 void onDraw(int loops, SkCanvas*) override {
herb1052f512015-09-18 12:09:43 -070056 size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
57 SkGraphics::SetFontCacheLimit(fCacheSize);
Mike Reed32c60662018-11-28 10:28:07 -050058 SkFont font;
59 font.setEdging(SkFont::Edging::kAntiAlias);
60 font.setSubpixel(true);
Mike Kleinea3f0142019-03-20 11:12:10 -050061 font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic()));
herb1052f512015-09-18 12:09:43 -070062
63 for (int work = 0; work < loops; work++) {
Mike Reed32c60662018-11-28 10:28:07 -050064 do_font_stuff(&font);
herb1052f512015-09-18 12:09:43 -070065 }
66 SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
67 }
68
69private:
70 typedef Benchmark INHERITED;
71 const size_t fCacheSize;
72 SkString fName;
73};
74
75class SkGlyphCacheStressTest : public Benchmark {
76public:
77 explicit SkGlyphCacheStressTest(int cacheSize) : fCacheSize(cacheSize) { }
78
79protected:
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
mtkleina1ebeb22015-10-01 09:43:39 -070089 void onDraw(int loops, SkCanvas*) override {
herb1052f512015-09-18 12:09:43 -070090 size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
91 SkGraphics::SetFontCacheLimit(fCacheSize);
Mike Kleinea3f0142019-03-20 11:12:10 -050092 sk_sp<SkTypeface> typefaces[] = {
93 ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic()),
94 ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Italic())};
herb1052f512015-09-18 12:09:43 -070095
96 for (int work = 0; work < loops; work++) {
mtklein279c7862016-01-04 19:13:19 -080097 SkTaskGroup().batch(16, [&](int threadIndex) {
Mike Reed32c60662018-11-28 10:28:07 -050098 SkFont font;
99 font.setEdging(SkFont::Edging::kAntiAlias);
100 font.setSubpixel(true);
101 font.setTypeface(typefaces[threadIndex % 2]);
102 do_font_stuff(&font);
herb1052f512015-09-18 12:09:43 -0700103 });
104 }
105 SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
herb1052f512015-09-18 12:09:43 -0700106 }
107
108private:
109 typedef Benchmark INHERITED;
110 const size_t fCacheSize;
111 SkString fName;
112};
113
114DEF_BENCH( return new SkGlyphCacheBasic(256 * 1024); )
115DEF_BENCH( return new SkGlyphCacheBasic(32 * 1024 * 1024); )
116DEF_BENCH( return new SkGlyphCacheStressTest(256 * 1024); )
117DEF_BENCH( return new SkGlyphCacheStressTest(32 * 1024 * 1024); )