Flutter Engine
The Flutter Engine
TextBlobBench.cpp
Go to the documentation of this file.
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 "bench/Benchmark.h"
10#include "include/core/SkFont.h"
17#include "src/base/SkRandom.h"
18#include "tools/Resources.h"
19#include "tools/ToolUtils.h"
21
22/*
23 * A trivial test which benchmarks the performance of a textblob with a single run.
24 */
25class SkTextBlobBench : public Benchmark {
26public:
28
29 void onDelayedSetup() override {
31 fFont.setSubpixel(true);
32
33 // This text seems representative in both length and letter frequency.
34 const char* text = "Keep your sentences short, but not overly so.";
35
36 fGlyphs.resize(fFont.countText(text, strlen(text), SkTextEncoding::kUTF8));
37 fXPos.resize(fGlyphs.size());
38
39 fFont.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphs.size());
40 fFont.getXPos(&fGlyphs[0], fGlyphs.size(), fXPos.begin());
41 }
42
45 fBuilder.allocRunPosH(fFont, fGlyphs.size(), 10, nullptr);
46 memcpy(run.glyphs, &fGlyphs[0], fGlyphs.size() * sizeof(uint16_t));
47 memcpy(run.pos, &fXPos[0], fXPos.size() * sizeof(SkScalar));
48 return fBuilder.make();
49 }
50
51private:
52 SkTextBlobBuilder fBuilder;
53 SkFont fFont;
54 SkTDArray<uint16_t> fGlyphs;
56
57 using INHERITED = Benchmark;
58};
59
61 const char* onGetName() override {
62 return "TextBlobCachedBench";
63 }
64
65 void onDraw(int loops, SkCanvas* canvas) override {
67
68 auto blob = this->makeBlob();
69 auto bigLoops = loops * 100;
70 for (int i = 0; i < bigLoops; i++) {
71 // To ensure maximum caching, we just redraw the blob at the same place everytime
72 canvas->drawTextBlob(blob, 0, 0, paint);
73 }
74 }
75};
76DEF_BENCH( return new TextBlobCachedBench(); )
77
79 const char* onGetName() override {
80 return "TextBlobFirstTimeBench";
81 }
82
83 void onDraw(int loops, SkCanvas* canvas) override {
85
86 auto bigLoops = loops * 100;
87 for (int i = 0; i < bigLoops; i++) {
88 canvas->drawTextBlob(this->makeBlob(), 0, 0, paint);
89 }
90 }
91};
92DEF_BENCH( return new TextBlobFirstTimeBench(); )
93
95 const char* onGetName() override {
96 return "TextBlobMakeBench";
97 }
98
99 bool isSuitableFor(Backend backend) override {
100 return backend == Backend::kNonRendering;
101 }
102
103 void onDraw(int loops, SkCanvas*) override {
104 for (int i = 0; i < loops; i++) {
105 for (int inner = 0; inner < 1000; ++inner) {
106 this->makeBlob();
107 }
108 }
109 }
110};
111DEF_BENCH( return new TextBlobMakeBench(); )
#define DEF_BENCH(code)
Definition: Benchmark.h:20
const char * backend
@ kUTF8
uses bytes to represent UTF-8 or ASCII
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
Definition: SkFont.h:35
void setSubpixel(bool subpixel)
Definition: SkFont.cpp:109
void getXPos(const SkGlyphID glyphs[], int count, SkScalar xpos[], SkScalar origin=0) const
Definition: SkFont.cpp:270
void setTypeface(sk_sp< SkTypeface > tf)
Definition: SkFont.cpp:90
int countText(const void *text, size_t byteLength, SkTextEncoding encoding) const
Definition: SkFont.h:323
int textToGlyphs(const void *text, size_t byteLength, SkTextEncoding encoding, SkGlyphID glyphs[], int maxGlyphCount) const
Definition: SkFont.cpp:181
int size() const
Definition: SkTDArray.h:138
T * begin()
Definition: SkTDArray.h:150
void resize(int count)
Definition: SkTDArray.h:183
void onDelayedSetup() override
sk_sp< SkTextBlob > makeBlob()
const RunBuffer & allocRunPosH(const SkFont &font, int count, SkScalar y, const SkRect *bounds=nullptr)
Definition: SkTextBlob.cpp:546
sk_sp< SkTextBlob > make()
Definition: SkTextBlob.cpp:617
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
std::u16string text
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
Definition: run.py:1