• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 class GrContext;
22 class GrRenderTarget;
23 
24 class SkCanvas;
25 class SkData;
26 class SkDocument;
27 class SkEvent;
28 class SkTypeface;
29 class SkViewFactory;
30 
31 class SampleWindow : public SkOSWindow {
32     SkTDArray<const SkViewFactory*> fSamples;
33 public:
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         kCommandBufferES2_DeviceType,
43 #endif // SK_COMMAND_BUFFER
44 #endif // SK_SUPPORT_GPU
45         kDeviceTypeCnt
46     };
47 
IsGpuDeviceType(DeviceType devType)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 kCommandBufferES2_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 
createSurface()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 
getGrContext()128     GrContext* getGrContext() const { return fDevManager->getGrContext(); }
129 
130     void setZoomCenter(float x, float y);
131     void changeZoomLevel(float delta);
132     bool nextSample();
133     bool previousSample();
134     bool goToSample(int i);
135     SkString getSampleTitle(int i);
136     int  sampleCount();
137     bool handleTouch(int ownerId, float x, float y,
138             SkView::Click::State state);
139     void saveToPdf();
140     void postInvalDelay();
141 
getDeviceType()142     DeviceType getDeviceType() const { return fDeviceType; }
143 
144 protected:
145     void onDraw(SkCanvas* canvas) override;
146     bool onHandleKey(SkKey key) override;
147     bool onHandleChar(SkUnichar) override;
148     void onSizeChange() override;
149 
150     SkCanvas* beforeChildren(SkCanvas*) override;
151     void afterChildren(SkCanvas*) override;
152     void beforeChild(SkView* child, SkCanvas* canvas) override;
153 
154     bool onEvent(const SkEvent& evt) override;
155     bool onQuery(SkEvent* evt) override;
156 
157     virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
158                                  unsigned modi) override;
159     bool onClick(Click* click) override;
160     virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
161                                       unsigned modi) override;
162 
163 private:
164     class DefaultDeviceManager;
165 
166     int fCurrIndex;
167 
168     SkPictureRecorder fRecorder;
169     SkAutoTDelete<SkCanvas> fFlagsFilterCanvas;
170     SkPath fClipPath;
171 
172     SkTouchGesture fGesture;
173     SkScalar fZoomLevel;
174     SkScalar fZoomScale;
175 
176     DeviceType fDeviceType;
177     DeviceManager* fDevManager;
178 
179     bool fSaveToPdf;
180     bool fSaveToSKP;
181     SkAutoTUnref<SkDocument> fPDFDocument;
182 
183     bool fUseClip;
184     bool fUsePicture;
185     bool fAnimating;
186     bool fRotate;
187     bool fPerspAnim;
188     bool fRequestGrabImage;
189     bool fMeasureFPS;
190     WallTimer fTimer;
191     double fMeasureFPS_Time;
192     bool fMagnify;
193     int fTilingMode;
194 
195     // The following are for the 'fatbits' drawing
196     // Latest position of the mouse.
197     int fMouseX, fMouseY;
198     int fFatBitsScale;
199     // Used by the text showing position and color values.
200     SkTypeface* fTypeface;
201     bool fShowZoomer;
202 
203     SkOSMenu::TriState fLCDState;
204     SkOSMenu::TriState fAAState;
205     SkOSMenu::TriState fSubpixelState;
206     int fHintingState;
207     int fFilterQualityIndex;
208     unsigned   fFlipAxis;
209 
210     int fMSAASampleCount;
211 
212     int fScrollTestX, fScrollTestY;
213     SkScalar fZoomCenterX, fZoomCenterY;
214 
215     //Stores global settings
216     SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
217     //Stores slide specific settings
218     SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
219 
220     void loadView(SkView*);
221     void updateTitle();
222     bool getRawTitle(SkString*);
223 
224     bool zoomIn();
225     bool zoomOut();
226     void updatePointer(int x, int y);
227     void magnify(SkCanvas* canvas);
228     void showZoomer(SkCanvas* canvas);
229     void updateMatrix();
230     void postAnimatingEvent();
231     int findByTitle(const char*);
232     void listTitles();
233     SkSize tileSize() const;
234     bool sendAnimatePulse();
235 
236     typedef SkOSWindow INHERITED;
237 };
238 
239 #endif
240