blob: 61e6c5d5267240fea804404bcaf4b526ff3186f6 [file] [log] [blame]
Scroggo2c8208f2011-06-15 16:49:08 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Skia
Scroggo2c8208f2011-06-15 16:49:08 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
Scroggo2c8208f2011-06-15 16:49:08 +00006 */
7
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +00008#ifndef SampleApp_DEFINED
9#define SampleApp_DEFINED
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000011#include "SkOSMenu.h"
Scroggo2c8208f2011-06-15 16:49:08 +000012#include "SkPath.h"
13#include "SkScalar.h"
14#include "SkTDArray.h"
15#include "SkTouchGesture.h"
16#include "SkWindow.h"
17
18class GrContext;
reed@google.com29038ed2011-07-06 17:56:47 +000019class GrRenderTarget;
Scroggo2c8208f2011-06-15 16:49:08 +000020
Scroggo2c8208f2011-06-15 16:49:08 +000021class SkCanvas;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000022class SkData;
23class SkEvent;
Scroggo2c8208f2011-06-15 16:49:08 +000024class SkPicture;
25class SkTypeface;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000026class SkViewFactory;
Scroggo2c8208f2011-06-15 16:49:08 +000027
Scroggo2c8208f2011-06-15 16:49:08 +000028class SampleWindow : public SkOSWindow {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000029 SkTDArray<const SkViewFactory*> fSamples;
Scroggo2c8208f2011-06-15 16:49:08 +000030public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000031 enum DeviceType {
32 kRaster_DeviceType,
33 kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000034#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +000035 kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000036#if SK_ANGLE
37 kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000038#endif // SK_ANGLE
39 kNullGPU_DeviceType,
40#endif // SK_SUPPORT_GPU
41
42 kDeviceTypeCnt
bsalomon@google.com098e96d2011-07-14 14:30:46 +000043 };
bsalomon@google.com82502e22013-01-24 20:47:18 +000044
45 static bool IsGpuDeviceType(DeviceType devType) {
46 #if SK_SUPPORT_GPU
47 switch (devType) {
48 case kGPU_DeviceType:
49 #if SK_ANGLE
50 case kANGLE_DeviceType:
51 #endif // SK_ANGLE
52 case kNullGPU_DeviceType:
53 return true;
reed@google.com58c0aaa2013-01-24 22:09:06 +000054 default:
55 return false;
bsalomon@google.com82502e22013-01-24 20:47:18 +000056 }
57 #endif // SK_SUPPORT_GPU
58 return false;
59 }
60
bsalomon@google.com098e96d2011-07-14 14:30:46 +000061 /**
62 * SampleApp ports can subclass this manager class if they want to:
63 * * filter the types of devices supported
robertphillips@google.com9b051a32013-08-20 20:06:40 +000064 * * customize plugging of SkDevice objects into an SkCanvas
bsalomon@google.com098e96d2011-07-14 14:30:46 +000065 * * customize publishing the results of draw to the OS window
66 * * manage GrContext / GrRenderTarget lifetimes
67 */
68 class DeviceManager : public SkRefCnt {
69 public:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000070 SK_DECLARE_INST_COUNT(DeviceManager)
71
bsalomon@google.com11959252012-04-06 20:13:38 +000072 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000073
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000074 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000075
76 // called before drawing. should install correct device
77 // type on the canvas. Will skip drawing if returns false.
reed@google.com5957f472012-10-01 20:31:56 +000078 virtual SkCanvas* createCanvas(DeviceType dType, SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000079
80 // called after drawing, should get the results onto the
81 // screen.
82 virtual void publishCanvas(DeviceType dType,
83 SkCanvas* canvas,
84 SampleWindow* win) = 0;
85
86 // called when window changes size, guaranteed to be called
87 // at least once before first draw (after init)
88 virtual void windowSizeChanged(SampleWindow* win) = 0;
89
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000090 // return the GrContext backing gpu devices (NULL if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000091 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000092
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000093 // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support)
bsalomon@google.com11959252012-04-06 20:13:38 +000094 virtual GrRenderTarget* getGrRenderTarget() = 0;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000095 private:
96 typedef SkRefCnt INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000097 };
98
99 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +0000100 virtual ~SampleWindow();
101
reed@google.com5957f472012-10-01 20:31:56 +0000102 virtual SkCanvas* createCanvas() SK_OVERRIDE {
103 SkCanvas* canvas = NULL;
104 if (fDevManager) {
105 canvas = fDevManager->createCanvas(fDeviceType, this);
106 }
107 if (NULL == canvas) {
108 canvas = this->INHERITED::createCanvas();
109 }
110 return canvas;
111 }
112
Scroggo2c8208f2011-06-15 16:49:08 +0000113 virtual void draw(SkCanvas* canvas);
114
yangsu@google.com921091f2011-08-02 13:39:12 +0000115 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +0000116 void toggleRendering();
117 void toggleSlideshow();
118 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000119 void showOverview();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000120
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000121 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000122
Scroggo2c8208f2011-06-15 16:49:08 +0000123 void setZoomCenter(float x, float y);
124 void changeZoomLevel(float delta);
125 bool nextSample();
126 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000127 bool goToSample(int i);
128 SkString getSampleTitle(int i);
129 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000130 bool handleTouch(int ownerId, float x, float y,
131 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000132 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000133 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000134 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000135
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000136 DeviceType getDeviceType() const { return fDeviceType; }
137
Scroggo2c8208f2011-06-15 16:49:08 +0000138protected:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000139 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE;
140 virtual bool onHandleKey(SkKey key) SK_OVERRIDE;
141 virtual bool onHandleChar(SkUnichar) SK_OVERRIDE;
142 virtual void onSizeChange() SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000143
reed@google.com4d5c26d2013-01-08 16:17:50 +0000144 virtual SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE;
145 virtual void afterChildren(SkCanvas*) SK_OVERRIDE;
146 virtual void beforeChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
147 virtual void afterChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000148
reed@google.com4d5c26d2013-01-08 16:17:50 +0000149 virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE;
150 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000151
reed@google.com4d5c26d2013-01-08 16:17:50 +0000152 virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
153 unsigned modi) SK_OVERRIDE;
154 virtual bool onClick(Click* click) SK_OVERRIDE;
155 virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
156 unsigned modi) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000157
reed@google.com1830c7a2012-06-04 12:05:43 +0000158 void registerPictFileSamples(char** argv, int argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000159 void registerPictFileSample(char** argv, int argc);
reed@google.com1830c7a2012-06-04 12:05:43 +0000160
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000161#ifdef SAMPLE_PDF_FILE_VIEWER
162 void registerPdfFileViewerSamples(char** argv, int argc);
163#endif // SAMPLE_PDF_FILE_VIEWER
164
165
Scroggo2c8208f2011-06-15 16:49:08 +0000166private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000167 class DefaultDeviceManager;
168
Scroggo2c8208f2011-06-15 16:49:08 +0000169 int fCurrIndex;
170
171 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000172 SkPath fClipPath;
173
174 SkTouchGesture fGesture;
175 SkScalar fZoomLevel;
176 SkScalar fZoomScale;
177
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000178 DeviceType fDeviceType;
179 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000180
Scroggo8ac0d542011-06-21 14:44:57 +0000181 bool fSaveToPdf;
182 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000183 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000184
Scroggo2c8208f2011-06-15 16:49:08 +0000185 bool fUseClip;
186 bool fNClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000187 bool fAnimating;
188 bool fRotate;
bungeman@google.comb1785912013-07-09 21:58:56 +0000189 SkScalar fRotateAnimTime;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000190 bool fPerspAnim;
191 SkScalar fPerspAnimTime;
Scroggo2c8208f2011-06-15 16:49:08 +0000192 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000193 bool fMeasureFPS;
194 SkMSec fMeasureFPS_Time;
djsollen@google.com796763e2012-12-10 14:12:55 +0000195 SkMSec fMeasureFPS_StartTime;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000196 bool fMagnify;
scroggo@google.com85cade02012-08-17 13:29:50 +0000197 SkISize fTileCount;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000198
199
scroggo@google.comb073d922012-06-08 15:35:03 +0000200 SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe
201 // On uses a normal pipe
202 // Off uses no pipe
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000203 int fUsePipeMenuItemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000204
Scroggo2c8208f2011-06-15 16:49:08 +0000205 // The following are for the 'fatbits' drawing
206 // Latest position of the mouse.
207 int fMouseX, fMouseY;
208 int fFatBitsScale;
209 // Used by the text showing position and color values.
210 SkTypeface* fTypeface;
211 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000212
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000213 SkOSMenu::TriState fLCDState;
214 SkOSMenu::TriState fAAState;
215 SkOSMenu::TriState fFilterState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000216 SkOSMenu::TriState fSubpixelState;
217 int fHintingState;
reed@google.com67b89ee2012-08-15 14:41:58 +0000218 SkOSMenu::TriState fTilingState;
Scroggo2c8208f2011-06-15 16:49:08 +0000219 unsigned fFlipAxis;
220
bsalomon@google.com11959252012-04-06 20:13:38 +0000221 int fMSAASampleCount;
222
Scroggo2c8208f2011-06-15 16:49:08 +0000223 int fScrollTestX, fScrollTestY;
224 SkScalar fZoomCenterX, fZoomCenterY;
225
yangsu@google.com921091f2011-08-02 13:39:12 +0000226 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000227 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000228 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000229 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
230
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000231 int fTransitionNext;
232 int fTransitionPrev;
scroggo@google.com7dadc742012-04-18 14:07:57 +0000233
Scroggo2c8208f2011-06-15 16:49:08 +0000234 void loadView(SkView*);
235 void updateTitle();
236
Scroggo2c8208f2011-06-15 16:49:08 +0000237 bool zoomIn();
238 bool zoomOut();
239 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000240 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000241 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000242 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000243 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000244 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000245 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000246 void listTitles();
Scroggo2c8208f2011-06-15 16:49:08 +0000247
Scroggo2c8208f2011-06-15 16:49:08 +0000248 typedef SkOSWindow INHERITED;
249};
250
251#endif