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"
|
|
|
|
#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-08-07 17:29:41 +00:00
|
|
|
fPaint.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle()));
|
|
|
|
fPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
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-08-07 17:29:41 +00:00
|
|
|
fGlyphs.setCount(fPaint.textToGlyphs(text, strlen(text), nullptr));
|
|
|
|
fPaint.textToGlyphs(text, strlen(text), fGlyphs.begin());
|
|
|
|
}
|
2015-04-03 15:07:26 +00:00
|
|
|
|
2018-08-07 17:29:41 +00:00
|
|
|
sk_sp<SkTextBlob> makeBlob() {
|
|
|
|
const SkTextBlobBuilder::RunBuffer& run =
|
|
|
|
fBuilder.allocRunPosH(fPaint, fGlyphs.count(), 10, nullptr);
|
|
|
|
for (int i = 0; i < fGlyphs.count(); i++) {
|
|
|
|
run.glyphs[i] = fGlyphs[i];
|
|
|
|
run. pos[i] = (i+1) * 10;
|
|
|
|
}
|
|
|
|
return fBuilder.make();
|
2015-04-03 15:07:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 17:29:41 +00:00
|
|
|
private:
|
|
|
|
SkTextBlobBuilder fBuilder;
|
|
|
|
SkPaint fPaint;
|
|
|
|
SkTDArray<uint16_t> fGlyphs;
|
|
|
|
|
|
|
|
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();
|
2015-04-03 15:07:26 +00:00
|
|
|
for (int i = 0; i < loops; 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-08-07 17:29:41 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
|
|
|
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(); )
|