SampleApp.h revision 36352bf5e38f45a70ee4f4fc132a38048d38206d
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() 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*) 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) override;
140    bool onHandleKey(SkKey key) override;
141    bool onHandleChar(SkUnichar) override;
142    void onSizeChange() override;
143
144    SkCanvas* beforeChildren(SkCanvas*) override;
145    void afterChildren(SkCanvas*) override;
146    void beforeChild(SkView* child, SkCanvas* canvas) override;
147    void afterChild(SkView* child, SkCanvas* canvas) override;
148
149    bool onEvent(const SkEvent& evt) override;
150    bool onQuery(SkEvent* evt) override;
151
152    virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
153                                 unsigned modi) override;
154    bool onClick(Click* click) override;
155    virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
156                                      unsigned modi) 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 fAnimating;
178    bool fRotate;
179    bool fPerspAnim;
180    bool fRequestGrabImage;
181    bool fMeasureFPS;
182    SkMSec fMeasureFPS_Time;
183    SkMSec fMeasureFPS_StartTime;
184    bool fMagnify;
185    int fTilingMode;
186
187
188    SkOSMenu::TriState fPipeState;  // Mixed uses a tiled pipe
189                                    // On uses a normal pipe
190                                    // Off uses no pipe
191    int  fUsePipeMenuItemID;
192
193    // The following are for the 'fatbits' drawing
194    // Latest position of the mouse.
195    int fMouseX, fMouseY;
196    int fFatBitsScale;
197    // Used by the text showing position and color values.
198    SkTypeface* fTypeface;
199    bool fShowZoomer;
200
201    SkOSMenu::TriState fLCDState;
202    SkOSMenu::TriState fAAState;
203    SkOSMenu::TriState fSubpixelState;
204    int fHintingState;
205    int fFilterQualityIndex;
206    unsigned   fFlipAxis;
207
208    int fMSAASampleCount;
209
210    int fScrollTestX, fScrollTestY;
211    SkScalar fZoomCenterX, fZoomCenterY;
212
213    //Stores global settings
214    SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
215    //Stores slide specific settings
216    SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
217
218    int fTransitionNext;
219    int fTransitionPrev;
220
221    void loadView(SkView*);
222    void updateTitle();
223    bool getRawTitle(SkString*);
224
225    bool zoomIn();
226    bool zoomOut();
227    void updatePointer(int x, int y);
228    void magnify(SkCanvas* canvas);
229    void showZoomer(SkCanvas* canvas);
230    void updateMatrix();
231    void postAnimatingEvent();
232    void installDrawFilter(SkCanvas*);
233    int findByTitle(const char*);
234    void listTitles();
235    SkSize tileSize() const;
236    bool sendAnimatePulse();
237
238    typedef SkOSWindow INHERITED;
239};
240
241#endif
242