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