skia2/gm/verttext2.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

102 lines
3.1 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.
*/
/* Tests text vertical text rendering with different fonts and centering.
*/
#include "gm.h"
#include "SkCanvas.h"
#include "SkTypeface.h"
namespace skiagm {
class VertText2GM : public GM {
public:
VertText2GM()
: fProp(NULL)
, fMono(NULL) {
}
virtual ~VertText2GM() {
SkSafeUnref(fProp);
SkSafeUnref(fMono);
}
protected:
void onOnceBeforeDraw() override {
const int pointSize = 24;
textHeight = SkIntToScalar(pointSize);
fProp = sk_tool_utils::create_portable_typeface("Helvetica", SkTypeface::kNormal);
fMono = sk_tool_utils::create_portable_typeface("Courier New", SkTypeface::kNormal);
}
SkString onShortName() override {
return SkString("verttext2");
}
SkISize onISize() override { return SkISize::Make(640, 480); }
void onDraw(SkCanvas* canvas) override {
for (int i = 0; i < 3; ++i) {
SkPaint paint;
paint.setColor(SK_ColorRED);
paint.setAntiAlias(true);
y = textHeight;
canvas->drawLine(0, SkIntToScalar(10),
SkIntToScalar(110), SkIntToScalar(10), paint);
canvas->drawLine(0, SkIntToScalar(240),
SkIntToScalar(110), SkIntToScalar(240), paint);
canvas->drawLine(0, SkIntToScalar(470),
SkIntToScalar(110), SkIntToScalar(470), paint);
drawText(canvas, SkString("Proportional / Top Aligned"),
fProp, SkPaint::kLeft_Align);
drawText(canvas, SkString("< Proportional / Centered >"),
fProp, SkPaint::kCenter_Align);
drawText(canvas, SkString("Monospaced / Top Aligned"),
fMono, SkPaint::kLeft_Align);
drawText(canvas, SkString("< Monospaced / Centered >"),
fMono, SkPaint::kCenter_Align);
canvas->rotate(SkIntToScalar(-15));
canvas->translate(textHeight * 4, SkIntToScalar(50));
if (i > 0) {
canvas->translate(0, SkIntToScalar(50));
}
}
}
void drawText(SkCanvas* canvas, const SkString& string,
SkTypeface* family, SkPaint::Align alignment) {
SkPaint paint;
paint.setColor(SK_ColorBLACK);
paint.setAntiAlias(true);
paint.setVerticalText(true);
paint.setTextAlign(alignment);
paint.setTypeface(family);
paint.setTextSize(textHeight);
canvas->drawText(string.c_str(), string.size(), y,
SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240),
paint);
y += textHeight;
}
private:
typedef GM INHERITED;
SkScalar y, textHeight;
SkTypeface* fProp;
SkTypeface* fMono;
};
///////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new VertText2GM; }
static GMRegistry reg(MyFactory);
}