Debian Codegraph @

bench/SkGlyphCacheBench.cpp

raw
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 #include "src/core/SkStrike.h"
9
10 #include "bench/Benchmark.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkFontMgr.h"
14 #include "include/core/SkGraphics.h"
15 #include "include/core/SkTypeface.h"
16 #include "include/private/chromium/SkChromeRemoteGlyphCache.h"
17 #include "src/core/SkStrikeSpec.h"
18 #include "src/core/SkTaskGroup.h"
19 #include "tools/Resources.h"
20 #include "tools/ToolUtils.h"
21 #include "tools/fonts/FontToolUtils.h"
22 #include "tools/text/SkTextBlobTrace.h"
23
24 #include <optional>
25
26 using namespace skia_private;
27
28 static void do_font_stuff(SkFont* font) {
29 SkPaint defaultPaint;
30 for (SkScalar i = 8; i < 64; i++) {
31 font->setSize(i);
32 auto strikeSpec = SkStrikeSpec::MakeMask(
33 *font, defaultPaint, SkSurfaceProps(0, kUnknown_SkPixelGeometry),
34 SkScalerContextFlags::kNone, SkMatrix::I());
35 SkPackedGlyphID glyphs['z'];
36 for (int c = ' '; c < 'z'; c++) {
37 glyphs[c] = SkPackedGlyphID{font->unicharToGlyph(c)};
38 }
39 constexpr size_t glyphCount = 'z' - ' ';
40 SkSpan<const SkPackedGlyphID> glyphIDs{&glyphs[SkTo<int>(' ')], glyphCount};
41 SkBulkGlyphMetricsAndImages images{strikeSpec};
42 for (int lookups = 0; lookups < 10; lookups++) {
43 (void)images.glyphs(glyphIDs);
44 }
45 }
46 }
47
48 class SkGlyphCacheBasic : public Benchmark {
49 public:
50 explicit SkGlyphCacheBasic(size_t cacheSize) : fCacheSize(cacheSize) { }
51
52 protected:
53 const char* onGetName() override {
54 fName.printf("SkGlyphCacheBasic%dK", (int)(fCacheSize >> 10));
55 return fName.c_str();
56 }
57
58 bool isSuitableFor(Backend backend) override {
59 return backend == Backend::kNonRendering;
60 }
61
62 void onDraw(int loops, SkCanvas*) override {
63 size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
64 SkGraphics::SetFontCacheLimit(fCacheSize);
65 SkFont font = ToolUtils::DefaultFont();
66 font.setEdging(SkFont::Edging::kAntiAlias);
67 font.setSubpixel(true);
68 font.setTypeface(ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Italic()));
69
70 for (int work = 0; work < loops; work++) {
71 do_font_stuff(&font);
72 }
73 SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
74 }
75
76 private:
77 using INHERITED = Benchmark;
78 const size_t fCacheSize;
79 SkString fName;
80 };
81
82 class SkGlyphCacheStressTest : public Benchmark {
83 public:
84 explicit SkGlyphCacheStressTest(int cacheSize) : fCacheSize(cacheSize) { }
85
86 protected:
87 const char* onGetName() override {
88 fName.printf("SkGlyphCacheStressTest%dK", (int)(fCacheSize >> 10));
89 return fName.c_str();
90 }
91
92 bool isSuitableFor(Backend backend) override {
93 return backend == Backend::kNonRendering;
94 }
95
96 void onDraw(int loops, SkCanvas*) override {
97 size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
98 SkGraphics::SetFontCacheLimit(fCacheSize);
99 sk_sp<SkTypeface> typefaces[] = {
100 ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Italic()),
101 ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Italic())};
102
103 for (int work = 0; work < loops; work++) {
104 SkTaskGroup().batch(16, [&](int threadIndex) {
105 SkFont font = ToolUtils::DefaultFont();
106 font.setEdging(SkFont::Edging::kAntiAlias);
107 font.setSubpixel(true);
108 font.setTypeface(typefaces[threadIndex % 2]);
109 do_font_stuff(&font);
110 });
111 }
112 SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
113 }
114
115 private:
116 using INHERITED = Benchmark;
117 const size_t fCacheSize;
118 SkString fName;
119 };
120
121 DEF_BENCH( return new SkGlyphCacheBasic(256 * 1024); )
122 DEF_BENCH( return new SkGlyphCacheBasic(32 * 1024 * 1024); )
123 DEF_BENCH( return new SkGlyphCacheStressTest(256 * 1024); )
124 DEF_BENCH( return new SkGlyphCacheStressTest(32 * 1024 * 1024); )
125
126 namespace {
127 class DiscardableManager : public SkStrikeServer::DiscardableHandleManager,
128 public SkStrikeClient::DiscardableHandleManager {
129 public:
130 DiscardableManager() { sk_bzero(&fCacheMissCount, sizeof(fCacheMissCount)); }
131 ~DiscardableManager() override = default;
132
133 // Server implementation.
134 SkDiscardableHandleId createHandle() override {
135 SkAutoMutexExclusive l(fMutex);
136
137 // Handles starts as locked.
138 fLockedHandles.add(++fNextHandleId);
139 return fNextHandleId;
140 }
141 bool lockHandle(SkDiscardableHandleId id) override {
142 SkAutoMutexExclusive l(fMutex);
143
144 if (id <= fLastDeletedHandleId) return false;
145 fLockedHandles.add(id);
146 return true;
147 }
148
149 // Client implementation.
150 bool deleteHandle(SkDiscardableHandleId id) override {
151 SkAutoMutexExclusive l(fMutex);
152
153 return id <= fLastDeletedHandleId;
154 }
155
156 void notifyCacheMiss(SkStrikeClient::CacheMissType type, int fontSize) override {
157 SkAutoMutexExclusive l(fMutex);
158
159 fCacheMissCount[type]++;
160 }
161 bool isHandleDeleted(SkDiscardableHandleId id) override {
162 SkAutoMutexExclusive l(fMutex);
163
164 return id <= fLastDeletedHandleId;
165 }
166
167 void unlockAll() {
168 SkAutoMutexExclusive l(fMutex);
169
170 fLockedHandles.reset();
171 }
172 void unlockAndDeleteAll() {
173 SkAutoMutexExclusive l(fMutex);
174
175 fLockedHandles.reset();
176 fLastDeletedHandleId = fNextHandleId;
177 }
178 const THashSet<SkDiscardableHandleId>& lockedHandles() const {
179 SkAutoMutexExclusive l(fMutex);
180
181 return fLockedHandles;
182 }
183 SkDiscardableHandleId handleCount() {
184 SkAutoMutexExclusive l(fMutex);
185
186 return fNextHandleId;
187 }
188 int cacheMissCount(uint32_t type) {
189 SkAutoMutexExclusive l(fMutex);
190
191 return fCacheMissCount[type];
192 }
193 bool hasCacheMiss() const {
194 SkAutoMutexExclusive l(fMutex);
195
196 for (uint32_t i = 0; i <= SkStrikeClient::CacheMissType::kLast; ++i) {
197 if (fCacheMissCount[i] > 0) return true;
198 }
199 return false;
200 }
201 void resetCacheMissCounts() {
202 SkAutoMutexExclusive l(fMutex);
203 sk_bzero(&fCacheMissCount, sizeof(fCacheMissCount));
204 }
205
206 private:
207 // The tests below run in parallel on multiple threads and use the same
208 // process global SkStrikeCache. So the implementation needs to be
209 // thread-safe.
210 mutable SkMutex fMutex;
211
212 SkDiscardableHandleId fNextHandleId = 0u;
213 SkDiscardableHandleId fLastDeletedHandleId = 0u;
214 THashSet<SkDiscardableHandleId> fLockedHandles;
215 int fCacheMissCount[SkStrikeClient::CacheMissType::kLast + 1u];
216 };
217
218 class DiffCanvasBench : public Benchmark {
219 SkString fBenchName;
220 std::function<std::unique_ptr<SkStreamAsset>()> fDataProvider;
221 std::vector<SkTextBlobTrace::Record> fTrace;
222 sk_sp<DiscardableManager> fDiscardableManager;
223 std::optional<SkStrikeServer> fServer;
224
225 const char* onGetName() override { return fBenchName.c_str(); }
226
227 bool isSuitableFor(Backend b) override { return b == Backend::kNonRendering; }
228
229 void onDraw(int loops, SkCanvas* modelCanvas) override {
230 SkSurfaceProps props;
231 if (modelCanvas) { modelCanvas->getProps(&props); }
232 std::unique_ptr<SkCanvas> canvas = fServer->makeAnalysisCanvas(1024, 1024, props,
233 nullptr, true, true);
234 loops *= 100;
235 while (loops --> 0) {
236 for (const auto& record : fTrace) {
237 canvas->drawTextBlob(
238 record.blob.get(), record.offset.x(), record.offset.y(),record.paint);
239 }
240 }
241 }
242
243 void onDelayedSetup() override {
244 auto stream = fDataProvider();
245 fDiscardableManager = sk_make_sp<DiscardableManager>();
246 fServer.emplace(fDiscardableManager.get());
247 fTrace = SkTextBlobTrace::CreateBlobTrace(stream.get(), nullptr);
248 }
249
250 public:
251 DiffCanvasBench(SkString n, std::function<std::unique_ptr<SkStreamAsset>()> f)
252 : fBenchName(std::move(n)), fDataProvider(std::move(f)) {}
253 };
254 } // namespace
255
256 Benchmark* CreateDiffCanvasBench(
257 SkString name, std::function<std::unique_ptr<SkStreamAsset>()> dataSrc) {
258 return new DiffCanvasBench(std::move(name), std::move(dataSrc));
259 }
260
261 DEF_BENCH( return CreateDiffCanvasBench(
262 SkString("SkDiffBench-lorem_ipsum"),
263 [](){ return GetResourceAsStream("diff_canvas_traces/lorem_ipsum.trace"); }))