blob: fcf40be88a72644b51e0073817011867308fedb7 [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 {
reed@google.combcb42ae2013-07-02 13:56:39 +000014 NGLYPHS = 100
15};
16
17static 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.comc2897432013-09-10 19:23:38 +000023typedef void (*TypefaceProc)(int loops, const SkPaint&, const void* text, size_t len,
reed@google.combcb42ae2013-07-02 13:56:39 +000024 int glyphCount);
25
mtklein@google.comc2897432013-09-10 19:23:38 +000026static void containsText_proc(int loops, const SkPaint& paint, const void* text, size_t len,
reed@google.combcb42ae2013-07-02 13:56:39 +000027 int glyphCount) {
mtklein@google.comc2897432013-09-10 19:23:38 +000028 for (int i = 0; i < loops; ++i) {
reed@google.combcb42ae2013-07-02 13:56:39 +000029 paint.containsText(text, len);
30 }
31}
32
mtklein@google.comc2897432013-09-10 19:23:38 +000033static void textToGlyphs_proc(int loops, const SkPaint& paint, const void* text, size_t len,
reed@google.combcb42ae2013-07-02 13:56:39 +000034 int glyphCount) {
35 uint16_t glyphs[NGLYPHS];
36 SkASSERT(glyphCount <= NGLYPHS);
37
mtklein@google.comc2897432013-09-10 19:23:38 +000038 for (int i = 0; i < loops; ++i) {
reed@google.combcb42ae2013-07-02 13:56:39 +000039 paint.textToGlyphs(text, len, glyphs);
40 }
41}
42
mtklein@google.comc2897432013-09-10 19:23:38 +000043static void charsToGlyphs_proc(int loops, const SkPaint& paint, const void* text,
reed@google.combcb42ae2013-07-02 13:56:39 +000044 size_t len, int glyphCount) {
45 SkTypeface::Encoding encoding = paint2Encoding(paint);
46 uint16_t glyphs[NGLYPHS];
47 SkASSERT(glyphCount <= NGLYPHS);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000048
reed@google.combcb42ae2013-07-02 13:56:39 +000049 SkTypeface* face = paint.getTypeface();
mtklein@google.comc2897432013-09-10 19:23:38 +000050 for (int i = 0; i < loops; ++i) {
reed@google.combcb42ae2013-07-02 13:56:39 +000051 face->charsToGlyphs(text, encoding, glyphs, glyphCount);
52 }
53}
54
mtklein@google.comc2897432013-09-10 19:23:38 +000055static void charsToGlyphsNull_proc(int loops, const SkPaint& paint, const void* text,
reed@google.com23112972013-07-02 16:28:53 +000056 size_t len, int glyphCount) {
57 SkTypeface::Encoding encoding = paint2Encoding(paint);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000058
reed@google.com23112972013-07-02 16:28:53 +000059 SkTypeface* face = paint.getTypeface();
mtklein@google.comc2897432013-09-10 19:23:38 +000060 for (int i = 0; i < loops; ++i) {
reed@google.com23112972013-07-02 16:28:53 +000061 face->charsToGlyphs(text, encoding, NULL, glyphCount);
62 }
63}
64
reed@google.combcb42ae2013-07-02 13:56:39 +000065class CMAPBench : public SkBenchmark {
66 TypefaceProc fProc;
67 SkString fName;
68 char fText[NGLYPHS];
69 SkPaint fPaint;
70
71public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000072 CMAPBench(TypefaceProc proc, const char name[]) {
reed@google.combcb42ae2013-07-02 13:56:39 +000073 fProc = proc;
74 fName.printf("cmap_%s", name);
skia.committer@gmail.com98a19672013-07-03 07:00:57 +000075
reed@google.combcb42ae2013-07-02 13:56:39 +000076 for (int i = 0; i < NGLYPHS; ++i) {
reed@google.com2d903ae2013-07-02 20:06:00 +000077 // we're jamming values into utf8, so we must keep it legal utf8
78 fText[i] = 'A' + (i & 31);
reed@google.combcb42ae2013-07-02 13:56:39 +000079 }
80 fPaint.setTypeface(SkTypeface::RefDefault())->unref();
81 }
82
83protected:
84 virtual const char* onGetName() SK_OVERRIDE {
85 return fName.c_str();
86 }
87
commit-bot@chromium.org33614712013-12-03 18:17:16 +000088 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
89 fProc(loops, fPaint, fText, sizeof(fText), NGLYPHS);
reed@google.combcb42ae2013-07-02 13:56:39 +000090 }
91
92private:
93
94 typedef SkBenchmark INHERITED;
95};
96
97//////////////////////////////////////////////////////////////////////////////
98
mtklein@google.com410e6e82013-09-13 19:52:27 +000099DEF_BENCH( return new CMAPBench(containsText_proc, "paint_containsText"); )
100DEF_BENCH( return new CMAPBench(textToGlyphs_proc, "paint_textToGlyphs"); )
101DEF_BENCH( return new CMAPBench(charsToGlyphs_proc, "face_charsToGlyphs"); )
102DEF_BENCH( return new CMAPBench(charsToGlyphsNull_proc, "face_charsToGlyphs_null"); )