SampleApp.h revision b17859133aeb0ebe80805fbd437759e2b956d859
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
161private:
162    class DefaultDeviceManager;
163
164    int fCurrIndex;
165
166    SkPicture* fPicture;
167    SkPath fClipPath;
168
169    SkTouchGesture fGesture;
170    SkScalar fZoomLevel;
171    SkScalar fZoomScale;
172
173    DeviceType fDeviceType;
174    DeviceManager* fDevManager;
175
176    bool fSaveToPdf;
177    SkCanvas* fPdfCanvas;
178    SkData* fPDFData;
179
180    bool fUseClip;
181    bool fNClip;
182    bool fAnimating;
183    bool fRotate;
184    SkScalar fRotateAnimTime;
185    bool fPerspAnim;
186    SkScalar fPerspAnimTime;
187    bool fRequestGrabImage;
188    bool fMeasureFPS;
189    SkMSec fMeasureFPS_Time;
190    SkMSec fMeasureFPS_StartTime;
191    bool fMagnify;
192    SkISize fTileCount;
193
194
195    SkOSMenu::TriState fPipeState;  // Mixed uses a tiled pipe
196                                    // On uses a normal pipe
197                                    // Off uses no pipe
198    int  fUsePipeMenuItemID;
199
200    // The following are for the 'fatbits' drawing
201    // Latest position of the mouse.
202    int fMouseX, fMouseY;
203    int fFatBitsScale;
204    // Used by the text showing position and color values.
205    SkTypeface* fTypeface;
206    bool fShowZoomer;
207
208    SkOSMenu::TriState fLCDState;
209    SkOSMenu::TriState fAAState;
210    SkOSMenu::TriState fFilterState;
211    SkOSMenu::TriState fSubpixelState;
212    int fHintingState;
213    SkOSMenu::TriState fTilingState;
214    unsigned   fFlipAxis;
215
216    int fMSAASampleCount;
217
218    int fScrollTestX, fScrollTestY;
219    SkScalar fZoomCenterX, fZoomCenterY;
220
221    //Stores global settings
222    SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
223    //Stores slide specific settings
224    SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
225
226    int fTransitionNext;
227    int fTransitionPrev;
228
229    void loadView(SkView*);
230    void updateTitle();
231
232    bool zoomIn();
233    bool zoomOut();
234    void updatePointer(int x, int y);
235    void magnify(SkCanvas* canvas);
236    void showZoomer(SkCanvas* canvas);
237    void updateMatrix();
238    void postAnimatingEvent();
239    void installDrawFilter(SkCanvas*);
240    int findByTitle(const char*);
241    void listTitles();
242
243    typedef SkOSWindow INHERITED;
244};
245
246#endif
247