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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkTypeface.h" |
| 12 | |
| 13 | enum { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 14 | NGLYPHS = 100 |
| 15 | }; |
| 16 | |
| 17 | static SkTypeface::Encoding paint2Encoding(const SkPaint& paint) { |
| 18 | SkPaint::TextEncoding enc = paint.getTextEncoding(); |
| 19 | SkASSERT(SkPaint::kGlyphID_TextEncoding != enc); |
| 20 | return (SkTypeface::Encoding)enc; |
| 21 | } |
| 22 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 23 | typedef void (*TypefaceProc)(int loops, const SkPaint&, const void* text, size_t len, |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 24 | int glyphCount); |
| 25 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 26 | static void containsText_proc(int loops, const SkPaint& paint, const void* text, size_t len, |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 27 | int glyphCount) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 28 | for (int i = 0; i < loops; ++i) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 29 | paint.containsText(text, len); |
| 30 | } |
| 31 | } |
| 32 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 33 | static void textToGlyphs_proc(int loops, const SkPaint& paint, const void* text, size_t len, |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 34 | int glyphCount) { |
| 35 | uint16_t glyphs[NGLYPHS]; |
| 36 | SkASSERT(glyphCount <= NGLYPHS); |
| 37 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 38 | for (int i = 0; i < loops; ++i) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 39 | paint.textToGlyphs(text, len, glyphs); |
| 40 | } |
| 41 | } |
| 42 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 43 | static void charsToGlyphs_proc(int loops, const SkPaint& paint, const void* text, |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 44 | size_t len, int glyphCount) { |
| 45 | SkTypeface::Encoding encoding = paint2Encoding(paint); |
| 46 | uint16_t glyphs[NGLYPHS]; |
| 47 | SkASSERT(glyphCount <= NGLYPHS); |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 48 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 49 | SkTypeface* face = paint.getTypeface(); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 50 | for (int i = 0; i < loops; ++i) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 51 | face->charsToGlyphs(text, encoding, glyphs, glyphCount); |
| 52 | } |
| 53 | } |
| 54 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 55 | static void charsToGlyphsNull_proc(int loops, const SkPaint& paint, const void* text, |
reed@google.com | 2311297 | 2013-07-02 16:28:53 +0000 | [diff] [blame] | 56 | size_t len, int glyphCount) { |
| 57 | SkTypeface::Encoding encoding = paint2Encoding(paint); |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 58 | |
reed@google.com | 2311297 | 2013-07-02 16:28:53 +0000 | [diff] [blame] | 59 | SkTypeface* face = paint.getTypeface(); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 60 | for (int i = 0; i < loops; ++i) { |
reed@google.com | 2311297 | 2013-07-02 16:28:53 +0000 | [diff] [blame] | 61 | face->charsToGlyphs(text, encoding, NULL, glyphCount); |
| 62 | } |
| 63 | } |
| 64 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 65 | class CMAPBench : public Benchmark { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 66 | TypefaceProc fProc; |
| 67 | SkString fName; |
| 68 | char fText[NGLYPHS]; |
| 69 | SkPaint fPaint; |
| 70 | |
| 71 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 72 | CMAPBench(TypefaceProc proc, const char name[]) { |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 73 | fProc = proc; |
| 74 | fName.printf("cmap_%s", name); |
skia.committer@gmail.com | 98a1967 | 2013-07-03 07:00:57 +0000 | [diff] [blame] | 75 | |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 76 | for (int i = 0; i < NGLYPHS; ++i) { |
reed@google.com | 2d903ae | 2013-07-02 20:06:00 +0000 | [diff] [blame] | 77 | // we're jamming values into utf8, so we must keep it legal utf8 |
| 78 | fText[i] = 'A' + (i & 31); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 79 | } |
| 80 | fPaint.setTypeface(SkTypeface::RefDefault())->unref(); |
| 81 | } |
| 82 | |
| 83 | protected: |
| 84 | virtual const char* onGetName() SK_OVERRIDE { |
| 85 | return fName.c_str(); |
| 86 | } |
| 87 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 88 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| 89 | fProc(loops, fPaint, fText, sizeof(fText), NGLYPHS); |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private: |
| 93 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 94 | typedef Benchmark INHERITED; |
reed@google.com | bcb42ae | 2013-07-02 13:56:39 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | ////////////////////////////////////////////////////////////////////////////// |
| 98 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 99 | DEF_BENCH( return new CMAPBench(containsText_proc, "paint_containsText"); ) |
| 100 | DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "paint_textToGlyphs"); ) |
| 101 | DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charsToGlyphs"); ) |
| 102 | DEF_BENCH( return new CMAPBench(charsToGlyphsNull_proc, "face_charsToGlyphs_null"); ) |