2015-04-03 15:07:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTextBlob.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/private/SkTemplates.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "tools/Resources.h"
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2015-04-03 15:07:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A trivial test which benchmarks the performance of a textblob with a single run.
|
|
|
|
*/
|
2018-08-07 17:29:41 +00:00
|
|
|
class SkTextBlobBench : public Benchmark {
|
2015-04-03 15:07:26 +00:00
|
|
|
public:
|
2018-08-07 17:29:41 +00:00
|
|
|
SkTextBlobBench() {}
|
2015-05-21 13:15:28 +00:00
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
void onDelayedSetup() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fFont.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle()));
|
2018-11-21 20:10:08 +00:00
|
|
|
fFont.setSubpixel(true);
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2018-08-07 17:29:41 +00:00
|
|
|
// This text seems representative in both length and letter frequency.
|
|
|
|
const char* text = "Keep your sentences short, but not overly so.";
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
fGlyphs.setCount(fFont.countText(text, strlen(text), SkTextEncoding::kUTF8));
|
2018-12-05 19:15:25 +00:00
|
|
|
fXPos.setCount(fGlyphs.count());
|
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
fFont.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphs.count());
|
2018-12-05 19:15:25 +00:00
|
|
|
fFont.getXPos(&fGlyphs[0], fGlyphs.count(), fXPos.begin());
|
2018-08-07 17:29:41 +00:00
|
|
|
}
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2018-08-07 17:29:41 +00:00
|
|
|
sk_sp<SkTextBlob> makeBlob() {
|
|
|
|
const SkTextBlobBuilder::RunBuffer& run =
|
2018-11-21 20:10:08 +00:00
|
|
|
fBuilder.allocRunPosH(fFont, fGlyphs.count(), 10, nullptr);
|
2018-11-24 18:27:27 +00:00
|
|
|
memcpy(run.glyphs, &fGlyphs[0], fGlyphs.count() * sizeof(uint16_t));
|
2018-12-05 19:15:25 +00:00
|
|
|
memcpy(run.pos, &fXPos[0], fXPos.count() * sizeof(SkScalar));
|
2018-08-07 17:29:41 +00:00
|
|
|
return fBuilder.make();
|
2015-04-03 15:07:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 17:29:41 +00:00
|
|
|
private:
|
2018-11-21 20:10:08 +00:00
|
|
|
SkTextBlobBuilder fBuilder;
|
|
|
|
SkFont fFont;
|
|
|
|
SkTDArray<uint16_t> fGlyphs;
|
2018-12-05 19:15:25 +00:00
|
|
|
SkTDArray<SkScalar> fXPos;
|
2018-08-07 17:29:41 +00:00
|
|
|
|
|
|
|
typedef Benchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TextBlobCachedBench : public SkTextBlobBench {
|
2015-07-13 13:18:39 +00:00
|
|
|
const char* onGetName() override {
|
2018-08-07 17:29:41 +00:00
|
|
|
return "TextBlobCachedBench";
|
2015-04-03 15:07:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2015-04-03 15:07:26 +00:00
|
|
|
SkPaint paint;
|
|
|
|
|
2018-08-07 17:29:41 +00:00
|
|
|
auto blob = this->makeBlob();
|
2018-10-08 18:06:09 +00:00
|
|
|
auto bigLoops = loops * 100;
|
2018-10-02 20:48:17 +00:00
|
|
|
for (int i = 0; i < bigLoops; i++) {
|
2018-08-07 17:29:41 +00:00
|
|
|
// To ensure maximum caching, we just redraw the blob at the same place everytime
|
|
|
|
canvas->drawTextBlob(blob, 0, 0, paint);
|
2015-04-03 15:07:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-07 17:29:41 +00:00
|
|
|
};
|
2018-12-05 19:15:25 +00:00
|
|
|
DEF_BENCH( return new TextBlobCachedBench(); )
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2018-08-07 17:29:41 +00:00
|
|
|
class TextBlobFirstTimeBench : public SkTextBlobBench {
|
|
|
|
const char* onGetName() override {
|
|
|
|
return "TextBlobFirstTimeBench";
|
|
|
|
}
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2018-08-07 17:29:41 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
|
|
|
SkPaint paint;
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2018-10-08 18:06:09 +00:00
|
|
|
auto bigLoops = loops * 100;
|
2018-10-02 20:48:17 +00:00
|
|
|
for (int i = 0; i < bigLoops; i++) {
|
2018-08-07 17:29:41 +00:00
|
|
|
canvas->drawTextBlob(this->makeBlob(), 0, 0, paint);
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 15:07:26 +00:00
|
|
|
};
|
2018-08-07 17:29:41 +00:00
|
|
|
DEF_BENCH( return new TextBlobFirstTimeBench(); )
|
2018-12-05 19:15:25 +00:00
|
|
|
|
|
|
|
class TextBlobMakeBench : public SkTextBlobBench {
|
|
|
|
const char* onGetName() override {
|
|
|
|
return "TextBlobMakeBench";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSuitableFor(Backend backend) override {
|
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
|
|
|
for (int i = 0; i < loops; i++) {
|
|
|
|
for (int inner = 0; inner < 1000; ++inner) {
|
|
|
|
this->makeBlob();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DEF_BENCH( return new TextBlobMakeBench(); )
|