blob: b517d26848d45425a0bec308419abf94a3ce97cc [file] [log] [blame]
joshualitteef5b3e2015-04-03 08:07:26 -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#include "Benchmark.h"
9#include "Resources.h"
10#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkStream.h"
14#include "SkString.h"
15#include "SkTemplates.h"
16#include "SkTextBlob.h"
17#include "SkTypeface.h"
18
19#include "sk_tool_utils.h"
20
21/*
22 * A trivial test which benchmarks the performance of a textblob with a single run.
23 */
Herb Derby430424b2018-08-07 13:29:41 -040024class SkTextBlobBench : public Benchmark {
joshualitteef5b3e2015-04-03 08:07:26 -070025public:
Herb Derby430424b2018-08-07 13:29:41 -040026 SkTextBlobBench() {}
kkinnunenb4a797f2015-05-21 06:15:28 -070027
joshualitt8a6697a2015-09-30 12:11:07 -070028 void onDelayedSetup() override {
Herb Derby430424b2018-08-07 13:29:41 -040029 fPaint.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle()));
Herb Derby55bc0512018-10-08 14:06:09 -040030 fPaint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
joshualitteef5b3e2015-04-03 08:07:26 -070031
Herb Derby430424b2018-08-07 13:29:41 -040032 // This text seems representative in both length and letter frequency.
33 const char* text = "Keep your sentences short, but not overly so.";
joshualitteef5b3e2015-04-03 08:07:26 -070034
Herb Derby430424b2018-08-07 13:29:41 -040035 fGlyphs.setCount(fPaint.textToGlyphs(text, strlen(text), nullptr));
36 fPaint.textToGlyphs(text, strlen(text), fGlyphs.begin());
Herb Derby55bc0512018-10-08 14:06:09 -040037
38 fPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
39 fPaint.setSubpixelText(true);
joshualitteef5b3e2015-04-03 08:07:26 -070040 }
41
Herb Derby430424b2018-08-07 13:29:41 -040042 sk_sp<SkTextBlob> makeBlob() {
43 const SkTextBlobBuilder::RunBuffer& run =
44 fBuilder.allocRunPosH(fPaint, fGlyphs.count(), 10, nullptr);
45 for (int i = 0; i < fGlyphs.count(); i++) {
46 run.glyphs[i] = fGlyphs[i];
Herb Derby55bc0512018-10-08 14:06:09 -040047 run. pos[i] = (i+1) * 10.125;
Herb Derby430424b2018-08-07 13:29:41 -040048 }
49 return fBuilder.make();
50 }
51
52private:
53 SkTextBlobBuilder fBuilder;
54 SkPaint fPaint;
55 SkTDArray<uint16_t> fGlyphs;
56
57 typedef Benchmark INHERITED;
58};
59
60class TextBlobCachedBench : public SkTextBlobBench {
mtkleinf0599002015-07-13 06:18:39 -070061 const char* onGetName() override {
Herb Derby430424b2018-08-07 13:29:41 -040062 return "TextBlobCachedBench";
joshualitteef5b3e2015-04-03 08:07:26 -070063 }
64
mtkleina1ebeb22015-10-01 09:43:39 -070065 void onDraw(int loops, SkCanvas* canvas) override {
joshualitteef5b3e2015-04-03 08:07:26 -070066 SkPaint paint;
67
Herb Derby430424b2018-08-07 13:29:41 -040068 auto blob = this->makeBlob();
Herb Derby55bc0512018-10-08 14:06:09 -040069 auto bigLoops = loops * 100;
Herb Derby434377a2018-10-02 16:48:17 -040070 for (int i = 0; i < bigLoops; i++) {
Herb Derby430424b2018-08-07 13:29:41 -040071 // To ensure maximum caching, we just redraw the blob at the same place everytime
72 canvas->drawTextBlob(blob, 0, 0, paint);
joshualitteef5b3e2015-04-03 08:07:26 -070073 }
74 }
joshualitteef5b3e2015-04-03 08:07:26 -070075};
76
Herb Derby430424b2018-08-07 13:29:41 -040077class TextBlobFirstTimeBench : public SkTextBlobBench {
78 const char* onGetName() override {
79 return "TextBlobFirstTimeBench";
80 }
81
82 void onDraw(int loops, SkCanvas* canvas) override {
83 SkPaint paint;
84
Herb Derby55bc0512018-10-08 14:06:09 -040085 auto bigLoops = loops * 100;
Herb Derby434377a2018-10-02 16:48:17 -040086 for (int i = 0; i < bigLoops; i++) {
Herb Derby430424b2018-08-07 13:29:41 -040087 canvas->drawTextBlob(this->makeBlob(), 0, 0, paint);
88 }
89 }
90};
91
92DEF_BENCH( return new TextBlobCachedBench(); )
93DEF_BENCH( return new TextBlobFirstTimeBench(); )