Home | History | Annotate | Download | only in bench
      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 "SkBenchmark.h"
      9 #include "SkCanvas.h"
     10 #include "SkPaint.h"
     11 #include "SkTypeface.h"
     12 
     13 enum {
     14     LOOP = SkBENCHLOOP(1000),
     15     NGLYPHS = 100
     16 };
     17 
     18 static 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 
     24 typedef void (*TypefaceProc)(const SkPaint&, const void* text, size_t len,
     25                              int glyphCount);
     26 
     27 static 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 
     34 static 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 
     44 static 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 
     56 static 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 
     66 class CMAPBench : public SkBenchmark {
     67     TypefaceProc fProc;
     68     SkString     fName;
     69     char         fText[NGLYPHS];
     70     SkPaint      fPaint;
     71 
     72 public:
     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) {
     78             // we're jamming values into utf8, so we must keep it legal utf8
     79             fText[i] = 'A' + (i & 31);
     80         }
     81         fPaint.setTypeface(SkTypeface::RefDefault())->unref();
     82     }
     83 
     84 protected:
     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 
     93 private:
     94 
     95     typedef SkBenchmark INHERITED;
     96 };
     97 
     98 //////////////////////////////////////////////////////////////////////////////
     99 
    100 DEF_BENCH( return new CMAPBench(p, containsText_proc, "paint_containsText"); )
    101 DEF_BENCH( return new CMAPBench(p, textToGlyphs_proc, "paint_textToGlyphs"); )
    102 DEF_BENCH( return new CMAPBench(p, charsToGlyphs_proc, "face_charsToGlyphs"); )
    103 DEF_BENCH( return new CMAPBench(p, charsToGlyphsNull_proc, "face_charsToGlyphs_null"); )
    104