blob: f8397142568ae4be9f12ee74b2b286b5e5ddf253 [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()));
30 fPaint.setTextEncoding(SkPaint::kGlyphID_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());
joshualitteef5b3e2015-04-03 08:07:26 -070037 }
38
Herb Derby430424b2018-08-07 13:29:41 -040039 sk_sp<SkTextBlob> makeBlob() {
40 const SkTextBlobBuilder::RunBuffer& run =
41 fBuilder.allocRunPosH(fPaint, fGlyphs.count(), 10, nullptr);
42 for (int i = 0; i < fGlyphs.count(); i++) {
43 run.glyphs[i] = fGlyphs[i];
44 run. pos[i] = (i+1) * 10;
45 }
46 return fBuilder.make();
47 }
48
49private:
50 SkTextBlobBuilder fBuilder;
51 SkPaint fPaint;
52 SkTDArray<uint16_t> fGlyphs;
53
54 typedef Benchmark INHERITED;
55};
56
57class TextBlobCachedBench : public SkTextBlobBench {
mtkleinf0599002015-07-13 06:18:39 -070058 const char* onGetName() override {
Herb Derby430424b2018-08-07 13:29:41 -040059 return "TextBlobCachedBench";
joshualitteef5b3e2015-04-03 08:07:26 -070060 }
61
mtkleina1ebeb22015-10-01 09:43:39 -070062 void onDraw(int loops, SkCanvas* canvas) override {
joshualitteef5b3e2015-04-03 08:07:26 -070063 SkPaint paint;
64
Herb Derby430424b2018-08-07 13:29:41 -040065 auto blob = this->makeBlob();
joshualitteef5b3e2015-04-03 08:07:26 -070066 for (int i = 0; i < loops; i++) {
Herb Derby430424b2018-08-07 13:29:41 -040067 // To ensure maximum caching, we just redraw the blob at the same place everytime
68 canvas->drawTextBlob(blob, 0, 0, paint);
joshualitteef5b3e2015-04-03 08:07:26 -070069 }
70 }
joshualitteef5b3e2015-04-03 08:07:26 -070071};
72
Herb Derby430424b2018-08-07 13:29:41 -040073class TextBlobFirstTimeBench : public SkTextBlobBench {
74 const char* onGetName() override {
75 return "TextBlobFirstTimeBench";
76 }
77
78 void onDraw(int loops, SkCanvas* canvas) override {
79 SkPaint paint;
80
81 for (int i = 0; i < loops; i++) {
82 canvas->drawTextBlob(this->makeBlob(), 0, 0, paint);
83 }
84 }
85};
86
87DEF_BENCH( return new TextBlobCachedBench(); )
88DEF_BENCH( return new TextBlobFirstTimeBench(); )