| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBenchmark.h" | 8 #include "SkBenchmark.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkPaint.h" | 10 #include "SkPaint.h" |
| 11 #include "SkTypeface.h" | 11 #include "SkTypeface.h" |
| 12 | 12 |
| 13 enum { | 13 enum { |
| 14 LOOP = SkBENCHLOOP(1000), | |
| 15 NGLYPHS = 100 | 14 NGLYPHS = 100 |
| 16 }; | 15 }; |
| 17 | 16 |
| 18 static SkTypeface::Encoding paint2Encoding(const SkPaint& paint) { | 17 static SkTypeface::Encoding paint2Encoding(const SkPaint& paint) { |
| 19 SkPaint::TextEncoding enc = paint.getTextEncoding(); | 18 SkPaint::TextEncoding enc = paint.getTextEncoding(); |
| 20 SkASSERT(SkPaint::kGlyphID_TextEncoding != enc); | 19 SkASSERT(SkPaint::kGlyphID_TextEncoding != enc); |
| 21 return (SkTypeface::Encoding)enc; | 20 return (SkTypeface::Encoding)enc; |
| 22 } | 21 } |
| 23 | 22 |
| 24 typedef void (*TypefaceProc)(const SkPaint&, const void* text, size_t len, | 23 typedef void (*TypefaceProc)(int loops, const SkPaint&, const void* text, size_t
len, |
| 25 int glyphCount); | 24 int glyphCount); |
| 26 | 25 |
| 27 static void containsText_proc(const SkPaint& paint, const void* text, size_t len
, | 26 static void containsText_proc(int loops, const SkPaint& paint, const void* text,
size_t len, |
| 28 int glyphCount) { | 27 int glyphCount) { |
| 29 for (int i = 0; i < LOOP; ++i) { | 28 for (int i = 0; i < loops; ++i) { |
| 30 paint.containsText(text, len); | 29 paint.containsText(text, len); |
| 31 } | 30 } |
| 32 } | 31 } |
| 33 | 32 |
| 34 static void textToGlyphs_proc(const SkPaint& paint, const void* text, size_t len
, | 33 static void textToGlyphs_proc(int loops, const SkPaint& paint, const void* text,
size_t len, |
| 35 int glyphCount) { | 34 int glyphCount) { |
| 36 uint16_t glyphs[NGLYPHS]; | 35 uint16_t glyphs[NGLYPHS]; |
| 37 SkASSERT(glyphCount <= NGLYPHS); | 36 SkASSERT(glyphCount <= NGLYPHS); |
| 38 | 37 |
| 39 for (int i = 0; i < LOOP; ++i) { | 38 for (int i = 0; i < loops; ++i) { |
| 40 paint.textToGlyphs(text, len, glyphs); | 39 paint.textToGlyphs(text, len, glyphs); |
| 41 } | 40 } |
| 42 } | 41 } |
| 43 | 42 |
| 44 static void charsToGlyphs_proc(const SkPaint& paint, const void* text, | 43 static void charsToGlyphs_proc(int loops, const SkPaint& paint, const void* text
, |
| 45 size_t len, int glyphCount) { | 44 size_t len, int glyphCount) { |
| 46 SkTypeface::Encoding encoding = paint2Encoding(paint); | 45 SkTypeface::Encoding encoding = paint2Encoding(paint); |
| 47 uint16_t glyphs[NGLYPHS]; | 46 uint16_t glyphs[NGLYPHS]; |
| 48 SkASSERT(glyphCount <= NGLYPHS); | 47 SkASSERT(glyphCount <= NGLYPHS); |
| 49 | 48 |
| 50 SkTypeface* face = paint.getTypeface(); | 49 SkTypeface* face = paint.getTypeface(); |
| 51 for (int i = 0; i < LOOP; ++i) { | 50 for (int i = 0; i < loops; ++i) { |
| 52 face->charsToGlyphs(text, encoding, glyphs, glyphCount); | 51 face->charsToGlyphs(text, encoding, glyphs, glyphCount); |
| 53 } | 52 } |
| 54 } | 53 } |
| 55 | 54 |
| 56 static void charsToGlyphsNull_proc(const SkPaint& paint, const void* text, | 55 static void charsToGlyphsNull_proc(int loops, const SkPaint& paint, const void*
text, |
| 57 size_t len, int glyphCount) { | 56 size_t len, int glyphCount) { |
| 58 SkTypeface::Encoding encoding = paint2Encoding(paint); | 57 SkTypeface::Encoding encoding = paint2Encoding(paint); |
| 59 | 58 |
| 60 SkTypeface* face = paint.getTypeface(); | 59 SkTypeface* face = paint.getTypeface(); |
| 61 for (int i = 0; i < LOOP; ++i) { | 60 for (int i = 0; i < loops; ++i) { |
| 62 face->charsToGlyphs(text, encoding, NULL, glyphCount); | 61 face->charsToGlyphs(text, encoding, NULL, glyphCount); |
| 63 } | 62 } |
| 64 } | 63 } |
| 65 | 64 |
| 66 class CMAPBench : public SkBenchmark { | 65 class CMAPBench : public SkBenchmark { |
| 67 TypefaceProc fProc; | 66 TypefaceProc fProc; |
| 68 SkString fName; | 67 SkString fName; |
| 69 char fText[NGLYPHS]; | 68 char fText[NGLYPHS]; |
| 70 SkPaint fPaint; | 69 SkPaint fPaint; |
| 71 | 70 |
| 72 public: | 71 public: |
| 73 CMAPBench(void* param, TypefaceProc proc, const char name[]) : SkBenchmark(p
aram) { | 72 CMAPBench(void* param, TypefaceProc proc, const char name[]) : SkBenchmark(p
aram) { |
| 74 fProc = proc; | 73 fProc = proc; |
| 75 fName.printf("cmap_%s", name); | 74 fName.printf("cmap_%s", name); |
| 76 | 75 |
| 77 for (int i = 0; i < NGLYPHS; ++i) { | 76 for (int i = 0; i < NGLYPHS; ++i) { |
| 78 // we're jamming values into utf8, so we must keep it legal utf8 | 77 // we're jamming values into utf8, so we must keep it legal utf8 |
| 79 fText[i] = 'A' + (i & 31); | 78 fText[i] = 'A' + (i & 31); |
| 80 } | 79 } |
| 81 fPaint.setTypeface(SkTypeface::RefDefault())->unref(); | 80 fPaint.setTypeface(SkTypeface::RefDefault())->unref(); |
| 82 } | 81 } |
| 83 | 82 |
| 84 protected: | 83 protected: |
| 85 virtual const char* onGetName() SK_OVERRIDE { | 84 virtual const char* onGetName() SK_OVERRIDE { |
| 86 return fName.c_str(); | 85 return fName.c_str(); |
| 87 } | 86 } |
| 88 | 87 |
| 89 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 88 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 90 fProc(fPaint, fText, sizeof(fText), NGLYPHS); | 89 fProc(this->getLoops(), fPaint, fText, sizeof(fText), NGLYPHS); |
| 91 } | 90 } |
| 92 | 91 |
| 93 private: | 92 private: |
| 94 | 93 |
| 95 typedef SkBenchmark INHERITED; | 94 typedef SkBenchmark INHERITED; |
| 96 }; | 95 }; |
| 97 | 96 |
| 98 ////////////////////////////////////////////////////////////////////////////// | 97 ////////////////////////////////////////////////////////////////////////////// |
| 99 | 98 |
| 100 DEF_BENCH( return new CMAPBench(p, containsText_proc, "paint_containsText"); ) | 99 DEF_BENCH( return new CMAPBench(p, containsText_proc, "paint_containsText"); ) |
| 101 DEF_BENCH( return new CMAPBench(p, textToGlyphs_proc, "paint_textToGlyphs"); ) | 100 DEF_BENCH( return new CMAPBench(p, textToGlyphs_proc, "paint_textToGlyphs"); ) |
| 102 DEF_BENCH( return new CMAPBench(p, charsToGlyphs_proc, "face_charsToGlyphs"); ) | 101 DEF_BENCH( return new CMAPBench(p, charsToGlyphs_proc, "face_charsToGlyphs"); ) |
| 103 DEF_BENCH( return new CMAPBench(p, charsToGlyphsNull_proc, "face_charsToGlyphs_n
ull"); ) | 102 DEF_BENCH( return new CMAPBench(p, charsToGlyphsNull_proc, "face_charsToGlyphs_n
ull"); ) |
| OLD | NEW |