blob: c99b5728fd305e2d5afbb633b90a92216fcac905 [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);
56 SkTypeface* typeface = sk_tool_utils::create_portable_typeface(
57 "serif", SkTypeface::kItalic);
58 SkPaint paint;
59 paint.setAntiAlias(true);
60 paint.setSubpixelText(true);
61 paint.setTypeface(typeface);
62
63 for (int work = 0; work < loops; work++) {
64 do_font_stuff(&paint);
65 }
66 SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
herb0d39d372015-09-18 13:52:18 -070067 SkSafeUnref(typeface);
herb1052f512015-09-18 12:09:43 -070068 }
69
70private:
71 typedef Benchmark INHERITED;
72 const size_t fCacheSize;
73 SkString fName;
74};
75
76class SkGlyphCacheStressTest : public Benchmark {
77public:
78 explicit SkGlyphCacheStressTest(int cacheSize) : fCacheSize(cacheSize) { }
79
80protected:
81 const char* onGetName() override {
82 fName.printf("SkGlyphCacheStressTest%dK", (int)(fCacheSize >> 10));
83 return fName.c_str();
84 }
85
86 bool isSuitableFor(Backend backend) override {
87 return backend == kNonRendering_Backend;
88 }
89
mtkleina1ebeb22015-10-01 09:43:39 -070090 void onDraw(int loops, SkCanvas*) override {
herb1052f512015-09-18 12:09:43 -070091 size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
92 SkGraphics::SetFontCacheLimit(fCacheSize);
93 SkTypeface* typefaces[] =
94 {sk_tool_utils::create_portable_typeface("serif", SkTypeface::kItalic),
95 sk_tool_utils::create_portable_typeface("sans-serif", SkTypeface::kItalic)};
96
97 for (int work = 0; work < loops; work++) {
98 sk_parallel_for(16, [&](int threadIndex) {
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);
herb0d39d372015-09-18 13:52:18 -0700107 SkSafeUnref(typefaces[0]);
108 SkSafeUnref(typefaces[1]);
herb1052f512015-09-18 12:09:43 -0700109 }
110
111private:
112 typedef Benchmark INHERITED;
113 const size_t fCacheSize;
114 SkString fName;
115};
116
117DEF_BENCH( return new SkGlyphCacheBasic(256 * 1024); )
118DEF_BENCH( return new SkGlyphCacheBasic(32 * 1024 * 1024); )
119DEF_BENCH( return new SkGlyphCacheStressTest(256 * 1024); )
120DEF_BENCH( return new SkGlyphCacheStressTest(32 * 1024 * 1024); )