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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Benchmark.h"
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
2018-11-21 20:10:08 +00:00
|
|
|
#include "SkFont.h"
|
2015-04-03 15:07:26 +00:00
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkTemplates.h"
|
|
|
|
#include "SkTextBlob.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
#include "sk_tool_utils.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 {
|
2018-11-21 20:10:08 +00:00
|
|
|
fFont.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle()));
|
|
|
|
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
|
|
|
|
2018-11-21 20:10:08 +00:00
|
|
|
fGlyphs.setCount(fFont.countText(text, strlen(text), kUTF8_SkTextEncoding));
|
|
|
|
fFont.textToGlyphs(text, strlen(text), kUTF8_SkTextEncoding, fGlyphs.begin(), fGlyphs.count());
|
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));
|
|
|
|
fFont.getXPos(&fGlyphs[0], fGlyphs.count(), run.pos);
|
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-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
|
|
|
};
|
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 TextBlobCachedBench(); )
|
|
|
|
DEF_BENCH( return new TextBlobFirstTimeBench(); )
|