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 13enum { 14 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 23typedef void (*TypefaceProc)(int loops, const SkPaint&, const void* text, size_t len, 24 int glyphCount); 25 26static 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 33static 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 43static 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 55static 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 65class CMAPBench : public Benchmark { 66 TypefaceProc fProc; 67 SkString fName; 68 char fText[NGLYPHS]; 69 SkPaint fPaint; 70 71public: 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 83protected: 84 virtual const char* onGetName() SK_OVERRIDE { 85 return fName.c_str(); 86 } 87 88 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { 89 fProc(loops, fPaint, fText, sizeof(fText), NGLYPHS); 90 } 91 92private: 93 94 typedef Benchmark INHERITED; 95}; 96 97////////////////////////////////////////////////////////////////////////////// 98 99DEF_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"); ) 103