3980669e312ca59cfd54fdb7cda21b718a10c165
[platform/framework/web/crosswalk.git] / src / third_party / skia / samplecode / SampleApp.h
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
20 class GrContext;
21 class GrRenderTarget;
22
23 class SkCanvas;
24 class SkData;
25 class SkDocument;
26 class SkEvent;
27 class SkTypeface;
28 class SkViewFactory;
29
30 class SampleWindow : public SkOSWindow {
31     SkTDArray<const SkViewFactory*> fSamples;
32 public:
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         kNullGPU_DeviceType,
42 #endif // SK_SUPPORT_GPU
43
44         kDeviceTypeCnt
45     };
46
47     static bool IsGpuDeviceType(DeviceType devType) {
48     #if SK_SUPPORT_GPU
49         switch (devType) {
50             case kGPU_DeviceType:
51     #if SK_ANGLE
52             case kANGLE_DeviceType:
53     #endif // SK_ANGLE
54             case kNullGPU_DeviceType:
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         SK_DECLARE_INST_COUNT(DeviceManager)
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     virtual SkSurface* createSurface() SK_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     virtual void draw(SkCanvas*) SK_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
140 protected:
141     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE;
142     virtual bool onHandleKey(SkKey key) SK_OVERRIDE;
143     virtual bool onHandleChar(SkUnichar) SK_OVERRIDE;
144     virtual void onSizeChange() SK_OVERRIDE;
145
146     virtual SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE;
147     virtual void afterChildren(SkCanvas*) SK_OVERRIDE;
148     virtual void beforeChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
149     virtual void afterChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
150
151     virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE;
152     virtual bool onQuery(SkEvent* evt) SK_OVERRIDE;
153
154     virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
155                                  unsigned modi) SK_OVERRIDE;
156     virtual bool onClick(Click* click) SK_OVERRIDE;
157     virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
158                                       unsigned modi) SK_OVERRIDE;
159
160 private:
161     class DefaultDeviceManager;
162
163     int fCurrIndex;
164
165     SkPictureRecorder fRecorder;
166     SkPath fClipPath;
167
168     SkTouchGesture fGesture;
169     SkScalar fZoomLevel;
170     SkScalar fZoomScale;
171
172     DeviceType fDeviceType;
173     DeviceManager* fDevManager;
174
175     bool fSaveToPdf;
176     SkAutoTUnref<SkDocument> fPDFDocument;
177
178     bool fUseClip;
179     bool fNClip;
180     bool fAnimating;
181     bool fRotate;
182     SkScalar fRotateAnimTime;
183     bool fPerspAnim;
184     SkScalar fPerspAnimTime;
185     bool fRequestGrabImage;
186     bool fMeasureFPS;
187     SkMSec fMeasureFPS_Time;
188     SkMSec fMeasureFPS_StartTime;
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 fFilterLevelIndex;
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     int fTransitionNext;
224     int fTransitionPrev;
225
226     void loadView(SkView*);
227     void updateTitle();
228     bool getRawTitle(SkString*);
229
230     bool zoomIn();
231     bool zoomOut();
232     void updatePointer(int x, int y);
233     void magnify(SkCanvas* canvas);
234     void showZoomer(SkCanvas* canvas);
235     void updateMatrix();
236     void postAnimatingEvent();
237     void installDrawFilter(SkCanvas*);
238     int findByTitle(const char*);
239     void listTitles();
240     SkSize tileSize() const;
241
242     typedef SkOSWindow INHERITED;
243 };
244
245 #endif