Flutter Engine
The Flutter Engine
third_party
skia
bench
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
"
9
#include "
include/core/SkCanvas.h
"
10
#include "
include/core/SkFont.h
"
11
#include "
include/core/SkPaint.h
"
12
#include "
include/core/SkStream.h
"
13
#include "
include/core/SkString.h
"
14
#include "
include/core/SkTextBlob.h
"
15
#include "
include/core/SkTypeface.h
"
16
#include "
include/private/base/SkTemplates.h
"
17
#include "
src/base/SkRandom.h
"
18
#include "
tools/Resources.h
"
19
#include "
tools/ToolUtils.h
"
20
#include "
tools/fonts/FontToolUtils.h
"
21
22
/*
23
* A trivial test which benchmarks the performance of a textblob with a single run.
24
*/
25
class
SkTextBlobBench
:
public
Benchmark
{
26
public
:
27
SkTextBlobBench
() {}
28
29
void
onDelayedSetup
()
override
{
30
fFont.
setTypeface
(
ToolUtils::CreatePortableTypeface
(
"serif"
,
SkFontStyle
()));
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
43
sk_sp<SkTextBlob>
makeBlob
() {
44
const
SkTextBlobBuilder::RunBuffer
&
run
=
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
51
private
:
52
SkTextBlobBuilder
fBuilder;
53
SkFont
fFont;
54
SkTDArray<uint16_t>
fGlyphs;
55
SkTDArray<SkScalar>
fXPos;
56
57
using
INHERITED =
Benchmark
;
58
};
59
60
class
TextBlobCachedBench
:
public
SkTextBlobBench
{
61
const
char
* onGetName()
override
{
62
return
"TextBlobCachedBench"
;
63
}
64
65
void
onDraw(
int
loops,
SkCanvas
* canvas)
override
{
66
SkPaint
paint
;
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
};
76
DEF_BENCH
(
return
new
TextBlobCachedBench
(); )
77
78
class
TextBlobFirstTimeBench
:
public
SkTextBlobBench
{
79
const
char
* onGetName()
override
{
80
return
"TextBlobFirstTimeBench"
;
81
}
82
83
void
onDraw(
int
loops,
SkCanvas
* canvas)
override
{
84
SkPaint
paint
;
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
};
92
DEF_BENCH
(
return
new
TextBlobFirstTimeBench
(); )
93
94
class
TextBlobMakeBench
:
public
SkTextBlobBench
{
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
};
111
DEF_BENCH
(
return
new
TextBlobMakeBench
(); )
Benchmark.h
DEF_BENCH
#define DEF_BENCH(code)
Definition:
Benchmark.h:20
backend
const char * backend
Definition:
CommonFlagsConfig.cpp:42
FontToolUtils.h
Resources.h
SkCanvas.h
SkTextEncoding::kUTF8
@ kUTF8
uses bytes to represent UTF-8 or ASCII
SkFont.h
SkPaint.h
SkRandom.h
SkStream.h
SkString.h
SkTemplates.h
SkTextBlob.h
SkTypeface.h
ToolUtils.h
Benchmark
Definition:
Benchmark.h:36
Benchmark::Backend
Backend
Definition:
Benchmark.h:44
Benchmark::Benchmark
Benchmark()
Definition:
Benchmark.cpp:16
SkCanvas
Definition:
SkCanvas.h:106
SkCanvas::drawTextBlob
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition:
SkCanvas.cpp:2484
SkFontStyle
Definition:
SkFontStyle.h:16
SkFont
Definition:
SkFont.h:35
SkFont::setSubpixel
void setSubpixel(bool subpixel)
Definition:
SkFont.cpp:109
SkFont::getXPos
void getXPos(const SkGlyphID glyphs[], int count, SkScalar xpos[], SkScalar origin=0) const
Definition:
SkFont.cpp:270
SkFont::setTypeface
void setTypeface(sk_sp< SkTypeface > tf)
Definition:
SkFont.cpp:90
SkFont::countText
int countText(const void *text, size_t byteLength, SkTextEncoding encoding) const
Definition:
SkFont.h:323
SkFont::textToGlyphs
int textToGlyphs(const void *text, size_t byteLength, SkTextEncoding encoding, SkGlyphID glyphs[], int maxGlyphCount) const
Definition:
SkFont.cpp:181
SkPaint
Definition:
SkPaint.h:44
SkTDArray< uint16_t >
SkTDArray::size
int size() const
Definition:
SkTDArray.h:138
SkTDArray::begin
T * begin()
Definition:
SkTDArray.h:150
SkTDArray::resize
void resize(int count)
Definition:
SkTDArray.h:183
SkTextBlobBench
Definition:
TextBlobBench.cpp:25
SkTextBlobBench::onDelayedSetup
void onDelayedSetup() override
Definition:
TextBlobBench.cpp:29
SkTextBlobBench::SkTextBlobBench
SkTextBlobBench()
Definition:
TextBlobBench.cpp:27
SkTextBlobBench::makeBlob
sk_sp< SkTextBlob > makeBlob()
Definition:
TextBlobBench.cpp:43
SkTextBlobBuilder
Definition:
SkTextBlob.h:290
SkTextBlobBuilder::allocRunPosH
const RunBuffer & allocRunPosH(const SkFont &font, int count, SkScalar y, const SkRect *bounds=nullptr)
Definition:
SkTextBlob.cpp:546
SkTextBlobBuilder::make
sk_sp< SkTextBlob > make()
Definition:
SkTextBlob.cpp:617
TextBlobCachedBench
Definition:
TextBlobBench.cpp:60
TextBlobFirstTimeBench
Definition:
TextBlobBench.cpp:78
TextBlobMakeBench
Definition:
TextBlobBench.cpp:94
sk_sp< SkTextBlob >
paint
const Paint & paint
Definition:
color_source.cc:38
SkScalar
float SkScalar
Definition:
extension.cpp:12
i
int i
Definition:
fl_socket_accessible.cc:18
text
std::u16string text
Definition:
keyboard_unittests.cc:332
ToolUtils::CreatePortableTypeface
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
Definition:
FontToolUtils.cpp:187
run
Definition:
run.py:1
SkTextBlobBuilder::RunBuffer
Definition:
SkTextBlob.h:328
Generated on Sun Jun 23 2024 21:55:54 for Flutter Engine by
1.9.4