blob: 85392e0c9a2fba0074707ba26efa7ec94a37dc4f [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:
122 virtual void onDraw(SkCanvas* canvas);
123 virtual bool onHandleKey(SkKey key);
124 virtual bool onHandleChar(SkUnichar);
125 virtual void onSizeChange();
126
127 virtual SkCanvas* beforeChildren(SkCanvas*);
128 virtual void afterChildren(SkCanvas*);
129 virtual void beforeChild(SkView* child, SkCanvas* canvas);
130 virtual void afterChild(SkView* child, SkCanvas* canvas);
131
132 virtual bool onEvent(const SkEvent& evt);
133 virtual bool onQuery(SkEvent* evt);
134
Scroggod3aed392011-06-22 13:26:56 +0000135 virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
Scroggo2c8208f2011-06-15 16:49:08 +0000136 virtual bool onClick(Click* click);
137 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
138
reed@google.com1830c7a2012-06-04 12:05:43 +0000139 void registerPictFileSamples(char** argv, int argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000140 void registerPictFileSample(char** argv, int argc);
reed@google.com1830c7a2012-06-04 12:05:43 +0000141
Scroggo2c8208f2011-06-15 16:49:08 +0000142private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000143 class DefaultDeviceManager;
144
Scroggo2c8208f2011-06-15 16:49:08 +0000145 int fCurrIndex;
146
147 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000148 SkPath fClipPath;
149
150 SkTouchGesture fGesture;
151 SkScalar fZoomLevel;
152 SkScalar fZoomScale;
153
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000154 DeviceType fDeviceType;
155 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000156
Scroggo8ac0d542011-06-21 14:44:57 +0000157 bool fSaveToPdf;
158 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000159 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000160
Scroggo2c8208f2011-06-15 16:49:08 +0000161 bool fUseClip;
162 bool fNClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000163 bool fAnimating;
164 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000165 bool fPerspAnim;
166 SkScalar fPerspAnimTime;
Scroggo2c8208f2011-06-15 16:49:08 +0000167 bool fScale;
168 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000169 bool fMeasureFPS;
170 SkMSec fMeasureFPS_Time;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000171 bool fMagnify;
scroggo@google.com85cade02012-08-17 13:29:50 +0000172 SkISize fTileCount;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000173
174
scroggo@google.comb073d922012-06-08 15:35:03 +0000175 SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe
176 // On uses a normal pipe
177 // Off uses no pipe
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000178 int fUsePipeMenuItemID;
179 bool fDebugger;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000180
Scroggo2c8208f2011-06-15 16:49:08 +0000181 // The following are for the 'fatbits' drawing
182 // Latest position of the mouse.
183 int fMouseX, fMouseY;
184 int fFatBitsScale;
185 // Used by the text showing position and color values.
186 SkTypeface* fTypeface;
187 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000188
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000189 SkOSMenu::TriState fLCDState;
190 SkOSMenu::TriState fAAState;
191 SkOSMenu::TriState fFilterState;
192 SkOSMenu::TriState fHintingState;
reed@google.com67b89ee2012-08-15 14:41:58 +0000193 SkOSMenu::TriState fTilingState;
Scroggo2c8208f2011-06-15 16:49:08 +0000194 unsigned fFlipAxis;
195
bsalomon@google.com11959252012-04-06 20:13:38 +0000196 int fMSAASampleCount;
197
Scroggo2c8208f2011-06-15 16:49:08 +0000198 int fScrollTestX, fScrollTestY;
199 SkScalar fZoomCenterX, fZoomCenterY;
200
yangsu@google.com921091f2011-08-02 13:39:12 +0000201 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000202 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000203 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000204 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
205
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000206 int fTransitionNext;
207 int fTransitionPrev;
scroggo@google.com7dadc742012-04-18 14:07:57 +0000208
Scroggo2c8208f2011-06-15 16:49:08 +0000209 void loadView(SkView*);
210 void updateTitle();
211
Scroggo2c8208f2011-06-15 16:49:08 +0000212 bool zoomIn();
213 bool zoomOut();
214 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000215 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000216 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000217 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000218 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000219 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000220 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000221 void listTitles();
Scroggo2c8208f2011-06-15 16:49:08 +0000222
Scroggo2c8208f2011-06-15 16:49:08 +0000223 typedef SkOSWindow INHERITED;
224};
225
226#endif