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
8 #include "Benchmark.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "SkTypeface.h"
12
13 enum {
14 NGLYPHS = 100
15 };
16
paint2Encoding(const SkPaint & paint)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
23 typedef void (*TypefaceProc)(int loops, const SkPaint&, const void* text, size_t len,
24 int glyphCount);
25
containsText_proc(int loops,const SkPaint & paint,const void * text,size_t len,int glyphCount)26 static void containsText_proc(int loops, const SkPaint& paint, const void* text, size_t len,
27 int glyphCount) {
28 for (int i = 0; i < loops; ++i) {
29 paint.containsText(text, len);
30 }
31 }
32
textToGlyphs_proc(int loops,const SkPaint & paint,const void * text,size_t len,int glyphCount)33 static void textToGlyphs_proc(int loops, const SkPaint& paint, const void* text, size_t len,
34 int glyphCount) {
35 uint16_t glyphs[NGLYPHS];
36 SkASSERT(glyphCount <= NGLYPHS);
37
38 for (int i = 0; i < loops; ++i) {
39 paint.textToGlyphs(text, len, glyphs);
40 }
41 }
42
charsToGlyphs_proc(int loops,const SkPaint & paint,const void * text,size_t len,int glyphCount)43 static void charsToGlyphs_proc(int loops, const SkPaint& paint, const void* text,
44 size_t len, int glyphCount) {
45 SkTypeface::Encoding encoding = paint2Encoding(paint);
46 uint16_t glyphs[NGLYPHS];
47 SkASSERT(glyphCount <= NGLYPHS);
48
49 SkTypeface* face = paint.getTypeface();
50 for (int i = 0; i < loops; ++i) {
51 face->charsToGlyphs(text, encoding, glyphs, glyphCount);
52 }
53 }
54
charsToGlyphsNull_proc(int loops,const SkPaint & paint,const void * text,size_t len,int glyphCount)55 static void charsToGlyphsNull_proc(int loops, const SkPaint& paint, const void* text,
56 size_t len, int glyphCount) {
57 SkTypeface::Encoding encoding = paint2Encoding(paint);
58
59 SkTypeface* face = paint.getTypeface();
60 for (int i = 0; i < loops; ++i) {
61 face->charsToGlyphs(text, encoding, NULL, glyphCount);
62 }
63 }
64
65 class CMAPBench : public Benchmark {
66 TypefaceProc fProc;
67 SkString fName;
68 char fText[NGLYPHS];
69 SkPaint fPaint;
70
71 public:
CMAPBench(TypefaceProc proc,const char name[])72 CMAPBench(TypefaceProc proc, const char name[]) {
73 fProc = proc;
74 fName.printf("cmap_%s", name);
75
76 for (int i = 0; i < NGLYPHS; ++i) {
77 // we're jamming values into utf8, so we must keep it legal utf8
78 fText[i] = 'A' + (i & 31);
79 }
80 fPaint.setTypeface(SkTypeface::RefDefault())->unref();
81 }
82
83 protected:
onGetName()84 const char* onGetName() override {
85 return fName.c_str();
86 }
87
onDraw(const int loops,SkCanvas * canvas)88 void onDraw(const int loops, SkCanvas* canvas) override {
89 fProc(loops, fPaint, fText, sizeof(fText), NGLYPHS);
90 }
91
92 private:
93
94 typedef Benchmark INHERITED;
95 };
96
97 //////////////////////////////////////////////////////////////////////////////
98
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"); )
103