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