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 "SkTRegistry.h"
     17 #include "sk_tool_utils.h"
     18 
     19 class SkAnimTimer;
     20 
     21 #if SK_SUPPORT_GPU
     22 #include "GrContext.h"
     23 #endif
     24 
     25 #define DEF_GM(code) \
     26     static skiagm::GM*          SK_MACRO_APPEND_LINE(F_)(void*) { code; } \
     27     static skiagm::GMRegistry   SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
     28 
     29 // See colorwheel.cpp for example usage.
     30 #define DEF_SIMPLE_GM(NAME, CANVAS, W, H)                               \
     31     static void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas* CANVAS);           \
     32     DEF_GM( return SkNEW_ARGS(skiagm::SimpleGM,                         \
     33                               (SkString(#NAME),                         \
     34                                SK_MACRO_CONCAT(NAME, _GM),              \
     35                                SkISize::Make(W, H))); )                 \
     36     void SK_MACRO_CONCAT(NAME, _GM)(SkCanvas* CANVAS)
     37 
     38 namespace skiagm {
     39 
     40     class GM {
     41     public:
     42         GM();
     43         virtual ~GM();
     44 
     45         enum Mode {
     46             kGM_Mode,
     47             kSample_Mode,
     48             kBench_Mode,
     49         };
     50 
     51         void setMode(Mode mode) { fMode = mode; }
     52         Mode getMode() const { return fMode; }
     53 
     54         void draw(SkCanvas*);
     55         void drawBackground(SkCanvas*);
     56         void drawContent(SkCanvas*);
     57 
     58         SkISize getISize() { return this->onISize(); }
     59         const char* getName();
     60 
     61         virtual bool runAsBench() const { return false; }
     62 
     63         SkScalar width() {
     64             return SkIntToScalar(this->getISize().width());
     65         }
     66         SkScalar height() {
     67             return SkIntToScalar(this->getISize().height());
     68         }
     69 
     70         // TODO(vandebo) Instead of exposing this, we should run all the GMs
     71         // with and without an initial transform.
     72         // Most GMs will return the identity matrix, but some PDFs tests
     73         // require setting the initial transform.
     74         SkMatrix getInitialTransform() const {
     75             SkMatrix matrix = fStarterMatrix;
     76             matrix.preConcat(this->onGetInitialTransform());
     77             return matrix;
     78         }
     79 
     80         SkColor getBGColor() const { return fBGColor; }
     81         void setBGColor(SkColor);
     82 
     83         // helper: fill a rect in the specified color based on the
     84         // GM's getISize bounds.
     85         void drawSizeBounds(SkCanvas*, SkColor);
     86 
     87         bool isCanvasDeferred() const { return fCanvasIsDeferred; }
     88         void setCanvasIsDeferred(bool isDeferred) {
     89             fCanvasIsDeferred = isDeferred;
     90         }
     91 
     92         const SkMatrix& getStarterMatrix() { return fStarterMatrix; }
     93         void setStarterMatrix(const SkMatrix& matrix) {
     94             fStarterMatrix = matrix;
     95         }
     96 
     97         bool animate(const SkAnimTimer&);
     98 
     99     protected:
    100         /** draws a standard message that the GM is only intended to be used with the GPU.*/
    101         void drawGpuOnlyMessage(SkCanvas*);
    102         virtual void onOnceBeforeDraw() {}
    103         virtual void onDraw(SkCanvas*) = 0;
    104         virtual void onDrawBackground(SkCanvas*);
    105         virtual SkISize onISize() = 0;
    106         virtual SkString onShortName() = 0;
    107 
    108         virtual bool onAnimate(const SkAnimTimer&) { return false; }
    109         virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); }
    110 
    111     private:
    112         Mode     fMode;
    113         SkString fShortName;
    114         SkColor  fBGColor;
    115         bool     fCanvasIsDeferred; // work-around problem in srcmode.cpp
    116         bool     fHaveCalledOnceBeforeDraw;
    117         SkMatrix fStarterMatrix;
    118     };
    119 
    120     typedef SkTRegistry<GM*(*)(void*)> GMRegistry;
    121 
    122     class SimpleGM : public skiagm::GM {
    123     public:
    124         SimpleGM(const SkString& name,
    125                  void (*drawProc)(SkCanvas*),
    126                  const SkISize& size)
    127             : fName(name), fDrawProc(drawProc), fSize(size) {}
    128     protected:
    129         void onDraw(SkCanvas* canvas) override;
    130         SkISize onISize() override;
    131         SkString onShortName() override;
    132     private:
    133         SkString fName;
    134         void (*fDrawProc)(SkCanvas*);
    135         SkISize fSize;
    136     };
    137 }
    138 
    139 #endif
    140