skia2/bench/TextBench.cpp
kkinnunen b4a797f3aa Move font loading in gm tests and benches out of constructors
Constructing the gm tests and benches causes many calls to font loads.
This is visible as profiling samples in fontconfig and freetype on Linux
for all profiling runs of nanobench. This complicates analysis of
test-cases that are suspected of being slow due to font-related issues.

Move the font loading to GM::onOnceBeforeDraw and Benchmark::onPreDraw.
This way the code is not executed if the testcase does not match the
nanobench --match filter. This way the samples in font-related code are
more easy to identify as legitimate occurances caused by the testcase.

This should not cause differences in timings, because:
* Benchmark::preDraw / onPreDraw is defined to be run outside the timer
* GM::runAsBench is not enabled for any of the modified testcases. Also
  nanobench untimed warmup round should run the onOnceBeforeDraw.
  (and there are other GM::runAsBench gms already doing loading in
   onOnceBeforeDraw).

Changes the behavior:
In TextBench:
Before, the test would report two different gms with the same name if
the color emoji font was not loaded successfully.
After, the test always reports all tests as individual names.

Generally:
The errors from loading fonts now print inbetween each testcase, as
opposed to printing during construction phase. Sample output:
( 143/145 MB  1872) 14.7ms	8888 gm  quadclosepathResource /fonts/Funkster.ttf not a valid font.
( 160/160 MB  1831) 575µs	8888 gm  surfacenewResource /fonts/Funkster.ttf not a valid font.
( 163/165 MB  1816) 12.5ms	8888 gm  linepathResource /fonts/Funkster.ttf not a valid font.
( 263/411 MB  1493) 118ms	8888 gm  typefacestyles_kerningResource /fonts/Funkster.ttf not a valid font.
( 374/411 MB  1231) 7.16ms	565 gm  getpostextpathResource /fonts/Funkster.ttf not a valid font.
( 323/411 MB  1179) 4.92ms	565 gm  stringartResource /fonts/Funkster.ttf not a valid font.
( 347/493 MB   917) 191ms	565 gm  patch_gridResource /fonts/Funkster.ttf not a valid font.
( 375/493 MB   857) 23.9ms	gpu gm  clipdrawdrawCannot render path (0)
( 393/493 MB   706) 2.91ms	unit test  ParsePath------ png error IEND: CRC error
( 394/493 MB   584) 166ms	gpu gm  hairmodesResource /fonts/Funkster.ttf not a valid font.
Resource /fonts/Funkster.ttf not a valid font.
Resource /fonts/Funkster.ttf not a valid font.
...

Review URL: https://codereview.chromium.org/1144023002
2015-05-21 06:15:28 -07:00

173 lines
5.0 KiB
C++

/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Benchmark.h"
#include "Resources.h"
#include "SkCanvas.h"
#include "SkPaint.h"
#include "SkRandom.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkTemplates.h"
#include "SkTypeface.h"
enum FontQuality {
kBW,
kAA,
kLCD,
};
static const char* fontQualityName(const SkPaint& paint) {
if (!paint.isAntiAlias()) {
return "BW";
}
if (paint.isLCDRenderText()) {
return "LCD";
}
return "AA";
}
/* Some considerations for performance:
short -vs- long strings (measuring overhead)
tiny -vs- large pointsize (measure blit -vs- overhead)
1 -vs- many point sizes (measure cache lookup)
normal -vs- subpixel -vs- lineartext (minor)
force purge after each draw to measure scaler
textencoding?
text -vs- postext - pathtext
*/
class TextBench : public Benchmark {
SkPaint fPaint;
SkString fText;
SkString fName;
FontQuality fFQ;
bool fDoPos;
bool fDoColorEmoji;
SkAutoTUnref<SkTypeface> fColorEmojiTypeface;
SkPoint* fPos;
public:
TextBench(const char text[], int ps,
SkColor color, FontQuality fq, bool doColorEmoji = false, bool doPos = false)
: fText(text)
, fFQ(fq)
, fDoPos(doPos)
, fDoColorEmoji(doColorEmoji)
, fPos(NULL) {
fPaint.setAntiAlias(kBW != fq);
fPaint.setLCDRenderText(kLCD == fq);
fPaint.setTextSize(SkIntToScalar(ps));
fPaint.setColor(color);
}
virtual ~TextBench() {
delete[] fPos;
}
protected:
void onPreDraw() override {
if (fDoColorEmoji) {
SkASSERT(kBW == fFQ);
fColorEmojiTypeface.reset(GetResourceAsTypeface("/fonts/Funkster.ttf"));
}
if (fDoPos) {
size_t len = fText.size();
SkScalar* adv = new SkScalar[len];
fPaint.getTextWidths(fText.c_str(), len, adv);
fPos = new SkPoint[len];
SkScalar x = 0;
for (size_t i = 0; i < len; ++i) {
fPos[i].set(x, SkIntToScalar(50));
x += adv[i];
}
delete[] adv;
}
}
virtual const char* onGetName() {
fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
if (fDoPos) {
fName.append("_pos");
}
fName.appendf("_%s", fontQualityName(fPaint));
if (SK_ColorBLACK != fPaint.getColor()) {
fName.appendf("_%02X", fPaint.getAlpha());
} else {
fName.append("_BK");
}
if (fDoColorEmoji) {
fName.append("_ColorEmoji");
}
return fName.c_str();
}
virtual void onDraw(const int loops, SkCanvas* canvas) {
const SkIPoint dim = this->getSize();
SkRandom rand;
SkPaint paint(fPaint);
this->setupPaint(&paint);
// explicitly need these
paint.setColor(fPaint.getColor());
paint.setAntiAlias(kBW != fFQ);
paint.setLCDRenderText(kLCD == fFQ);
if (fDoColorEmoji && fColorEmojiTypeface) {
paint.setTypeface(fColorEmojiTypeface);
}
const SkScalar x0 = SkIntToScalar(-10);
const SkScalar y0 = SkIntToScalar(-10);
if (fDoPos) {
// realistically, the matrix is often at least translated, so we
// do that since it exercises different code in drawPosText.
canvas->translate(SK_Scalar1, SK_Scalar1);
}
for (int i = 0; i < loops; i++) {
if (fDoPos) {
canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
} else {
SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
}
}
}
private:
typedef Benchmark INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
#define STR "Hamburgefons"
DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW); )
DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW); )
DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW); )
DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); )
DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); )
DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); )
DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); )
DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); )
DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); )
DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true); )
DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW, true); )
DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW, true); )
DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true, true); )
DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, false, true); )