SampleApp.h revision 1195925b05ee9d666ea8a8f68fde5d8ca7e49b04
1
2/*
3 * Copyright 2011 Skia
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SampleWindow_DEFINED
11#define SampleWindow_DEFINED
12
13#include "SkWindow.h"
14
15#include "SampleCode.h"
16#include "SkPath.h"
17#include "SkScalar.h"
18#include "SkTDArray.h"
19#include "SkTouchGesture.h"
20#include "SkWindow.h"
21#include "SkOSMenu.h"
22
23class GrContext;
24class GrRenderTarget;
25
26class SkEvent;
27class SkCanvas;
28class SkPicture;
29class SkTypeface;
30class SkData;
31
32class SampleWindow : public SkOSWindow {
33    SkTDArray<const SkViewFactory*> fSamples;
34public:
35    enum DeviceType {
36        kRaster_DeviceType,
37        kPicture_DeviceType,
38        kGPU_DeviceType,
39#if SK_ANGLE
40        kANGLE_DeviceType,
41#endif
42        kNullGPU_DeviceType
43    };
44    /**
45     * SampleApp ports can subclass this manager class if they want to:
46     *      * filter the types of devices supported
47     *      * customize plugging of SkDevice objects into an SkCanvas
48     *      * customize publishing the results of draw to the OS window
49     *      * manage GrContext / GrRenderTarget lifetimes
50     */
51    class DeviceManager : public SkRefCnt {
52    public:
53        virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
54
55        virtual void tearDownBackend(SampleWindow* win) = 0;
56
57        // called before drawing. should install correct device
58        // type on the canvas. Will skip drawing if returns false.
59        virtual bool prepareCanvas(DeviceType dType,
60                                   SkCanvas* canvas,
61                                   SampleWindow* win) = 0;
62
63        // called after drawing, should get the results onto the
64        // screen.
65        virtual void publishCanvas(DeviceType dType,
66                                   SkCanvas* canvas,
67                                   SampleWindow* win) = 0;
68
69        // called when window changes size, guaranteed to be called
70        // at least once before first draw (after init)
71        virtual void windowSizeChanged(SampleWindow* win) = 0;
72
73        // return the GrContext backing gpu devices
74        virtual GrContext* getGrContext() = 0;
75
76        // return the GrRenderTarget backing gpu devices
77        virtual GrRenderTarget* getGrRenderTarget() = 0;
78    };
79
80    SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
81    virtual ~SampleWindow();
82
83    virtual void draw(SkCanvas* canvas);
84
85    void setDeviceType(DeviceType type);
86    void toggleRendering();
87    void toggleSlideshow();
88    void toggleFPS();
89    void showOverview();
90
91    GrContext* getGrContext() const { return fDevManager->getGrContext(); }
92
93    void setZoomCenter(float x, float y);
94    void changeZoomLevel(float delta);
95    bool nextSample();
96    bool previousSample();
97    bool goToSample(int i);
98    SkString getSampleTitle(int i);
99    int  sampleCount();
100    bool handleTouch(int ownerId, float x, float y,
101            SkView::Click::State state);
102    void saveToPdf();
103    SkData* getPDFData() { return fPDFData; }
104    void postInvalDelay();
105
106    DeviceType getDeviceType() const { return fDeviceType; }
107
108protected:
109    virtual void onDraw(SkCanvas* canvas);
110    virtual bool onHandleKey(SkKey key);
111    virtual bool onHandleChar(SkUnichar);
112    virtual void onSizeChange();
113
114    virtual SkCanvas* beforeChildren(SkCanvas*);
115    virtual void afterChildren(SkCanvas*);
116    virtual void beforeChild(SkView* child, SkCanvas* canvas);
117    virtual void afterChild(SkView* child, SkCanvas* canvas);
118
119    virtual bool onEvent(const SkEvent& evt);
120    virtual bool onQuery(SkEvent* evt);
121
122    virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
123    virtual bool onClick(Click* click);
124    virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
125
126private:
127    class DefaultDeviceManager;
128
129    int fCurrIndex;
130
131    SkPicture* fPicture;
132    SkPath fClipPath;
133
134    SkTouchGesture fGesture;
135    SkScalar fZoomLevel;
136    SkScalar fZoomScale;
137
138    DeviceType fDeviceType;
139    DeviceManager* fDevManager;
140
141    bool fSaveToPdf;
142    SkCanvas* fPdfCanvas;
143    SkData* fPDFData;
144
145    bool fUseClip;
146    bool fNClip;
147    bool fAnimating;
148    bool fRotate;
149    bool fPerspAnim;
150    SkScalar fPerspAnimTime;
151    bool fScale;
152    bool fRequestGrabImage;
153    bool fMeasureFPS;
154    SkMSec fMeasureFPS_Time;
155    bool fMagnify;
156
157
158    bool fUsePipe;
159    int  fUsePipeMenuItemID;
160    bool fDebugger;
161
162    // The following are for the 'fatbits' drawing
163    // Latest position of the mouse.
164    int fMouseX, fMouseY;
165    int fFatBitsScale;
166    // Used by the text showing position and color values.
167    SkTypeface* fTypeface;
168    bool fShowZoomer;
169
170    SkOSMenu::TriState fLCDState;
171    SkOSMenu::TriState fAAState;
172    SkOSMenu::TriState fFilterState;
173    SkOSMenu::TriState fHintingState;
174    unsigned   fFlipAxis;
175
176    int fMSAASampleCount;
177
178    int fScrollTestX, fScrollTestY;
179    SkScalar fZoomCenterX, fZoomCenterY;
180
181    //Stores global settings
182    SkOSMenu fAppMenu;
183    //Stores slide specific settings
184    SkOSMenu fSlideMenu;
185    int fTransitionNext;
186    int fTransitionPrev;
187
188    void loadView(SkView*);
189    void updateTitle();
190
191    bool zoomIn();
192    bool zoomOut();
193    void updatePointer(int x, int y);
194    void magnify(SkCanvas* canvas);
195    void showZoomer(SkCanvas* canvas);
196    void updateMatrix();
197    void postAnimatingEvent();
198    void installDrawFilter(SkCanvas*);
199    int findByTitle(const char*);
200
201    typedef SkOSWindow INHERITED;
202};
203
204#endif
205