blob: 7988165e688b77a2978212013ac91305d1a8f3d5 [file] [log] [blame]
reed@google.combcb42ae2013-07-02 13:56:39 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkFont.h"
11#include "include/core/SkTypeface.h"
12#include "include/utils/SkRandom.h"
13#include "src/utils/SkCharToGlyphCache.h"
14#include "src/utils/SkUTF.h"
reed@google.combcb42ae2013-07-02 13:56:39 +000015
16enum {
reed@google.combcb42ae2013-07-02 13:56:39 +000017 NGLYPHS = 100
18};
19
Mike Reed0c607082019-04-11 17:10:17 -040020namespace {
21struct Rec {
22 const SkCharToGlyphCache& fCache;
23 int fLoops;
24 const SkFont& fFont;
25 const SkUnichar* fText;
26 int fCount;
27};
28}
reed@google.combcb42ae2013-07-02 13:56:39 +000029
Mike Reed0c607082019-04-11 17:10:17 -040030typedef void (*TypefaceProc)(const Rec& r);
31
32static void textToGlyphs_proc(const Rec& r) {
reed@google.combcb42ae2013-07-02 13:56:39 +000033 uint16_t glyphs[NGLYPHS];
Mike Reed0c607082019-04-11 17:10:17 -040034 SkASSERT(r.fCount <= NGLYPHS);
reed@google.combcb42ae2013-07-02 13:56:39 +000035
Mike Reed0c607082019-04-11 17:10:17 -040036 for (int i = 0; i < r.fLoops; ++i) {
Ben Wagner51e15a62019-05-07 15:38:46 -040037 r.fFont.textToGlyphs(r.fText, r.fCount*4, SkTextEncoding::kUTF32, glyphs, NGLYPHS);
reed@google.combcb42ae2013-07-02 13:56:39 +000038 }
39}
40
Mike Reed0c607082019-04-11 17:10:17 -040041static void charsToGlyphs_proc(const Rec& r) {
reed@google.combcb42ae2013-07-02 13:56:39 +000042 uint16_t glyphs[NGLYPHS];
Mike Reed0c607082019-04-11 17:10:17 -040043 SkASSERT(r.fCount <= NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000044
Mike Reed0c607082019-04-11 17:10:17 -040045 SkTypeface* face = r.fFont.getTypefaceOrDefault();
46 for (int i = 0; i < r.fLoops; ++i) {
Mike Reed64670cb2019-04-16 11:37:38 -070047 face->unicharsToGlyphs(r.fText, r.fCount, glyphs);
reed@google.combcb42ae2013-07-02 13:56:39 +000048 }
49}
50
Mike Reed0c607082019-04-11 17:10:17 -040051static void addcache_proc(const Rec& r) {
52 for (int i = 0; i < r.fLoops; ++i) {
53 SkCharToGlyphCache cache;
54 for (int i = 0; i < r.fCount; ++i) {
55 cache.addCharAndGlyph(r.fText[i], i);
56 }
reed@google.com23112972013-07-02 16:28:53 +000057 }
58}
59
Mike Reed0c607082019-04-11 17:10:17 -040060static void findcache_proc(const Rec& r) {
61 for (int i = 0; i < r.fLoops; ++i) {
62 for (int i = 0; i < r.fCount; ++i) {
63 r.fCache.findGlyphIndex(r.fText[i]);
64 }
65 }
66}
67
tfarinaf168b862014-06-19 12:32:29 -070068class CMAPBench : public Benchmark {
reed@google.combcb42ae2013-07-02 13:56:39 +000069 TypefaceProc fProc;
70 SkString fName;
Mike Reed0c607082019-04-11 17:10:17 -040071 SkUnichar fText[NGLYPHS];
Mike Reed358fcad2018-11-23 15:27:51 -050072 SkFont fFont;
Mike Reed0c607082019-04-11 17:10:17 -040073 SkCharToGlyphCache fCache;
Mike Reed194cab02019-04-15 12:07:19 -040074 int fCount;
reed@google.combcb42ae2013-07-02 13:56:39 +000075
76public:
Mike Reed194cab02019-04-15 12:07:19 -040077 CMAPBench(TypefaceProc proc, const char name[], int count) {
78 SkASSERT(count <= NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000079
Mike Reed194cab02019-04-15 12:07:19 -040080 fProc = proc;
81 fName.printf("%s_%d", name, count);
82 fCount = count;
83
84 SkRandom rand;
85 for (int i = 0; i < count; ++i) {
86 fText[i] = rand.nextU() & 0xFFFF;
Mike Reed0c607082019-04-11 17:10:17 -040087 fCache.addCharAndGlyph(fText[i], i);
reed@google.combcb42ae2013-07-02 13:56:39 +000088 }
Mike Reed358fcad2018-11-23 15:27:51 -050089 fFont.setTypeface(SkTypeface::MakeDefault());
reed@google.combcb42ae2013-07-02 13:56:39 +000090 }
91
Mike Reed0c607082019-04-11 17:10:17 -040092 bool isSuitableFor(Backend backend) override {
93 return backend == kNonRendering_Backend;
94 }
95
reed@google.combcb42ae2013-07-02 13:56:39 +000096protected:
mtklein36352bf2015-03-25 18:17:31 -070097 const char* onGetName() override {
reed@google.combcb42ae2013-07-02 13:56:39 +000098 return fName.c_str();
99 }
100
mtkleina1ebeb22015-10-01 09:43:39 -0700101 void onDraw(int loops, SkCanvas* canvas) override {
Mike Reed194cab02019-04-15 12:07:19 -0400102 fProc({fCache, loops, fFont, fText, fCount});
reed@google.combcb42ae2013-07-02 13:56:39 +0000103 }
104
105private:
106
tfarinaf168b862014-06-19 12:32:29 -0700107 typedef Benchmark INHERITED;
reed@google.combcb42ae2013-07-02 13:56:39 +0000108};
109
110//////////////////////////////////////////////////////////////////////////////
111
Mike Reed194cab02019-04-15 12:07:19 -0400112constexpr int SMALL = 10;
113
114DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", SMALL); )
115DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", SMALL); )
116DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", SMALL); )
117DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", SMALL); )
118
119constexpr int BIG = 100;
120
121DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", BIG); )
122DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", BIG); )
123DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", BIG); )
124DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", BIG); )