• 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 "SkStream.h"
17 #include "SkSurface.h"
18 #include "SkTDArray.h"
19 #include "SkTouchGesture.h"
20 #include "SkWindow.h"
21 #include "timer/Timer.h"
22 
23 #include "SkPipe.h"
24 
25 #if SK_SUPPORT_GPU
26 #include "GrContextOptions.h"
27 #endif
28 
29 class GrContext;
30 class GrRenderTarget;
31 
32 class SkCanvas;
33 class SkData;
34 class SkDocument;
35 class SkEvent;
36 class SkTypeface;
37 class SkViewFactory;
38 
39 class SampleWindow : public SkOSWindow {
40     SkTDArray<const SkViewFactory*> fSamples;
41 public:
42     enum DeviceType {
43         kRaster_DeviceType,
44 #if SK_SUPPORT_GPU
45         kGPU_DeviceType,
46 #if SK_ANGLE
47         kANGLE_DeviceType,
48 #endif // SK_ANGLE
49 #endif // SK_SUPPORT_GPU
50         kDeviceTypeCnt
51     };
52 
IsGpuDeviceType(DeviceType devType)53     static bool IsGpuDeviceType(DeviceType devType) {
54     #if SK_SUPPORT_GPU
55         switch (devType) {
56             case kGPU_DeviceType:
57     #if SK_ANGLE
58             case kANGLE_DeviceType:
59     #endif // SK_ANGLE
60                 return true;
61             default:
62                 return false;
63         }
64     #endif // SK_SUPPORT_GPU
65         return false;
66     }
67 
68     /**
69      * SampleApp ports can subclass this manager class if they want to:
70      *      * filter the types of devices supported
71      *      * customize plugging of SkBaseDevice objects into an SkCanvas
72      *      * customize publishing the results of draw to the OS window
73      *      * manage GrContext / GrRenderTarget lifetimes
74      */
75     class DeviceManager : public SkRefCnt {
76     public:
77         struct BackendOptions {
78 #if SK_SUPPORT_GPU
79             GrContextOptions   fGrContextOptions;
80             int                fMSAASampleCount;
81             bool               fDeepColor;
82 #endif
83         };
84 
85         virtual void setUpBackend(SampleWindow* win, const BackendOptions&) = 0;
86 
87         virtual void tearDownBackend(SampleWindow* win) = 0;
88 
89         // called before drawing. should install correct device
90         // type on the canvas. Will skip drawing if returns false.
91         virtual sk_sp<SkSurface> makeSurface(DeviceType dType, SampleWindow* win) = 0;
92 
93         // called after drawing, should get the results onto the
94         // screen.
95         virtual void publishCanvas(DeviceType dType,
96                                    SkCanvas* canvas,
97                                    SampleWindow* win) = 0;
98 
99         // called when window changes size, guaranteed to be called
100         // at least once before first draw (after init)
101         virtual void windowSizeChanged(SampleWindow* win) = 0;
102 
103         // return the GrContext backing gpu devices (nullptr if not built with GPU support)
104         virtual GrContext* getGrContext() = 0;
105 
106         // return the GrRenderTarget backing gpu devices (nullptr if not built with GPU support)
107         virtual int numColorSamples() const = 0;
108 
109         // return the color depth of the output device
110         virtual int getColorBits() = 0;
111 
112     private:
113         typedef SkRefCnt INHERITED;
114     };
115 
116     SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
117     ~SampleWindow() override;
118 
makeSurface()119     sk_sp<SkSurface> makeSurface() override {
120         sk_sp<SkSurface> surface;
121         if (fDevManager) {
122             surface = fDevManager->makeSurface(fDeviceType, this);
123         }
124         if (!surface) {
125             surface = this->INHERITED::makeSurface();
126         }
127         return surface;
128     }
129 
130     void draw(SkCanvas*) override;
131 
132     void setDeviceType(DeviceType type);
133     void setDeviceColorType(SkColorType, sk_sp<SkColorSpace>);
134     void toggleRendering();
135     void toggleSlideshow();
136     void toggleFPS();
137     void resetFPS();
138     void showOverview();
139     void toggleDistanceFieldFonts();
140     void setPixelGeometry(int pixelGeometryIndex);
141 
getGrContext()142     GrContext* getGrContext() const { return fDevManager->getGrContext(); }
143 
144     void setZoomCenter(float x, float y);
145     void changeZoomLevel(float delta);
146     void changeOffset(SkVector delta);
147     bool nextSample();
148     bool previousSample();
149     bool goToSample(int i);
150     SkString getSampleTitle(int i);
151     int  sampleCount();
152     bool handleTouch(int ownerId, float x, float y,
153             SkView::Click::State state);
154     void saveToPdf();
155     void postInvalDelay();
156 
getDeviceType()157     DeviceType getDeviceType() const { return fDeviceType; }
getColorConfigIndex()158     int getColorConfigIndex() const { return fColorConfigIndex; }
159 
getTiles()160     int getTiles() const { return fTiles; }
setTiles(int tiles)161     void setTiles(int tiles) { fTiles = tiles; }
getThreads()162     int getThreads() const { return fThreads; }
setThreads(int threads)163     void setThreads(int threads) { fThreads = threads; }
164 
165 protected:
166     void onDraw(SkCanvas* canvas) override;
167     bool onHandleKey(SkKey key) override;
168     bool onHandleChar(SkUnichar) override;
169     void onSizeChange() override;
170 
171     SkCanvas* beforeChildren(SkCanvas*) override;
172     void afterChildren(SkCanvas*) override;
173     void beforeChild(SkView* child, SkCanvas* canvas) override;
174 
175     bool onEvent(const SkEvent& evt) override;
176     bool onQuery(SkEvent* evt) override;
177 
178     virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
179                                  unsigned modi) override;
180     bool onClick(Click* click) override;
181     virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
182                                       unsigned modi) override;
183 
184 private:
185     class DefaultDeviceManager;
186 
187     int fCurrIndex;
188 
189     std::unique_ptr<SkDynamicMemoryWStream> fPipeStream;
190     SkPipeSerializer        fPipeSerializer;
191     SkPipeDeserializer      fPipeDeserializer;
192 
193     SkPictureRecorder fRecorder;
194     std::unique_ptr<SkCanvas> fFlagsFilterCanvas;
195     SkPath fClipPath;
196 
197     SkTouchGesture fGesture;
198     SkScalar fZoomLevel;
199     SkScalar fZoomScale;
200     SkVector fOffset;
201 
202     DeviceType fDeviceType;
203     DeviceManager* fDevManager;
204 
205     bool fSaveToPdf;
206     bool fSaveToSKP;
207     sk_sp<SkDocument> fPDFDocument;
208 
209     bool fUseClip;
210     bool fUsePicture;
211     bool fAnimating;
212     bool fRotate;
213     bool fPerspAnim;
214     bool fRequestGrabImage;
215     bool fMeasureFPS;
216     bool fUseDeferredCanvas;
217     WallTimer fTimer;
218     double fMeasureFPS_Time;
219     double fCumulativeFPS_Time;
220     int    fCumulativeFPS_Count;
221     bool fMagnify;
222     int fTilingMode;
223 
224     // The following are for the 'fatbits' drawing
225     // Latest position of the mouse.
226     int fMouseX, fMouseY;
227     int fFatBitsScale;
228     // Used by the text showing position and color values.
229     sk_sp<SkTypeface> fTypeface;
230     bool fShowZoomer;
231 
232     SkOSMenu::TriState fLCDState;
233     SkOSMenu::TriState fAAState;
234     SkOSMenu::TriState fSubpixelState;
235     int fHintingState;
236     int fPixelGeometryIndex;
237     int fFilterQualityIndex;
238     unsigned   fFlipAxis;
239 
240     DeviceManager::BackendOptions fBackendOptions;
241 
242     int fColorConfigIndex;
243 
244     SkScalar fZoomCenterX, fZoomCenterY;
245 
246     //Stores global settings
247     SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
248     //Stores slide specific settings
249     SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
250 
251     int fTiles = 0;
252     int fThreads = 0;
253 
254     void loadView(SkView*);
255     void updateTitle();
256     bool getRawTitle(SkString*);
257 
258     bool zoomIn();
259     bool zoomOut();
260     void updatePointer(int x, int y);
261     void magnify(SkCanvas* canvas);
262     void showZoomer(SkCanvas* canvas);
263     void updateMatrix();
264     void postAnimatingEvent();
265     int findByTitle(const char*);
266     void listTitles();
267     SkSize tileSize() const;
268     bool sendAnimatePulse();
269 
270     typedef SkOSWindow INHERITED;
271 };
272 
273 #endif
274