Home | History | Annotate | Download | only in bench
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #ifndef SkBenchmark_DEFINED
      9 #define SkBenchmark_DEFINED
     10 
     11 #include "SkRefCnt.h"
     12 #include "SkPoint.h"
     13 #include "SkTDict.h"
     14 #include "SkTRegistry.h"
     15 
     16 #define DEF_BENCH(code) \
     17 static SkBenchmark* SK_MACRO_APPEND_LINE(F_)(void* p) { code; } \
     18 static BenchRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
     19 
     20 /*
     21  *  With the above macros, you can register benches as follows (at the bottom
     22  *  of your .cpp)
     23  *
     24  *  DEF_BENCH(new MyBenchmark(p, ...))
     25  *  DEF_BENCH(new MyBenchmark(p, ...))
     26  *  DEF_BENCH(new MyBenchmark(p, ...))
     27  */
     28 
     29 
     30 #ifdef SK_DEBUG
     31     #define SkBENCHLOOP(n) 1
     32 #else
     33     #define SkBENCHLOOP(n) n
     34 #endif
     35 
     36 class SkCanvas;
     37 class SkPaint;
     38 
     39 class SkTriState {
     40 public:
     41     enum State {
     42         kDefault,
     43         kTrue,
     44         kFalse
     45     };
     46 };
     47 
     48 class SkBenchmark : public SkRefCnt {
     49 public:
     50     SK_DECLARE_INST_COUNT(SkBenchmark)
     51 
     52     SkBenchmark(void* defineDict);
     53 
     54     const char* getName();
     55     SkIPoint getSize();
     56 
     57     // Call before draw, allows the benchmark to do setup work outside of the
     58     // timer. When a benchmark is repeatedly drawn, this should be called once
     59     // before the initial draw.
     60     void preDraw();
     61 
     62     void draw(SkCanvas*);
     63 
     64     // Call after draw, allows the benchmark to do cleanup work outside of the
     65     // timer. When a benchmark is repeatedly drawn, this is only called once
     66     // after the last draw.
     67     void postDraw();
     68 
     69     void setForceAlpha(int alpha) {
     70         fForceAlpha = alpha;
     71     }
     72 
     73     void setForceAA(bool aa) {
     74         fForceAA = aa;
     75     }
     76 
     77     void setForceFilter(bool filter) {
     78         fForceFilter = filter;
     79     }
     80 
     81     void setDither(SkTriState::State state) {
     82         fDither = state;
     83     }
     84 
     85     void setStrokeWidth(SkScalar width) {
     86       strokeWidth = width;
     87       fHasStrokeWidth = true;
     88     }
     89 
     90     SkScalar getStrokeWidth() {
     91       return strokeWidth;
     92     }
     93 
     94     bool hasStrokeWidth() {
     95       return fHasStrokeWidth;
     96     }
     97 
     98     /** If true; the benchmark does rendering; if false, the benchmark
     99         doesn't, and so need not be re-run in every different rendering
    100         mode. */
    101     bool isRendering() {
    102         return fIsRendering;
    103     }
    104 
    105     const char* findDefine(const char* key) const;
    106     bool findDefine32(const char* key, int32_t* value) const;
    107     bool findDefineScalar(const char* key, SkScalar* value) const;
    108 
    109 protected:
    110     virtual void setupPaint(SkPaint* paint);
    111 
    112     virtual const char* onGetName() = 0;
    113     virtual void onPreDraw() {}
    114     virtual void onDraw(SkCanvas*) = 0;
    115     virtual void onPostDraw() {}
    116 
    117     virtual SkIPoint onGetSize();
    118     /// Defaults to true.
    119     bool    fIsRendering;
    120 
    121 private:
    122     const SkTDict<const char*>* fDict;
    123     int     fForceAlpha;
    124     bool    fForceAA;
    125     bool    fForceFilter;
    126     SkTriState::State  fDither;
    127     bool    fHasStrokeWidth;
    128     SkScalar strokeWidth;
    129 
    130     typedef SkRefCnt INHERITED;
    131 };
    132 
    133 typedef SkTRegistry<SkBenchmark*, void*> BenchRegistry;
    134 
    135 #endif
    136