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 "SkString.h"
     14 #include "SkTRegistry.h"
     15 
     16 #define DEF_BENCH(code) \
     17 static SkBenchmark* SK_MACRO_APPEND_LINE(F_)() { 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(return new MyBenchmark(...))
     25  *  DEF_BENCH(return new MyBenchmark(...))
     26  *  DEF_BENCH(return new MyBenchmark(...))
     27  */
     28 
     29 
     30 class SkCanvas;
     31 class SkPaint;
     32 
     33 class SkTriState {
     34 public:
     35     enum State {
     36         kDefault,
     37         kTrue,
     38         kFalse
     39     };
     40     static const char* Name[];
     41 };
     42 
     43 class SkBenchmark : public SkRefCnt {
     44 public:
     45     SK_DECLARE_INST_COUNT(SkBenchmark)
     46 
     47     SkBenchmark();
     48 
     49     const char* getName();
     50     SkIPoint getSize();
     51 
     52     enum Backend {
     53         kNonRendering_Backend,
     54         kRaster_Backend,
     55         kGPU_Backend,
     56         kPDF_Backend,
     57     };
     58 
     59     // Call to determine whether the benchmark is intended for
     60     // the rendering mode.
     61     virtual bool isSuitableFor(Backend backend) {
     62         return backend != kNonRendering_Backend;
     63     }
     64 
     65     // Call before draw, allows the benchmark to do setup work outside of the
     66     // timer. When a benchmark is repeatedly drawn, this should be called once
     67     // before the initial draw.
     68     void preDraw();
     69 
     70     // Bench framework can tune loops to be large enough for stable timing.
     71     void draw(const int loops, SkCanvas*);
     72 
     73     // Call after draw, allows the benchmark to do cleanup work outside of the
     74     // timer. When a benchmark is repeatedly drawn, this is only called once
     75     // after the last draw.
     76     void postDraw();
     77 
     78     void setForceAlpha(int alpha) {
     79         fForceAlpha = alpha;
     80     }
     81 
     82     void setForceAA(bool aa) {
     83         fForceAA = aa;
     84     }
     85 
     86     void setForceFilter(bool filter) {
     87         fForceFilter = filter;
     88     }
     89 
     90     void setDither(SkTriState::State state) {
     91         fDither = state;
     92     }
     93 
     94     /** Assign masks for paint-flags. These will be applied when setupPaint()
     95      *  is called.
     96      *
     97      *  Performs the following on the paint:
     98      *      uint32_t flags = paint.getFlags();
     99      *      flags &= ~clearMask;
    100      *      flags |= orMask;
    101      *      paint.setFlags(flags);
    102      */
    103     void setPaintMasks(uint32_t orMask, uint32_t clearMask) {
    104         fOrMask = orMask;
    105         fClearMask = clearMask;
    106     }
    107 
    108     static void SetResourcePath(const char* resPath) { gResourcePath.set(resPath); }
    109 
    110     static SkString& GetResourcePath() { return gResourcePath; }
    111 
    112 protected:
    113     virtual void setupPaint(SkPaint* paint);
    114 
    115     virtual const char* onGetName() = 0;
    116     virtual void onPreDraw() {}
    117     // Each bench should do its main work in a loop like this:
    118     //   for (int i = 0; i < loops; i++) { <work here> }
    119     virtual void onDraw(const int loops, SkCanvas*) = 0;
    120     virtual void onPostDraw() {}
    121 
    122     virtual SkIPoint onGetSize();
    123 
    124 private:
    125     int     fForceAlpha;
    126     bool    fForceAA;
    127     bool    fForceFilter;
    128     SkTriState::State  fDither;
    129     uint32_t    fOrMask, fClearMask;
    130     static  SkString gResourcePath;
    131 
    132     typedef SkRefCnt INHERITED;
    133 };
    134 
    135 typedef SkTRegistry<SkBenchmark*(*)()> BenchRegistry;
    136 
    137 #endif
    138