• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
8 #include "modules/skparagraph/include/FontCollection.h"
9 #include "modules/skparagraph/include/Paragraph.h"
10 #include "modules/skparagraph/src/ParagraphBuilderImpl.h"
11 #include "modules/skparagraph/src/ParagraphImpl.h"
12 #include "tools/Resources.h"
13 
14 #include <cfloat>
15 #include "include/core/SkPictureRecorder.h"
16 #include "modules/skparagraph/utils/TestFontCollection.h"
17 
18 using namespace skia::textlayout;
19 namespace {
20 struct ParagraphBench : public Benchmark {
ParagraphBench__anonc4a8669a0111::ParagraphBench21     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;
onGetName__anonc4a8669a0111::ParagraphBench27     const char* onGetName() override { return fName; }
isSuitableFor__anonc4a8669a0111::ParagraphBench28     bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
onDelayedSetup__anonc4a8669a0111::ParagraphBench29     void onDelayedSetup() override { fData = GetResourceAsData(fResource); }
onDraw__anonc4a8669a0111::ParagraphBench30     void onDraw(int loops, SkCanvas*) override {
31         if (!fData) {
32             return;
33         }
34 
35         const char* text = (const char*)fData->data();
36 
37         auto fontCollection = sk_make_sp<FontCollection>();
38         fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
39         ParagraphStyle paragraph_style;
40         paragraph_style.turnHintingOff();
41         ParagraphBuilderImpl builder(paragraph_style, fontCollection);
42         builder.addText(text);
43         auto paragraph = builder.Build();
44 
45         SkPictureRecorder rec;
46         SkCanvas* canvas = rec.beginRecording({0,0, 2000,3000});
47         while (loops-- > 0) {
48             paragraph->layout(fWidth);
49             auto impl = static_cast<ParagraphImpl*>(paragraph.get());
50             paragraph->paint(canvas, 0, 0);
51             paragraph->markDirty();
52             impl->resetCache();
53         }
54     }
55 };
56 }  // namespace
57 
58 #define PARAGRAPH_BENCH(X) DEF_BENCH(return new ParagraphBench(50000, "text/" #X ".txt", "paragraph_" #X);)
59 //PARAGRAPH_BENCH(arabic)
60 //PARAGRAPH_BENCH(emoji)
61 PARAGRAPH_BENCH(english)
62 #undef PARAGRAPH_BENCH
63 
64 #endif  // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)
65