Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2011 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 #ifndef skiagm_DEFINED
      9 #define skiagm_DEFINED
     10 
     11 #include "SkBitmap.h"
     12 #include "SkCanvas.h"
     13 #include "SkPaint.h"
     14 #include "SkSize.h"
     15 #include "SkString.h"
     16 #include "../tools/Registry.h"
     17 #include "SkClipOpPriv.h"
     18 
     19 class SkAnimTimer;
     20 struct GrContextOptions;
     21 
     22 #define DEF_GM(code) \
     23     static skiagm::GM*          SK_MACRO_APPEND_LINE(F_)(void*) { code; } \
     24     static skiagm::GMRegistry   SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
     25 
     26 // a Simple GM is a rendering test that does not store state between
     27 // rendering calls or make use of the onOnceBeforeDraw() virtual; it
     28 // consists of:
     29 //   *   A single void(*)(SkCanvas*) function.
     30 //   *   A name.
     31 //   *   Prefered width and height.
     32 //   *   Optionally, a background color (default is white).
     33 #define DEF_SIMPLE_GM(NAME, CANVAS, W, H) \
     34     DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, SK_ColorWHITE, SkString(#NAME))
     35 #define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR)\
     36     DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, SkString(#NAME))
     37 #define DEF_SIMPLE_GM_BG_NAME(NAME, CANVAS, W, H, BGCOLOR, NAME_STR)         \
     38     static void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS);               \
     39     DEF_GM(return new skiagm::SimpleGM(NAME_STR, SK_MACRO_CONCAT(NAME, _GM), \
     40                                        SkISize::Make(W, H), BGCOLOR);)       \
     41     void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas * CANVAS)
     42 
     43 namespace skiagm {
     44 
     45     class GM {
     46     public:
     47         GM();
     48         virtual ~GM();
     49 
     50         enum Mode {
     51             kGM_Mode,
     52             kSample_Mode,
     53             kBench_Mode,
     54         };
     55 
     56         void setMode(Mode mode) { fMode = mode; }
     57         Mode getMode() const { return fMode; }
     58 
     59         void draw(SkCanvas*);
     60         void drawBackground(SkCanvas*);
     61         void drawContent(SkCanvas*);
     62 
     63         SkISize getISize() { return this->onISize(); }
     64         const char* getName();
     65 
     66         virtual bool runAsBench() const { return false; }
     67 
     68         SkScalar width() {
     69             return SkIntToScalar(this->getISize().width());
     70         }
     71         SkScalar height() {
     72             return SkIntToScalar(this->getISize().height());
     73         }
     74 
     75         // TODO(vandebo) Instead of exposing this, we should run all the GMs
     76         // with and without an initial transform.
     77         // Most GMs will return the identity matrix, but some PDFs tests
     78         // require setting the initial transform.
     79         SkMatrix getInitialTransform() const {
     80             SkMatrix matrix = fStarterMatrix;
     81             matrix.preConcat(this->onGetInitialTransform());
     82             return matrix;
     83         }
     84 
     85         SkColor getBGColor() const { return fBGColor; }
     86         void setBGColor(SkColor);
     87 
     88         // helper: fill a rect in the specified color based on the
     89         // GM's getISize bounds.
     90         void drawSizeBounds(SkCanvas*, SkColor);
     91 
     92         bool isCanvasDeferred() const { return fCanvasIsDeferred; }
     93         void setCanvasIsDeferred(bool isDeferred) {
     94             fCanvasIsDeferred = isDeferred;
     95         }
     96 
     97         const SkMatrix& getStarterMatrix() { return fStarterMatrix; }
     98         void setStarterMatrix(const SkMatrix& matrix) {
     99             fStarterMatrix = matrix;
    100         }
    101 
    102         bool animate(const SkAnimTimer&);
    103         bool handleKey(SkUnichar uni) {
    104             return this->onHandleKey(uni);
    105         }
    106 
    107         virtual void modifyGrContextOptions(GrContextOptions* options) {}
    108 
    109         /** draws a standard message that the GM is only intended to be used with the GPU.*/
    110         static void DrawGpuOnlyMessage(SkCanvas*);
    111 
    112     protected:
    113         virtual void onOnceBeforeDraw() {}
    114         virtual void onDraw(SkCanvas*) = 0;
    115         virtual void onDrawBackground(SkCanvas*);
    116         virtual SkISize onISize() = 0;
    117         virtual SkString onShortName() = 0;
    118 
    119         virtual bool onAnimate(const SkAnimTimer&) { return false; }
    120         virtual bool onHandleKey(SkUnichar uni) { return false; }
    121         virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); }
    122 
    123     private:
    124         Mode     fMode;
    125         SkString fShortName;
    126         SkColor  fBGColor;
    127         bool     fCanvasIsDeferred; // work-around problem in srcmode.cpp
    128         bool     fHaveCalledOnceBeforeDraw;
    129         SkMatrix fStarterMatrix;
    130     };
    131 
    132     typedef sk_tools::Registry<GM*(*)(void*)> GMRegistry;
    133 
    134     class SimpleGM : public skiagm::GM {
    135     public:
    136         SimpleGM(const SkString& name,
    137                  void (*drawProc)(SkCanvas*),
    138                  const SkISize& size,
    139                  SkColor backgroundColor)
    140             : fName(name), fDrawProc(drawProc), fSize(size) {
    141             if (backgroundColor != SK_ColorWHITE) {
    142                 this->setBGColor(backgroundColor);
    143             }
    144         }
    145     protected:
    146         void onDraw(SkCanvas* canvas) override;
    147         SkISize onISize() override;
    148         SkString onShortName() override;
    149     private:
    150         SkString fName;
    151         void (*fDrawProc)(SkCanvas*);
    152         SkISize fSize;
    153     };
    154 }
    155 
    156 #endif
    157