SampleApp.h revision 5a59a422970eb63dd5af6baa797b25fba9dec5bb
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 SkDocument;
27class SkEvent;
28class SkTypeface;
29class SkViewFactory;
30
31class SampleWindow : public SkOSWindow {
32    SkTDArray<const SkViewFactory*> fSamples;
33public:
34    enum DeviceType {
35        kRaster_DeviceType,
36#if SK_SUPPORT_GPU
37        kGPU_DeviceType,
38#if SK_ANGLE
39        kANGLE_DeviceType,
40#endif // SK_ANGLE
41#if SK_COMMAND_BUFFER
42        kCommandBuffer_DeviceType,
43#endif // SK_COMMAND_BUFFER
44#endif // SK_SUPPORT_GPU
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    #if SK_COMMAND_BUFFER
56            case kCommandBuffer_DeviceType:
57    #endif // SK_COMMAND_BUFFER
58                return true;
59            default:
60                return false;
61        }
62    #endif // SK_SUPPORT_GPU
63        return false;
64    }
65
66    /**
67     * SampleApp ports can subclass this manager class if they want to:
68     *      * filter the types of devices supported
69     *      * customize plugging of SkBaseDevice objects into an SkCanvas
70     *      * customize publishing the results of draw to the OS window
71     *      * manage GrContext / GrRenderTarget lifetimes
72     */
73    class DeviceManager : public SkRefCnt {
74    public:
75
76
77        virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
78
79        virtual void tearDownBackend(SampleWindow* win) = 0;
80
81        // called before drawing. should install correct device
82        // type on the canvas. Will skip drawing if returns false.
83        virtual SkSurface* createSurface(DeviceType dType, SampleWindow* win) = 0;
84
85        // called after drawing, should get the results onto the
86        // screen.
87        virtual void publishCanvas(DeviceType dType,
88                                   SkCanvas* canvas,
89                                   SampleWindow* win) = 0;
90
91        // called when window changes size, guaranteed to be called
92        // at least once before first draw (after init)
93        virtual void windowSizeChanged(SampleWindow* win) = 0;
94
95        // return the GrContext backing gpu devices (nullptr if not built with GPU support)
96        virtual GrContext* getGrContext() = 0;
97
98        // return the GrRenderTarget backing gpu devices (nullptr if not built with GPU support)
99        virtual GrRenderTarget* getGrRenderTarget() = 0;
100    private:
101        typedef SkRefCnt INHERITED;
102    };
103
104    SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
105    virtual ~SampleWindow();
106
107    SkSurface* createSurface() override {
108        SkSurface* surface = nullptr;
109        if (fDevManager) {
110            surface = fDevManager->createSurface(fDeviceType, this);
111        }
112        if (nullptr == surface) {
113            surface = this->INHERITED::createSurface();
114        }
115        return surface;
116    }
117
118    void draw(SkCanvas*) override;
119
120    void setDeviceType(DeviceType type);
121    void setDeviceColorType(SkColorType, SkColorProfileType);
122    void toggleRendering();
123    void toggleSlideshow();
124    void toggleFPS();
125    void showOverview();
126    void toggleDistanceFieldFonts();
127    void setPixelGeometry(int pixelGeometryIndex);
128
129    GrContext* getGrContext() const { return fDevManager->getGrContext(); }
130
131    void setZoomCenter(float x, float y);
132    void changeZoomLevel(float delta);
133    void changeOffset(SkVector delta);
134    bool nextSample();
135    bool previousSample();
136    bool goToSample(int i);
137    SkString getSampleTitle(int i);
138    int  sampleCount();
139    bool handleTouch(int ownerId, float x, float y,
140            SkView::Click::State state);
141    void saveToPdf();
142    void postInvalDelay();
143
144    DeviceType getDeviceType() const { return fDeviceType; }
145
146protected:
147    void onDraw(SkCanvas* canvas) override;
148    bool onHandleKey(SkKey key) override;
149    bool onHandleChar(SkUnichar) override;
150    void onSizeChange() override;
151
152    SkCanvas* beforeChildren(SkCanvas*) override;
153    void afterChildren(SkCanvas*) override;
154    void beforeChild(SkView* child, SkCanvas* canvas) override;
155
156    bool onEvent(const SkEvent& evt) override;
157    bool onQuery(SkEvent* evt) override;
158
159    virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
160                                 unsigned modi) override;
161    bool onClick(Click* click) override;
162    virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
163                                      unsigned modi) override;
164
165private:
166    class DefaultDeviceManager;
167
168    int fCurrIndex;
169
170    SkPictureRecorder fRecorder;
171    SkAutoTDelete<SkCanvas> fFlagsFilterCanvas;
172    SkPath fClipPath;
173
174    SkTouchGesture fGesture;
175    SkScalar fZoomLevel;
176    SkScalar fZoomScale;
177    SkVector fOffset;
178
179    DeviceType fDeviceType;
180    DeviceManager* fDevManager;
181
182    bool fSaveToPdf;
183    bool fSaveToSKP;
184    SkAutoTUnref<SkDocument> fPDFDocument;
185
186    bool fUseClip;
187    bool fUsePicture;
188    bool fAnimating;
189    bool fRotate;
190    bool fPerspAnim;
191    bool fRequestGrabImage;
192    bool fMeasureFPS;
193    WallTimer fTimer;
194    double fMeasureFPS_Time;
195    bool fMagnify;
196    int fTilingMode;
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 fPixelGeometryIndex;
211    int fFilterQualityIndex;
212    unsigned   fFlipAxis;
213
214    int fMSAASampleCount;
215
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