blob: 9697477850e81a96586338c74eb4b295e6be4830 [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
56class CMAPBench : public SkBenchmark {
57 TypefaceProc fProc;
58 SkString fName;
59 char fText[NGLYPHS];
60 SkPaint fPaint;
61
62public:
63 CMAPBench(void* param, TypefaceProc proc, const char name[]) : SkBenchmark(param) {
64 fProc = proc;
65 fName.printf("cmap_%s", name);
66
67 for (int i = 0; i < NGLYPHS; ++i) {
68 // we're just jamming values into utf8, so we must keep it legal
69 fText[i] = i;
70 }
71 fPaint.setTypeface(SkTypeface::RefDefault())->unref();
72 }
73
74protected:
75 virtual const char* onGetName() SK_OVERRIDE {
76 return fName.c_str();
77 }
78
79 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
80 fProc(fPaint, fText, sizeof(fText), NGLYPHS);
81 }
82
83private:
84
85 typedef SkBenchmark INHERITED;
86};
87
88//////////////////////////////////////////////////////////////////////////////
89
90DEF_BENCH( return new CMAPBench(p, containsText_proc, "paint_containsText"); )
91DEF_BENCH( return new CMAPBench(p, textToGlyphs_proc, "paint_textToGlyphs"); )
92DEF_BENCH( return new CMAPBench(p, charsToGlyphs_proc, "face_charsToGlyphs"); )
93