1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2013 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "bench/Benchmark.h"
9cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
10cb93a386Sopenharmony_ci#include "include/core/SkFont.h"
11cb93a386Sopenharmony_ci#include "include/core/SkTypeface.h"
12cb93a386Sopenharmony_ci#include "include/utils/SkRandom.h"
13cb93a386Sopenharmony_ci#include "src/utils/SkCharToGlyphCache.h"
14cb93a386Sopenharmony_ci#include "src/utils/SkUTF.h"
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_cienum {
17cb93a386Sopenharmony_ci    NGLYPHS = 100
18cb93a386Sopenharmony_ci};
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_cinamespace {
21cb93a386Sopenharmony_cistruct Rec {
22cb93a386Sopenharmony_ci    const SkCharToGlyphCache&   fCache;
23cb93a386Sopenharmony_ci    int                         fLoops;
24cb93a386Sopenharmony_ci    const SkFont&               fFont;
25cb93a386Sopenharmony_ci    const SkUnichar*            fText;
26cb93a386Sopenharmony_ci    int                         fCount;
27cb93a386Sopenharmony_ci};
28cb93a386Sopenharmony_ci}  // namespace
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_citypedef void (*TypefaceProc)(const Rec& r);
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_cistatic void textToGlyphs_proc(const Rec& r) {
33cb93a386Sopenharmony_ci    uint16_t glyphs[NGLYPHS];
34cb93a386Sopenharmony_ci    SkASSERT(r.fCount <= NGLYPHS);
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci    for (int i = 0; i < r.fLoops; ++i) {
37cb93a386Sopenharmony_ci        r.fFont.textToGlyphs(r.fText, r.fCount*4, SkTextEncoding::kUTF32, glyphs, NGLYPHS);
38cb93a386Sopenharmony_ci    }
39cb93a386Sopenharmony_ci}
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_cistatic void charsToGlyphs_proc(const Rec& r) {
42cb93a386Sopenharmony_ci    uint16_t glyphs[NGLYPHS];
43cb93a386Sopenharmony_ci    SkASSERT(r.fCount <= NGLYPHS);
44cb93a386Sopenharmony_ci
45cb93a386Sopenharmony_ci    SkTypeface* face = r.fFont.getTypefaceOrDefault();
46cb93a386Sopenharmony_ci    for (int i = 0; i < r.fLoops; ++i) {
47cb93a386Sopenharmony_ci        face->unicharsToGlyphs(r.fText, r.fCount, glyphs);
48cb93a386Sopenharmony_ci    }
49cb93a386Sopenharmony_ci}
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_cistatic void addcache_proc(const Rec& r) {
52cb93a386Sopenharmony_ci    for (int loop = 0; loop < r.fLoops; ++loop) {
53cb93a386Sopenharmony_ci        SkCharToGlyphCache cache;
54cb93a386Sopenharmony_ci        for (int i = 0; i < r.fCount; ++i) {
55cb93a386Sopenharmony_ci            cache.addCharAndGlyph(r.fText[i], i);
56cb93a386Sopenharmony_ci        }
57cb93a386Sopenharmony_ci    }
58cb93a386Sopenharmony_ci}
59cb93a386Sopenharmony_ci
60cb93a386Sopenharmony_cistatic void findcache_proc(const Rec& r) {
61cb93a386Sopenharmony_ci    for (int loop = 0; loop < r.fLoops; ++loop) {
62cb93a386Sopenharmony_ci        for (int i = 0; i < r.fCount; ++i) {
63cb93a386Sopenharmony_ci            r.fCache.findGlyphIndex(r.fText[i]);
64cb93a386Sopenharmony_ci        }
65cb93a386Sopenharmony_ci    }
66cb93a386Sopenharmony_ci}
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ciclass CMAPBench : public Benchmark {
69cb93a386Sopenharmony_ci    TypefaceProc fProc;
70cb93a386Sopenharmony_ci    SkString     fName;
71cb93a386Sopenharmony_ci    SkUnichar    fText[NGLYPHS];
72cb93a386Sopenharmony_ci    SkFont       fFont;
73cb93a386Sopenharmony_ci    SkCharToGlyphCache fCache;
74cb93a386Sopenharmony_ci    int          fCount;
75cb93a386Sopenharmony_ci
76cb93a386Sopenharmony_cipublic:
77cb93a386Sopenharmony_ci    CMAPBench(TypefaceProc proc, const char name[], int count) {
78cb93a386Sopenharmony_ci        SkASSERT(count <= NGLYPHS);
79cb93a386Sopenharmony_ci
80cb93a386Sopenharmony_ci        fProc = proc;
81cb93a386Sopenharmony_ci        fName.printf("%s_%d", name, count);
82cb93a386Sopenharmony_ci        fCount = count;
83cb93a386Sopenharmony_ci
84cb93a386Sopenharmony_ci        SkRandom rand;
85cb93a386Sopenharmony_ci        for (int i = 0; i < count; ++i) {
86cb93a386Sopenharmony_ci            fText[i] = rand.nextU() & 0xFFFF;
87cb93a386Sopenharmony_ci            fCache.addCharAndGlyph(fText[i], i);
88cb93a386Sopenharmony_ci        }
89cb93a386Sopenharmony_ci        fFont.setTypeface(SkTypeface::MakeDefault());
90cb93a386Sopenharmony_ci    }
91cb93a386Sopenharmony_ci
92cb93a386Sopenharmony_ci    bool isSuitableFor(Backend backend) override {
93cb93a386Sopenharmony_ci        return backend == kNonRendering_Backend;
94cb93a386Sopenharmony_ci    }
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_ciprotected:
97cb93a386Sopenharmony_ci    const char* onGetName() override {
98cb93a386Sopenharmony_ci        return fName.c_str();
99cb93a386Sopenharmony_ci    }
100cb93a386Sopenharmony_ci
101cb93a386Sopenharmony_ci    void onDraw(int loops, SkCanvas* canvas) override {
102cb93a386Sopenharmony_ci        fProc({fCache, loops, fFont, fText, fCount});
103cb93a386Sopenharmony_ci    }
104cb93a386Sopenharmony_ci
105cb93a386Sopenharmony_ciprivate:
106cb93a386Sopenharmony_ci
107cb93a386Sopenharmony_ci    using INHERITED = Benchmark;
108cb93a386Sopenharmony_ci};
109cb93a386Sopenharmony_ci
110cb93a386Sopenharmony_ci//////////////////////////////////////////////////////////////////////////////
111cb93a386Sopenharmony_ci
112cb93a386Sopenharmony_ciconstexpr int SMALL = 10;
113cb93a386Sopenharmony_ci
114cb93a386Sopenharmony_ciDEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", SMALL); )
115cb93a386Sopenharmony_ciDEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", SMALL); )
116cb93a386Sopenharmony_ciDEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", SMALL); )
117cb93a386Sopenharmony_ciDEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", SMALL); )
118cb93a386Sopenharmony_ci
119cb93a386Sopenharmony_ciconstexpr int BIG = 100;
120cb93a386Sopenharmony_ci
121cb93a386Sopenharmony_ciDEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", BIG); )
122cb93a386Sopenharmony_ciDEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", BIG); )
123cb93a386Sopenharmony_ciDEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", BIG); )
124cb93a386Sopenharmony_ciDEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", BIG); )
125