reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 15 | |
| 16 | enum { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 17 | NGLYPHS = 100 |
| 18 | }; |
| 19 | |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 20 | namespace { |
| 21 | struct Rec { |
| 22 | const SkCharToGlyphCache& fCache; |
| 23 | int fLoops; |
| 24 | const SkFont& fFont; |
| 25 | const SkUnichar* fText; |
| 26 | int fCount; |
| 27 | }; |
| 28 | } |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 29 | |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 30 | typedef void (*TypefaceProc)(const Rec& r); |
| 31 | |
| 32 | static void textToGlyphs_proc(const Rec& r) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 33 | uint16_t glyphs[NGLYPHS]; |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 34 | SkASSERT(r.fCount <= NGLYPHS); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 35 | |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 36 | for (int i = 0; i < r.fLoops; ++i) { |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 37 | r.fFont.textToGlyphs(r.fText, r.fCount*4, SkTextEncoding::kUTF32, glyphs, NGLYPHS); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 41 | static void charsToGlyphs_proc(const Rec& r) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 42 | uint16_t glyphs[NGLYPHS]; |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 43 | SkASSERT(r.fCount <= NGLYPHS); |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 44 | |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 45 | SkTypeface* face = r.fFont.getTypefaceOrDefault(); |
| 46 | for (int i = 0; i < r.fLoops; ++i) { |
Mike Reed | 64670cb | 2019-04-16 11:37:38 -0700 | [diff] [blame] | 47 | face->unicharsToGlyphs(r.fText, r.fCount, glyphs); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 51 | static 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.com | 2311297 | 2013-07-02 16:28:53 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 60 | static 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 68 | class CMAPBench : public Benchmark { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 69 | TypefaceProc fProc; |
| 70 | SkString fName; |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 71 | SkUnichar fText[NGLYPHS]; |
Mike Reed | 358fcad | 2018-11-23 15:27:51 -0500 | [diff] [blame] | 72 | SkFont fFont; |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 73 | SkCharToGlyphCache fCache; |
Mike Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 74 | int fCount; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 75 | |
| 76 | public: |
Mike Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 77 | CMAPBench(TypefaceProc proc, const char name[], int count) { |
| 78 | SkASSERT(count <= NGLYPHS); |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 79 | |
Mike Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 80 | 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 Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 87 | fCache.addCharAndGlyph(fText[i], i); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 88 | } |
Mike Reed | 358fcad | 2018-11-23 15:27:51 -0500 | [diff] [blame] | 89 | fFont.setTypeface(SkTypeface::MakeDefault()); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 92 | bool isSuitableFor(Backend backend) override { |
| 93 | return backend == kNonRendering_Backend; |
| 94 | } |
| 95 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 96 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 97 | const char* onGetName() override { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 98 | return fName.c_str(); |
| 99 | } |
| 100 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 101 | void onDraw(int loops, SkCanvas* canvas) override { |
Mike Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 102 | fProc({fCache, loops, fFont, fText, fCount}); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private: |
| 106 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 107 | typedef Benchmark INHERITED; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
Mike Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 112 | constexpr int SMALL = 10; |
| 113 | |
| 114 | DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", SMALL); ) |
| 115 | DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", SMALL); ) |
| 116 | DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", SMALL); ) |
| 117 | DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", SMALL); ) |
| 118 | |
| 119 | constexpr int BIG = 100; |
| 120 | |
| 121 | DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "font_charToGlyph", BIG); ) |
| 122 | DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charToGlyph", BIG); ) |
| 123 | DEF_BENCH( return new CMAPBench(addcache_proc, "addcache_charToGlyph", BIG); ) |
| 124 | DEF_BENCH( return new CMAPBench(findcache_proc, "findcache_charToGlyph", BIG); ) |