SampleApp.h revision bb0502eec5c66ddd2f54330711010aabfa1c2176
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 "SkPicture.h"
14#include "SkPictureRecorder.h"
15#include "SkScalar.h"
16#include "SkTDArray.h"
17#include "SkTouchGesture.h"
18#include "SkWindow.h"
19
20class GrContext;
21class GrRenderTarget;
22
23class SkCanvas;
24class SkData;
25class SkDocument;
26class SkEvent;
27class SkTypeface;
28class SkViewFactory;
29
30class SampleWindow : public SkOSWindow {
31    SkTDArray<const SkViewFactory*> fSamples;
32public:
33    enum DeviceType {
34        kRaster_DeviceType,
35        kPicture_DeviceType,
36#if SK_SUPPORT_GPU
37        kGPU_DeviceType,
38#if SK_ANGLE
39        kANGLE_DeviceType,
40#endif // SK_ANGLE
41#endif // SK_SUPPORT_GPU
42
43        kDeviceTypeCnt
44    };
45
46    static bool IsGpuDeviceType(DeviceType devType) {
47    #if SK_SUPPORT_GPU
48        switch (devType) {
49            case kGPU_DeviceType:
50    #if SK_ANGLE
51            case kANGLE_DeviceType:
52    #endif // SK_ANGLE
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 SkBaseDevice 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 SkSurface* createSurface(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    SkSurface* createSurface() SK_OVERRIDE {
103        SkSurface* surface = NULL;
104        if (fDevManager) {
105            surface = fDevManager->createSurface(fDeviceType, this);
106        }
107        if (NULL == surface) {
108            surface = this->INHERITED::createSurface();
109        }
110        return surface;
111    }
112
113    void draw(SkCanvas*) SK_OVERRIDE;
114
115    void setDeviceType(DeviceType type);
116    void toggleRendering();
117    void toggleSlideshow();
118    void toggleFPS();
119    void showOverview();
120    void toggleDistanceFieldFonts();
121
122    GrContext* getGrContext() const { return fDevManager->getGrContext(); }
123
124    void setZoomCenter(float x, float y);
125    void changeZoomLevel(float delta);
126    bool nextSample();
127    bool previousSample();
128    bool goToSample(int i);
129    SkString getSampleTitle(int i);
130    int  sampleCount();
131    bool handleTouch(int ownerId, float x, float y,
132            SkView::Click::State state);
133    void saveToPdf();
134    void postInvalDelay();
135
136    DeviceType getDeviceType() const { return fDeviceType; }
137
138protected:
139    void onDraw(SkCanvas* canvas) SK_OVERRIDE;
140    bool onHandleKey(SkKey key) SK_OVERRIDE;
141    bool onHandleChar(SkUnichar) SK_OVERRIDE;
142    void onSizeChange() SK_OVERRIDE;
143
144    SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE;
145    void afterChildren(SkCanvas*) SK_OVERRIDE;
146    void beforeChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
147    void afterChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
148
149    bool onEvent(const SkEvent& evt) SK_OVERRIDE;
150    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    bool onClick(Click* click) SK_OVERRIDE;
155    virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
156                                      unsigned modi) SK_OVERRIDE;
157
158private:
159    class DefaultDeviceManager;
160
161    int fCurrIndex;
162
163    SkPictureRecorder fRecorder;
164    SkPath fClipPath;
165
166    SkTouchGesture fGesture;
167    SkScalar fZoomLevel;
168    SkScalar fZoomScale;
169
170    DeviceType fDeviceType;
171    DeviceManager* fDevManager;
172
173    bool fSaveToPdf;
174    SkAutoTUnref<SkDocument> fPDFDocument;
175
176    bool fUseClip;
177    bool fNClip;
178    bool fAnimating;
179    bool fRotate;
180    bool fPerspAnim;
181    bool fRequestGrabImage;
182    bool fMeasureFPS;
183    SkMSec fMeasureFPS_Time;
184    SkMSec fMeasureFPS_StartTime;
185    bool fMagnify;
186    int fTilingMode;
187
188
189    SkOSMenu::TriState fPipeState;  // Mixed uses a tiled pipe
190                                    // On uses a normal pipe
191                                    // Off uses no pipe
192    int  fUsePipeMenuItemID;
193
194    // The following are for the 'fatbits' drawing
195    // Latest position of the mouse.
196    int fMouseX, fMouseY;
197    int fFatBitsScale;
198    // Used by the text showing position and color values.
199    SkTypeface* fTypeface;
200    bool fShowZoomer;
201
202    SkOSMenu::TriState fLCDState;
203    SkOSMenu::TriState fAAState;
204    SkOSMenu::TriState fSubpixelState;
205    int fHintingState;
206    int fFilterLevelIndex;
207    unsigned   fFlipAxis;
208
209    int fMSAASampleCount;
210
211    int fScrollTestX, fScrollTestY;
212    SkScalar fZoomCenterX, fZoomCenterY;
213
214    //Stores global settings
215    SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
216    //Stores slide specific settings
217    SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
218
219    int fTransitionNext;
220    int fTransitionPrev;
221
222    void loadView(SkView*);
223    void updateTitle();
224    bool getRawTitle(SkString*);
225
226    bool zoomIn();
227    bool zoomOut();
228    void updatePointer(int x, int y);
229    void magnify(SkCanvas* canvas);
230    void showZoomer(SkCanvas* canvas);
231    void updateMatrix();
232    void postAnimatingEvent();
233    void installDrawFilter(SkCanvas*);
234    int findByTitle(const char*);
235    void listTitles();
236    SkSize tileSize() const;
237    bool sendAnimatePulse();
238
239    typedef SkOSWindow INHERITED;
240};
241
242#endif
243