1cb93a386Sopenharmony_ci// Copyright 2019 Google LLC.
2cb93a386Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3cb93a386Sopenharmony_ci
4cb93a386Sopenharmony_ci#include "bench/Benchmark.h"
5cb93a386Sopenharmony_ci
6cb93a386Sopenharmony_ci#if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "modules/skparagraph/include/FontCollection.h"
9cb93a386Sopenharmony_ci#include "modules/skparagraph/include/Paragraph.h"
10cb93a386Sopenharmony_ci#include "modules/skparagraph/src/ParagraphBuilderImpl.h"
11cb93a386Sopenharmony_ci#include "modules/skparagraph/src/ParagraphImpl.h"
12cb93a386Sopenharmony_ci#include "tools/Resources.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci#include <cfloat>
15cb93a386Sopenharmony_ci#include "include/core/SkPictureRecorder.h"
16cb93a386Sopenharmony_ci#include "modules/skparagraph/utils/TestFontCollection.h"
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ciusing namespace skia::textlayout;
19cb93a386Sopenharmony_cinamespace {
20cb93a386Sopenharmony_cistruct ParagraphBench : public Benchmark {
21cb93a386Sopenharmony_ci    ParagraphBench(SkScalar width, const char* r, const char* n)
22cb93a386Sopenharmony_ci            : fResource(r), fName(n), fWidth(width) {}
23cb93a386Sopenharmony_ci    sk_sp<SkData> fData;
24cb93a386Sopenharmony_ci    const char* fResource;
25cb93a386Sopenharmony_ci    const char* fName;
26cb93a386Sopenharmony_ci    SkScalar fWidth;
27cb93a386Sopenharmony_ci    const char* onGetName() override { return fName; }
28cb93a386Sopenharmony_ci    bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
29cb93a386Sopenharmony_ci    void onDelayedSetup() override { fData = GetResourceAsData(fResource); }
30cb93a386Sopenharmony_ci    void onDraw(int loops, SkCanvas*) override {
31cb93a386Sopenharmony_ci        if (!fData) {
32cb93a386Sopenharmony_ci            return;
33cb93a386Sopenharmony_ci        }
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci        const char* text = (const char*)fData->data();
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci        auto fontCollection = sk_make_sp<FontCollection>();
38cb93a386Sopenharmony_ci        fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
39cb93a386Sopenharmony_ci        ParagraphStyle paragraph_style;
40cb93a386Sopenharmony_ci        paragraph_style.turnHintingOff();
41cb93a386Sopenharmony_ci        ParagraphBuilderImpl builder(paragraph_style, fontCollection);
42cb93a386Sopenharmony_ci        builder.addText(text);
43cb93a386Sopenharmony_ci        auto paragraph = builder.Build();
44cb93a386Sopenharmony_ci
45cb93a386Sopenharmony_ci        SkPictureRecorder rec;
46cb93a386Sopenharmony_ci        SkCanvas* canvas = rec.beginRecording({0,0, 2000,3000});
47cb93a386Sopenharmony_ci        while (loops-- > 0) {
48cb93a386Sopenharmony_ci            paragraph->layout(fWidth);
49cb93a386Sopenharmony_ci            auto impl = static_cast<ParagraphImpl*>(paragraph.get());
50cb93a386Sopenharmony_ci            paragraph->paint(canvas, 0, 0);
51cb93a386Sopenharmony_ci            paragraph->markDirty();
52cb93a386Sopenharmony_ci            impl->resetCache();
53cb93a386Sopenharmony_ci        }
54cb93a386Sopenharmony_ci    }
55cb93a386Sopenharmony_ci};
56cb93a386Sopenharmony_ci}  // namespace
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci#define PARAGRAPH_BENCH(X) DEF_BENCH(return new ParagraphBench(50000, "text/" #X ".txt", "paragraph_" #X);)
59cb93a386Sopenharmony_ci//PARAGRAPH_BENCH(arabic)
60cb93a386Sopenharmony_ci//PARAGRAPH_BENCH(emoji)
61cb93a386Sopenharmony_ciPARAGRAPH_BENCH(english)
62cb93a386Sopenharmony_ci#undef PARAGRAPH_BENCH
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci#endif  // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3)
65