blob: 35e4300f90ffe4a09f2849d5fd67f032a82d7858 [file] [log] [blame]
Julia Lavrovaa3552c52019-05-30 16:12:56 -04001// Copyright 2019 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4#include "bench/Benchmark.h"
5
6#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)
7
Greg Danielf91aeb22019-06-18 09:58:02 -04008#include "modules/skparagraph/include/FontCollection.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -04009#include "modules/skparagraph/include/Paragraph.h"
10#include "modules/skparagraph/src/ParagraphBuilderImpl.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -040011#include "modules/skparagraph/src/ParagraphImpl.h"
12#include "tools/Resources.h"
13
14#include <cfloat>
Julia Lavrova5207f352019-06-21 12:22:32 -040015#include "include/core/SkPictureRecorder.h"
Mike Klein52337de2019-07-25 09:00:52 -050016#include "modules/skparagraph/utils/TestFontCollection.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -040017
18using namespace skia::textlayout;
19namespace {
20struct ParagraphBench : public Benchmark {
21 ParagraphBench(SkScalar width, const char* r, const char* n)
22 : fResource(r), fName(n), fWidth(width) {}
23 sk_sp<SkData> fData;
24 const char* fResource;
25 const char* fName;
26 SkScalar fWidth;
27 const char* onGetName() override { return fName; }
28 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
29 void onDelayedSetup() override { fData = GetResourceAsData(fResource); }
30 void onDraw(int loops, SkCanvas*) override {
31 if (!fData) {
32 return;
33 }
34
35 const char* text = (const char*)fData->data();
36
Julia Lavrova5207f352019-06-21 12:22:32 -040037 auto fontCollection = sk_make_sp<FontCollection>();
38 fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
Julia Lavrovaa3552c52019-05-30 16:12:56 -040039 ParagraphStyle paragraph_style;
40 paragraph_style.turnHintingOff();
41 ParagraphBuilderImpl builder(paragraph_style, fontCollection);
42 builder.addText(text);
43 auto paragraph = builder.Build();
44
Julia Lavrova5207f352019-06-21 12:22:32 -040045 SkPictureRecorder rec;
46 SkCanvas* canvas = rec.beginRecording({0,0, 2000,3000});
Julia Lavrovaa3552c52019-05-30 16:12:56 -040047 while (loops-- > 0) {
48 paragraph->layout(fWidth);
49 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
Julia Lavrova5207f352019-06-21 12:22:32 -040050 paragraph->paint(canvas, 0, 0);
51 paragraph->markDirty();
52 impl->resetCache();
Julia Lavrovaa3552c52019-05-30 16:12:56 -040053 }
54 }
55};
56} // namespace
57
Julia Lavrova5207f352019-06-21 12:22:32 -040058#define PARAGRAPH_BENCH(X) DEF_BENCH(return new ParagraphBench(50000, "text/" #X ".txt", "paragraph_" #X);)
Julia Lavrovaa3552c52019-05-30 16:12:56 -040059//PARAGRAPH_BENCH(arabic)
60//PARAGRAPH_BENCH(emoji)
61PARAGRAPH_BENCH(english)
62#undef PARAGRAPH_BENCH
63
64#endif // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)