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 
     18 #if SK_SUPPORT_GPU
     19 #include "GrContext.h"
     20 #endif
     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 namespace skiagm {
     27 
     28     class GM {
     29     public:
     30         GM();
     31         virtual ~GM();
     32 
     33         enum Flags {
     34             kSkipPDF_Flag               = 1 << 0,
     35             kSkipPicture_Flag           = 1 << 1,
     36             kSkipPipe_Flag              = 1 << 2,
     37             kSkipPipeCrossProcess_Flag  = 1 << 3,
     38             kSkipTiled_Flag             = 1 << 4,
     39             kSkip565_Flag               = 1 << 5,
     40             kSkipScaledReplay_Flag      = 1 << 6,
     41             kSkipGPU_Flag               = 1 << 7,
     42             kSkipPDFRasterization_Flag  = 1 << 8,
     43 
     44             kGPUOnly_Flag               = 1 << 9,
     45 
     46             kAsBench_Flag               = 1 << 10, // Run the GM as a benchmark in the bench tool
     47         };
     48 
     49         enum Mode {
     50             kGM_Mode,
     51             kSample_Mode,
     52             kBench_Mode,
     53         };
     54 
     55         void setMode(Mode mode) { fMode = mode; }
     56         Mode getMode() const { return fMode; }
     57 
     58         void draw(SkCanvas*);
     59         void drawBackground(SkCanvas*);
     60         void drawContent(SkCanvas*);
     61 
     62         SkISize getISize() { return this->onISize(); }
     63         const char* getName();
     64 
     65         uint32_t getFlags() const {
     66             return this->onGetFlags();
     67         }
     68 
     69         SkScalar width() {
     70             return SkIntToScalar(this->getISize().width());
     71         }
     72         SkScalar height() {
     73             return SkIntToScalar(this->getISize().height());
     74         }
     75 
     76         // TODO(vandebo) Instead of exposing this, we should run all the GMs
     77         // with and without an initial transform.
     78         // Most GMs will return the identity matrix, but some PDFs tests
     79         // require setting the initial transform.
     80         SkMatrix getInitialTransform() const {
     81             SkMatrix matrix = fStarterMatrix;
     82             matrix.preConcat(this->onGetInitialTransform());
     83             return matrix;
     84         }
     85 
     86         SkColor getBGColor() const { return fBGColor; }
     87         void setBGColor(SkColor);
     88 
     89         // helper: fill a rect in the specified color based on the
     90         // GM's getISize bounds.
     91         void drawSizeBounds(SkCanvas*, SkColor);
     92 
     93         bool isCanvasDeferred() const { return fCanvasIsDeferred; }
     94         void setCanvasIsDeferred(bool isDeferred) {
     95             fCanvasIsDeferred = isDeferred;
     96         }
     97 
     98         const SkMatrix& getStarterMatrix() { return fStarterMatrix; }
     99         void setStarterMatrix(const SkMatrix& matrix) {
    100             fStarterMatrix = matrix;
    101         }
    102 
    103     protected:
    104         virtual void onOnceBeforeDraw() {}
    105         virtual void onDraw(SkCanvas*) = 0;
    106         virtual void onDrawBackground(SkCanvas*);
    107         virtual SkISize onISize() = 0;
    108         virtual SkString onShortName() = 0;
    109         virtual uint32_t onGetFlags() const { return 0; }
    110         virtual SkMatrix onGetInitialTransform() const { return SkMatrix::I(); }
    111 
    112     private:
    113         Mode     fMode;
    114         SkString fShortName;
    115         SkColor  fBGColor;
    116         bool     fCanvasIsDeferred; // work-around problem in srcmode.cpp
    117         bool     fHaveCalledOnceBeforeDraw;
    118         SkMatrix fStarterMatrix;
    119     };
    120 
    121     typedef SkTRegistry<GM*(*)(void*)> GMRegistry;
    122 }
    123 
    124 #endif
    125