SampleApp.h revision f97593c899d01ef3587355addbe664bc8f7b2dc5
1/*
2 * Copyright 2011 Skia
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 SampleApp_DEFINED
9#define SampleApp_DEFINED
10
11#include "SkOSMenu.h"
12#include "SkPath.h"
13#include "SkScalar.h"
14#include "SkTDArray.h"
15#include "SkTouchGesture.h"
16#include "SkWindow.h"
17
18class GrContext;
19class GrRenderTarget;
20
21class SkCanvas;
22class SkData;
23class SkEvent;
24class SkPicture;
25class SkTypeface;
26class SkViewFactory;
27
28class SampleWindow : public SkOSWindow {
29    SkTDArray<const SkViewFactory*> fSamples;
30public:
31    enum DeviceType {
32        kRaster_DeviceType,
33        kPicture_DeviceType,
34#if SK_SUPPORT_GPU
35        kGPU_DeviceType,
36#if SK_ANGLE
37        kANGLE_DeviceType,
38#endif // SK_ANGLE
39        kNullGPU_DeviceType,
40#endif // SK_SUPPORT_GPU
41
42        kDeviceTypeCnt
43    };
44    /**
45     * SampleApp ports can subclass this manager class if they want to:
46     *      * filter the types of devices supported
47     *      * customize plugging of SkDevice objects into an SkCanvas
48     *      * customize publishing the results of draw to the OS window
49     *      * manage GrContext / GrRenderTarget lifetimes
50     */
51    class DeviceManager : public SkRefCnt {
52    public:
53        SK_DECLARE_INST_COUNT(DeviceManager)
54
55        virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
56
57        virtual void tearDownBackend(SampleWindow* win) = 0;
58
59        // called before drawing. should install correct device
60        // type on the canvas. Will skip drawing if returns false.
61        virtual SkCanvas* createCanvas(DeviceType dType, SampleWindow* win) = 0;
62
63        // called after drawing, should get the results onto the
64        // screen.
65        virtual void publishCanvas(DeviceType dType,
66                                   SkCanvas* canvas,
67                                   SampleWindow* win) = 0;
68
69        // called when window changes size, guaranteed to be called
70        // at least once before first draw (after init)
71        virtual void windowSizeChanged(SampleWindow* win) = 0;
72
73        // return the GrContext backing gpu devices (NULL if not built with GPU support)
74        virtual GrContext* getGrContext() = 0;
75
76        // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support)
77        virtual GrRenderTarget* getGrRenderTarget() = 0;
78    private:
79        typedef SkRefCnt INHERITED;
80    };
81
82    SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
83    virtual ~SampleWindow();
84
85    virtual SkCanvas* createCanvas() SK_OVERRIDE {
86        SkCanvas* canvas = NULL;
87        if (fDevManager) {
88            canvas = fDevManager->createCanvas(fDeviceType, this);
89        }
90        if (NULL == canvas) {
91            canvas = this->INHERITED::createCanvas();
92        }
93        return canvas;
94    }
95
96    virtual void draw(SkCanvas* canvas);
97
98    void setDeviceType(DeviceType type);
99    void toggleRendering();
100    void toggleSlideshow();
101    void toggleFPS();
102    void showOverview();
103
104    GrContext* getGrContext() const { return fDevManager->getGrContext(); }
105
106    void setZoomCenter(float x, float y);
107    void changeZoomLevel(float delta);
108    bool nextSample();
109    bool previousSample();
110    bool goToSample(int i);
111    SkString getSampleTitle(int i);
112    int  sampleCount();
113    bool handleTouch(int ownerId, float x, float y,
114            SkView::Click::State state);
115    void saveToPdf();
116    SkData* getPDFData() { return fPDFData; }
117    void postInvalDelay();
118
119    DeviceType getDeviceType() const { return fDeviceType; }
120
121protected:
122    virtual void onDraw(SkCanvas* canvas);
123    virtual bool onHandleKey(SkKey key);
124    virtual bool onHandleChar(SkUnichar);
125    virtual void onSizeChange();
126
127    virtual SkCanvas* beforeChildren(SkCanvas*);
128    virtual void afterChildren(SkCanvas*);
129    virtual void beforeChild(SkView* child, SkCanvas* canvas);
130    virtual void afterChild(SkView* child, SkCanvas* canvas);
131
132    virtual bool onEvent(const SkEvent& evt);
133    virtual bool onQuery(SkEvent* evt);
134
135    virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
136    virtual bool onClick(Click* click);
137    virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
138
139    void registerPictFileSamples(char** argv, int argc);
140    void registerPictFileSample(char** argv, int argc);
141
142private:
143    class DefaultDeviceManager;
144
145    int fCurrIndex;
146
147    SkPicture* fPicture;
148    SkPath fClipPath;
149
150    SkTouchGesture fGesture;
151    SkScalar fZoomLevel;
152    SkScalar fZoomScale;
153
154    DeviceType fDeviceType;
155    DeviceManager* fDevManager;
156
157    bool fSaveToPdf;
158    SkCanvas* fPdfCanvas;
159    SkData* fPDFData;
160
161    bool fUseClip;
162    bool fNClip;
163    bool fAnimating;
164    bool fRotate;
165    bool fPerspAnim;
166    SkScalar fPerspAnimTime;
167    bool fScale;
168    bool fRequestGrabImage;
169    bool fMeasureFPS;
170    SkMSec fMeasureFPS_Time;
171    bool fMagnify;
172    SkISize fTileCount;
173
174
175    SkOSMenu::TriState fPipeState;  // Mixed uses a tiled pipe
176                                    // On uses a normal pipe
177                                    // Off uses no pipe
178    int  fUsePipeMenuItemID;
179    bool fDebugger;
180
181    // The following are for the 'fatbits' drawing
182    // Latest position of the mouse.
183    int fMouseX, fMouseY;
184    int fFatBitsScale;
185    // Used by the text showing position and color values.
186    SkTypeface* fTypeface;
187    bool fShowZoomer;
188
189    SkOSMenu::TriState fLCDState;
190    SkOSMenu::TriState fAAState;
191    SkOSMenu::TriState fFilterState;
192    SkOSMenu::TriState fHintingState;
193    SkOSMenu::TriState fTilingState;
194    unsigned   fFlipAxis;
195
196    int fMSAASampleCount;
197
198    int fScrollTestX, fScrollTestY;
199    SkScalar fZoomCenterX, fZoomCenterY;
200
201    //Stores global settings
202    SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
203    //Stores slide specific settings
204    SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
205
206    int fTransitionNext;
207    int fTransitionPrev;
208
209    void loadView(SkView*);
210    void updateTitle();
211
212    bool zoomIn();
213    bool zoomOut();
214    void updatePointer(int x, int y);
215    void magnify(SkCanvas* canvas);
216    void showZoomer(SkCanvas* canvas);
217    void updateMatrix();
218    void postAnimatingEvent();
219    void installDrawFilter(SkCanvas*);
220    int findByTitle(const char*);
221    void listTitles();
222
223    typedef SkOSWindow INHERITED;
224};
225
226#endif
227