blob: be99c9c483f5861f5888589aa12b642374253807 [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
9#include "SkGlyphCache.h"
10
11#include "Benchmark.h"
12#include "SkCanvas.h"
Herb Derby840c66c2018-04-16 16:42:08 -040013#include "SkStrikeCache.h"
herb1052f512015-09-18 12:09:43 -070014#include "SkGraphics.h"
15#include "SkTaskGroup.h"
16#include "SkTypeface.h"
17#include "sk_tool_utils.h"
18
19
20static void do_font_stuff(SkPaint* paint) {
21 for (SkScalar i = 8; i < 64; i++) {
22 paint->setTextSize(i);
Herb Derbyfa996902018-04-18 11:36:12 -040023 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
Herb Derbyc3415002018-11-08 16:40:26 -050024 *paint, SkSurfaceProps(0, kUnknown_SkPixelGeometry),
25 SkScalerContextFlags::kNone, SkMatrix::I());
herb1052f512015-09-18 12:09:43 -070026 uint16_t glyphs['z'];
27 for (int c = ' '; c < 'z'; c++) {
28 glyphs[c] = cache->unicharToGlyph(c);
29 }
30 for (int lookups = 0; lookups < 10; lookups++) {
31 for (int c = ' '; c < 'z'; c++) {
32 const SkGlyph& g = cache->getGlyphIDMetrics(glyphs[c]);
33 cache->findImage(g);
34 }
35 }
36
37 }
38}
39
40class SkGlyphCacheBasic : public Benchmark {
41public:
42 explicit SkGlyphCacheBasic(size_t cacheSize) : fCacheSize(cacheSize) { }
43
44protected:
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
mtkleina1ebeb22015-10-01 09:43:39 -070054 void onDraw(int loops, SkCanvas*) override {
herb1052f512015-09-18 12:09:43 -070055 size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
56 SkGraphics::SetFontCacheLimit(fCacheSize);
herb1052f512015-09-18 12:09:43 -070057 SkPaint paint;
58 paint.setAntiAlias(true);
59 paint.setSubpixelText(true);
Ben Wagner71319502017-07-27 10:45:29 -040060 paint.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()));
herb1052f512015-09-18 12:09:43 -070061
62 for (int work = 0; work < loops; work++) {
63 do_font_stuff(&paint);
64 }
65 SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
66 }
67
68private:
69 typedef Benchmark INHERITED;
70 const size_t fCacheSize;
71 SkString fName;
72};
73
74class SkGlyphCacheStressTest : public Benchmark {
75public:
76 explicit SkGlyphCacheStressTest(int cacheSize) : fCacheSize(cacheSize) { }
77
78protected:
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
mtkleina1ebeb22015-10-01 09:43:39 -070088 void onDraw(int loops, SkCanvas*) override {
herb1052f512015-09-18 12:09:43 -070089 size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
90 SkGraphics::SetFontCacheLimit(fCacheSize);
bungeman13b9c952016-05-12 10:09:30 -070091 sk_sp<SkTypeface> typefaces[] =
Ben Wagner71319502017-07-27 10:45:29 -040092 {sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()),
93 sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Italic())};
herb1052f512015-09-18 12:09:43 -070094
95 for (int work = 0; work < loops; work++) {
mtklein279c7862016-01-04 19:13:19 -080096 SkTaskGroup().batch(16, [&](int threadIndex) {
herb1052f512015-09-18 12:09:43 -070097 SkPaint paint;
98 paint.setAntiAlias(true);
99 paint.setSubpixelText(true);
100 paint.setTypeface(typefaces[threadIndex % 2]);
101 do_font_stuff(&paint);
102 });
103 }
104 SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
herb1052f512015-09-18 12:09:43 -0700105 }
106
107private:
108 typedef Benchmark INHERITED;
109 const size_t fCacheSize;
110 SkString fName;
111};
112
113DEF_BENCH( return new SkGlyphCacheBasic(256 * 1024); )
114DEF_BENCH( return new SkGlyphCacheBasic(32 * 1024 * 1024); )
115DEF_BENCH( return new SkGlyphCacheStressTest(256 * 1024); )
116DEF_BENCH( return new SkGlyphCacheStressTest(32 * 1024 * 1024); )