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