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