SampleApp.h revision f726a1c9e6efad526eba603be9f24b4b77b993eb
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 bool prepareCanvas(DeviceType dType,
62                                   SkCanvas* canvas,
63                                   SampleWindow* win) = 0;
64
65        // called after drawing, should get the results onto the
66        // screen.
67        virtual void publishCanvas(DeviceType dType,
68                                   SkCanvas* canvas,
69                                   SampleWindow* win) = 0;
70
71        // called when window changes size, guaranteed to be called
72        // at least once before first draw (after init)
73        virtual void windowSizeChanged(SampleWindow* win) = 0;
74
75        // return the GrContext backing gpu devices (NULL if not built with GPU support)
76        virtual GrContext* getGrContext() = 0;
77
78        // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support)
79        virtual GrRenderTarget* getGrRenderTarget() = 0;
80    private:
81        typedef SkRefCnt INHERITED;
82    };
83
84    SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
85    virtual ~SampleWindow();
86
87    virtual void draw(SkCanvas* canvas);
88
89    void setDeviceType(DeviceType type);
90    void toggleRendering();
91    void toggleSlideshow();
92    void toggleFPS();
93    void showOverview();
94
95    GrContext* getGrContext() const { return fDevManager->getGrContext(); }
96
97    void setZoomCenter(float x, float y);
98    void changeZoomLevel(float delta);
99    bool nextSample();
100    bool previousSample();
101    bool goToSample(int i);
102    SkString getSampleTitle(int i);
103    int  sampleCount();
104    bool handleTouch(int ownerId, float x, float y,
105            SkView::Click::State state);
106    void saveToPdf();
107    SkData* getPDFData() { return fPDFData; }
108    void postInvalDelay();
109
110    DeviceType getDeviceType() const { return fDeviceType; }
111
112protected:
113    virtual void onDraw(SkCanvas* canvas);
114    virtual bool onHandleKey(SkKey key);
115    virtual bool onHandleChar(SkUnichar);
116    virtual void onSizeChange();
117
118    virtual SkCanvas* beforeChildren(SkCanvas*);
119    virtual void afterChildren(SkCanvas*);
120    virtual void beforeChild(SkView* child, SkCanvas* canvas);
121    virtual void afterChild(SkView* child, SkCanvas* canvas);
122
123    virtual bool onEvent(const SkEvent& evt);
124    virtual bool onQuery(SkEvent* evt);
125
126    virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
127    virtual bool onClick(Click* click);
128    virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
129
130    void registerPictFileSamples(char** argv, int argc);
131    void registerPictFileSample(char** argv, int argc);
132
133private:
134    class DefaultDeviceManager;
135
136    int fCurrIndex;
137
138    SkPicture* fPicture;
139    SkPath fClipPath;
140
141    SkTouchGesture fGesture;
142    SkScalar fZoomLevel;
143    SkScalar fZoomScale;
144
145    DeviceType fDeviceType;
146    DeviceManager* fDevManager;
147
148    bool fSaveToPdf;
149    SkCanvas* fPdfCanvas;
150    SkData* fPDFData;
151
152    bool fUseClip;
153    bool fNClip;
154    bool fAnimating;
155    bool fRotate;
156    bool fPerspAnim;
157    SkScalar fPerspAnimTime;
158    bool fScale;
159    bool fRequestGrabImage;
160    bool fMeasureFPS;
161    SkMSec fMeasureFPS_Time;
162    bool fMagnify;
163    SkISize fTileCount;
164
165
166    SkOSMenu::TriState fPipeState;  // Mixed uses a tiled pipe
167                                    // On uses a normal pipe
168                                    // Off uses no pipe
169    int  fUsePipeMenuItemID;
170    bool fDebugger;
171
172    // The following are for the 'fatbits' drawing
173    // Latest position of the mouse.
174    int fMouseX, fMouseY;
175    int fFatBitsScale;
176    // Used by the text showing position and color values.
177    SkTypeface* fTypeface;
178    bool fShowZoomer;
179
180    SkOSMenu::TriState fLCDState;
181    SkOSMenu::TriState fAAState;
182    SkOSMenu::TriState fFilterState;
183    SkOSMenu::TriState fHintingState;
184    SkOSMenu::TriState fTilingState;
185    unsigned   fFlipAxis;
186
187    int fMSAASampleCount;
188
189    int fScrollTestX, fScrollTestY;
190    SkScalar fZoomCenterX, fZoomCenterY;
191
192    //Stores global settings
193    SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
194    //Stores slide specific settings
195    SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
196
197    int fTransitionNext;
198    int fTransitionPrev;
199
200    void loadView(SkView*);
201    void updateTitle();
202
203    bool zoomIn();
204    bool zoomOut();
205    void updatePointer(int x, int y);
206    void magnify(SkCanvas* canvas);
207    void showZoomer(SkCanvas* canvas);
208    void updateMatrix();
209    void postAnimatingEvent();
210    void installDrawFilter(SkCanvas*);
211    int findByTitle(const char*);
212    void listTitles();
213
214    typedef SkOSWindow INHERITED;
215};
216
217#endif
218