blob: abd8ac7c67dc84bfcc2c8d2bcdea5765eebb2653 [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.
61 virtual bool prepareCanvas(DeviceType dType,
62 SkCanvas* canvas,
63 SampleWindow* win) = 0;
64
65 // called after drawing, should get the results onto the
66 // screen.
67 virtual void publishCanvas(DeviceType dType,
68 SkCanvas* canvas,
69 SampleWindow* win) = 0;
70
71 // called when window changes size, guaranteed to be called
72 // at least once before first draw (after init)
73 virtual void windowSizeChanged(SampleWindow* win) = 0;
74
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000075 // return the GrContext backing gpu devices (NULL if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000076 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000077
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000078 // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support)
bsalomon@google.com11959252012-04-06 20:13:38 +000079 virtual GrRenderTarget* getGrRenderTarget() = 0;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000080 private:
81 typedef SkRefCnt INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000082 };
83
84 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +000085 virtual ~SampleWindow();
86
87 virtual void draw(SkCanvas* canvas);
88
yangsu@google.com921091f2011-08-02 13:39:12 +000089 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +000090 void toggleRendering();
91 void toggleSlideshow();
92 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000093 void showOverview();
bsalomon@google.com098e96d2011-07-14 14:30:46 +000094
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000095 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +000096
Scroggo2c8208f2011-06-15 16:49:08 +000097 void setZoomCenter(float x, float y);
98 void changeZoomLevel(float delta);
99 bool nextSample();
100 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000101 bool goToSample(int i);
102 SkString getSampleTitle(int i);
103 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000104 bool handleTouch(int ownerId, float x, float y,
105 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000106 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000107 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000108 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000109
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000110 DeviceType getDeviceType() const { return fDeviceType; }
111
Scroggo2c8208f2011-06-15 16:49:08 +0000112protected:
113 virtual void onDraw(SkCanvas* canvas);
114 virtual bool onHandleKey(SkKey key);
115 virtual bool onHandleChar(SkUnichar);
116 virtual void onSizeChange();
117
118 virtual SkCanvas* beforeChildren(SkCanvas*);
119 virtual void afterChildren(SkCanvas*);
120 virtual void beforeChild(SkView* child, SkCanvas* canvas);
121 virtual void afterChild(SkView* child, SkCanvas* canvas);
122
123 virtual bool onEvent(const SkEvent& evt);
124 virtual bool onQuery(SkEvent* evt);
125
Scroggod3aed392011-06-22 13:26:56 +0000126 virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
Scroggo2c8208f2011-06-15 16:49:08 +0000127 virtual bool onClick(Click* click);
128 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
129
reed@google.com1830c7a2012-06-04 12:05:43 +0000130 void registerPictFileSamples(char** argv, int argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000131 void registerPictFileSample(char** argv, int argc);
reed@google.com1830c7a2012-06-04 12:05:43 +0000132
Scroggo2c8208f2011-06-15 16:49:08 +0000133private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000134 class DefaultDeviceManager;
135
Scroggo2c8208f2011-06-15 16:49:08 +0000136 int fCurrIndex;
137
138 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000139 SkPath fClipPath;
140
141 SkTouchGesture fGesture;
142 SkScalar fZoomLevel;
143 SkScalar fZoomScale;
144
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000145 DeviceType fDeviceType;
146 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000147
Scroggo8ac0d542011-06-21 14:44:57 +0000148 bool fSaveToPdf;
149 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000150 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000151
Scroggo2c8208f2011-06-15 16:49:08 +0000152 bool fUseClip;
153 bool fNClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000154 bool fAnimating;
155 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000156 bool fPerspAnim;
157 SkScalar fPerspAnimTime;
Scroggo2c8208f2011-06-15 16:49:08 +0000158 bool fScale;
159 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000160 bool fMeasureFPS;
161 SkMSec fMeasureFPS_Time;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000162 bool fMagnify;
scroggo@google.com85cade02012-08-17 13:29:50 +0000163 SkISize fTileCount;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000164
165
scroggo@google.comb073d922012-06-08 15:35:03 +0000166 SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe
167 // On uses a normal pipe
168 // Off uses no pipe
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000169 int fUsePipeMenuItemID;
170 bool fDebugger;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000171
Scroggo2c8208f2011-06-15 16:49:08 +0000172 // The following are for the 'fatbits' drawing
173 // Latest position of the mouse.
174 int fMouseX, fMouseY;
175 int fFatBitsScale;
176 // Used by the text showing position and color values.
177 SkTypeface* fTypeface;
178 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000179
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000180 SkOSMenu::TriState fLCDState;
181 SkOSMenu::TriState fAAState;
182 SkOSMenu::TriState fFilterState;
183 SkOSMenu::TriState fHintingState;
reed@google.com67b89ee2012-08-15 14:41:58 +0000184 SkOSMenu::TriState fTilingState;
Scroggo2c8208f2011-06-15 16:49:08 +0000185 unsigned fFlipAxis;
186
bsalomon@google.com11959252012-04-06 20:13:38 +0000187 int fMSAASampleCount;
188
Scroggo2c8208f2011-06-15 16:49:08 +0000189 int fScrollTestX, fScrollTestY;
190 SkScalar fZoomCenterX, fZoomCenterY;
191
yangsu@google.com921091f2011-08-02 13:39:12 +0000192 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000193 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000194 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000195 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
196
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000197 int fTransitionNext;
198 int fTransitionPrev;
scroggo@google.com7dadc742012-04-18 14:07:57 +0000199
Scroggo2c8208f2011-06-15 16:49:08 +0000200 void loadView(SkView*);
201 void updateTitle();
202
Scroggo2c8208f2011-06-15 16:49:08 +0000203 bool zoomIn();
204 bool zoomOut();
205 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000206 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000207 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000208 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000209 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000210 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000211 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000212 void listTitles();
Scroggo2c8208f2011-06-15 16:49:08 +0000213
Scroggo2c8208f2011-06-15 16:49:08 +0000214 typedef SkOSWindow INHERITED;
215};
216
217#endif