blob: 10591ed410c6133158177e260a6fb92a5f9d330f [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.combcb42ae2013-07-02 13:56:39 +00009#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
tfarinaf168b862014-06-19 12:32:29 -070065class CMAPBench : public Benchmark {
reed@google.combcb42ae2013-07-02 13:56:39 +000066 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:
mtklein36352bf2015-03-25 18:17:31 -070084 const char* onGetName() override {
reed@google.combcb42ae2013-07-02 13:56:39 +000085 return fName.c_str();
86 }
87
mtklein36352bf2015-03-25 18:17:31 -070088 void onDraw(const int loops, SkCanvas* canvas) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000089 fProc(loops, fPaint, fText, sizeof(fText), NGLYPHS);
reed@google.combcb42ae2013-07-02 13:56:39 +000090 }
91
92private:
93
tfarinaf168b862014-06-19 12:32:29 -070094 typedef Benchmark INHERITED;
reed@google.combcb42ae2013-07-02 13:56:39 +000095};
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"); )