blob: ecf552ce8bdae7dbed81b2fd36e89247573b3823 [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 };
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:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000053 SK_DECLARE_INST_COUNT(DeviceManager)
54
bsalomon@google.com11959252012-04-06 20:13:38 +000055 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000056
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000057 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000058
59 // called before drawing. should install correct device
60 // type on the canvas. Will skip drawing if returns false.
reed@google.com5957f472012-10-01 20:31:56 +000061 virtual SkCanvas* createCanvas(DeviceType dType, SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000062
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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000073 // return the GrContext backing gpu devices (NULL if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000074 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000075
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000076 // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support)
bsalomon@google.com11959252012-04-06 20:13:38 +000077 virtual GrRenderTarget* getGrRenderTarget() = 0;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000078 private:
79 typedef SkRefCnt INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000080 };
81
82 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +000083 virtual ~SampleWindow();
84
reed@google.com5957f472012-10-01 20:31:56 +000085 virtual SkCanvas* createCanvas() SK_OVERRIDE {
86 SkCanvas* canvas = NULL;
87 if (fDevManager) {
88 canvas = fDevManager->createCanvas(fDeviceType, this);
89 }
90 if (NULL == canvas) {
91 canvas = this->INHERITED::createCanvas();
92 }
93 return canvas;
94 }
95
Scroggo2c8208f2011-06-15 16:49:08 +000096 virtual void draw(SkCanvas* canvas);
97
yangsu@google.com921091f2011-08-02 13:39:12 +000098 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +000099 void toggleRendering();
100 void toggleSlideshow();
101 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000102 void showOverview();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000103
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000104 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000105
Scroggo2c8208f2011-06-15 16:49:08 +0000106 void setZoomCenter(float x, float y);
107 void changeZoomLevel(float delta);
108 bool nextSample();
109 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000110 bool goToSample(int i);
111 SkString getSampleTitle(int i);
112 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000113 bool handleTouch(int ownerId, float x, float y,
114 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000115 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000116 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000117 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000118
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000119 DeviceType getDeviceType() const { return fDeviceType; }
120
Scroggo2c8208f2011-06-15 16:49:08 +0000121protected:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000122 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE;
123 virtual bool onHandleKey(SkKey key) SK_OVERRIDE;
124 virtual bool onHandleChar(SkUnichar) SK_OVERRIDE;
125 virtual void onSizeChange() SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000126
reed@google.com4d5c26d2013-01-08 16:17:50 +0000127 virtual SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE;
128 virtual void afterChildren(SkCanvas*) SK_OVERRIDE;
129 virtual void beforeChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
130 virtual void afterChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000131
reed@google.com4d5c26d2013-01-08 16:17:50 +0000132 virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE;
133 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000134
reed@google.com4d5c26d2013-01-08 16:17:50 +0000135 virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
136 unsigned modi) SK_OVERRIDE;
137 virtual bool onClick(Click* click) SK_OVERRIDE;
138 virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
139 unsigned modi) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000140
reed@google.com1830c7a2012-06-04 12:05:43 +0000141 void registerPictFileSamples(char** argv, int argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000142 void registerPictFileSample(char** argv, int argc);
reed@google.com1830c7a2012-06-04 12:05:43 +0000143
Scroggo2c8208f2011-06-15 16:49:08 +0000144private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000145 class DefaultDeviceManager;
146
Scroggo2c8208f2011-06-15 16:49:08 +0000147 int fCurrIndex;
148
149 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000150 SkPath fClipPath;
151
152 SkTouchGesture fGesture;
153 SkScalar fZoomLevel;
154 SkScalar fZoomScale;
155
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000156 DeviceType fDeviceType;
157 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000158
Scroggo8ac0d542011-06-21 14:44:57 +0000159 bool fSaveToPdf;
160 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000161 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000162
Scroggo2c8208f2011-06-15 16:49:08 +0000163 bool fUseClip;
164 bool fNClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000165 bool fAnimating;
166 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000167 bool fPerspAnim;
168 SkScalar fPerspAnimTime;
Scroggo2c8208f2011-06-15 16:49:08 +0000169 bool fScale;
170 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000171 bool fMeasureFPS;
172 SkMSec fMeasureFPS_Time;
djsollen@google.com796763e2012-12-10 14:12:55 +0000173 SkMSec fMeasureFPS_StartTime;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000174 bool fMagnify;
scroggo@google.com85cade02012-08-17 13:29:50 +0000175 SkISize fTileCount;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000176
177
scroggo@google.comb073d922012-06-08 15:35:03 +0000178 SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe
179 // On uses a normal pipe
180 // Off uses no pipe
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000181 int fUsePipeMenuItemID;
182 bool fDebugger;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000183
Scroggo2c8208f2011-06-15 16:49:08 +0000184 // The following are for the 'fatbits' drawing
185 // Latest position of the mouse.
186 int fMouseX, fMouseY;
187 int fFatBitsScale;
188 // Used by the text showing position and color values.
189 SkTypeface* fTypeface;
190 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000191
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000192 SkOSMenu::TriState fLCDState;
193 SkOSMenu::TriState fAAState;
194 SkOSMenu::TriState fFilterState;
195 SkOSMenu::TriState fHintingState;
reed@google.com67b89ee2012-08-15 14:41:58 +0000196 SkOSMenu::TriState fTilingState;
Scroggo2c8208f2011-06-15 16:49:08 +0000197 unsigned fFlipAxis;
198
bsalomon@google.com11959252012-04-06 20:13:38 +0000199 int fMSAASampleCount;
200
Scroggo2c8208f2011-06-15 16:49:08 +0000201 int fScrollTestX, fScrollTestY;
202 SkScalar fZoomCenterX, fZoomCenterY;
203
yangsu@google.com921091f2011-08-02 13:39:12 +0000204 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000205 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000206 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000207 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
208
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000209 int fTransitionNext;
210 int fTransitionPrev;
scroggo@google.com7dadc742012-04-18 14:07:57 +0000211
Scroggo2c8208f2011-06-15 16:49:08 +0000212 void loadView(SkView*);
213 void updateTitle();
214
Scroggo2c8208f2011-06-15 16:49:08 +0000215 bool zoomIn();
216 bool zoomOut();
217 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000218 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000219 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000220 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000221 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000222 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000223 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000224 void listTitles();
Scroggo2c8208f2011-06-15 16:49:08 +0000225
Scroggo2c8208f2011-06-15 16:49:08 +0000226 typedef SkOSWindow INHERITED;
227};
228
229#endif