blob: 1cc257fd3fbac53acb5c5dc80cdaccb996eb6b23 [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"
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
20static 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
39class SkGlyphCacheBasic : public Benchmark {
40public:
41 explicit SkGlyphCacheBasic(size_t cacheSize) : fCacheSize(cacheSize) { }
42
43protected:
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
mtkleina1ebeb22015-10-01 09:43:39 -070053 void onDraw(int loops, SkCanvas*) override {
herb1052f512015-09-18 12:09:43 -070054 size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
55 SkGraphics::SetFontCacheLimit(fCacheSize);
herb1052f512015-09-18 12:09:43 -070056 SkPaint paint;
57 paint.setAntiAlias(true);
58 paint.setSubpixelText(true);
Ben Wagner71319502017-07-27 10:45:29 -040059 paint.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()));
herb1052f512015-09-18 12:09:43 -070060
61 for (int work = 0; work < loops; work++) {
62 do_font_stuff(&paint);
63 }
64 SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
65 }
66
67private:
68 typedef Benchmark INHERITED;
69 const size_t fCacheSize;
70 SkString fName;
71};
72
73class SkGlyphCacheStressTest : public Benchmark {
74public:
75 explicit SkGlyphCacheStressTest(int cacheSize) : fCacheSize(cacheSize) { }
76
77protected:
78 const char* onGetName() override {
79 fName.printf("SkGlyphCacheStressTest%dK", (int)(fCacheSize >> 10));
80 return fName.c_str();
81 }
82
83 bool isSuitableFor(Backend backend) override {
84 return backend == kNonRendering_Backend;
85 }
86
mtkleina1ebeb22015-10-01 09:43:39 -070087 void onDraw(int loops, SkCanvas*) override {
herb1052f512015-09-18 12:09:43 -070088 size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
89 SkGraphics::SetFontCacheLimit(fCacheSize);
bungeman13b9c952016-05-12 10:09:30 -070090 sk_sp<SkTypeface> typefaces[] =
Ben Wagner71319502017-07-27 10:45:29 -040091 {sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()),
92 sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Italic())};
herb1052f512015-09-18 12:09:43 -070093
94 for (int work = 0; work < loops; work++) {
mtklein279c7862016-01-04 19:13:19 -080095 SkTaskGroup().batch(16, [&](int threadIndex) {
herb1052f512015-09-18 12:09:43 -070096 SkPaint paint;
97 paint.setAntiAlias(true);
98 paint.setSubpixelText(true);
99 paint.setTypeface(typefaces[threadIndex % 2]);
100 do_font_stuff(&paint);
101 });
102 }
103 SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
herb1052f512015-09-18 12:09:43 -0700104 }
105
106private:
107 typedef Benchmark INHERITED;
108 const size_t fCacheSize;
109 SkString fName;
110};
111
112DEF_BENCH( return new SkGlyphCacheBasic(256 * 1024); )
113DEF_BENCH( return new SkGlyphCacheBasic(32 * 1024 * 1024); )
114DEF_BENCH( return new SkGlyphCacheStressTest(256 * 1024); )
115DEF_BENCH( return new SkGlyphCacheStressTest(32 * 1024 * 1024); )