blob: 3980669e312ca59cfd54fdb7cda21b718a10c165 [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"
robertphillips@google.com84b18c72014-04-13 19:09:42 +000013#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000014#include "SkPictureRecorder.h"
Scroggo2c8208f2011-06-15 16:49:08 +000015#include "SkScalar.h"
16#include "SkTDArray.h"
17#include "SkTouchGesture.h"
18#include "SkWindow.h"
19
20class GrContext;
reed@google.com29038ed2011-07-06 17:56:47 +000021class GrRenderTarget;
Scroggo2c8208f2011-06-15 16:49:08 +000022
Scroggo2c8208f2011-06-15 16:49:08 +000023class SkCanvas;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000024class SkData;
reedddb5eca2014-10-08 11:10:51 -070025class SkDocument;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000026class SkEvent;
Scroggo2c8208f2011-06-15 16:49:08 +000027class SkTypeface;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000028class SkViewFactory;
Scroggo2c8208f2011-06-15 16:49:08 +000029
Scroggo2c8208f2011-06-15 16:49:08 +000030class SampleWindow : public SkOSWindow {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000031 SkTDArray<const SkViewFactory*> fSamples;
Scroggo2c8208f2011-06-15 16:49:08 +000032public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000033 enum DeviceType {
34 kRaster_DeviceType,
35 kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000036#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +000037 kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000038#if SK_ANGLE
39 kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000040#endif // SK_ANGLE
41 kNullGPU_DeviceType,
42#endif // SK_SUPPORT_GPU
43
44 kDeviceTypeCnt
bsalomon@google.com098e96d2011-07-14 14:30:46 +000045 };
bsalomon@google.com82502e22013-01-24 20:47:18 +000046
47 static bool IsGpuDeviceType(DeviceType devType) {
48 #if SK_SUPPORT_GPU
49 switch (devType) {
50 case kGPU_DeviceType:
51 #if SK_ANGLE
52 case kANGLE_DeviceType:
53 #endif // SK_ANGLE
54 case kNullGPU_DeviceType:
55 return true;
reed@google.com58c0aaa2013-01-24 22:09:06 +000056 default:
57 return false;
bsalomon@google.com82502e22013-01-24 20:47:18 +000058 }
59 #endif // SK_SUPPORT_GPU
60 return false;
61 }
62
bsalomon@google.com098e96d2011-07-14 14:30:46 +000063 /**
64 * SampleApp ports can subclass this manager class if they want to:
65 * * filter the types of devices supported
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000066 * * customize plugging of SkBaseDevice objects into an SkCanvas
bsalomon@google.com098e96d2011-07-14 14:30:46 +000067 * * customize publishing the results of draw to the OS window
68 * * manage GrContext / GrRenderTarget lifetimes
69 */
70 class DeviceManager : public SkRefCnt {
71 public:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000072 SK_DECLARE_INST_COUNT(DeviceManager)
73
bsalomon@google.com11959252012-04-06 20:13:38 +000074 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000075
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000076 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000077
78 // called before drawing. should install correct device
79 // type on the canvas. Will skip drawing if returns false.
reed0397e9f2014-09-18 11:29:01 -070080 virtual SkSurface* createSurface(DeviceType dType, SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000081
82 // called after drawing, should get the results onto the
83 // screen.
84 virtual void publishCanvas(DeviceType dType,
85 SkCanvas* canvas,
86 SampleWindow* win) = 0;
87
88 // called when window changes size, guaranteed to be called
89 // at least once before first draw (after init)
90 virtual void windowSizeChanged(SampleWindow* win) = 0;
91
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000092 // return the GrContext backing gpu devices (NULL if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000093 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000094
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000095 // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support)
bsalomon@google.com11959252012-04-06 20:13:38 +000096 virtual GrRenderTarget* getGrRenderTarget() = 0;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000097 private:
98 typedef SkRefCnt INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000099 };
100
101 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +0000102 virtual ~SampleWindow();
103
reed0397e9f2014-09-18 11:29:01 -0700104 virtual SkSurface* createSurface() SK_OVERRIDE {
105 SkSurface* surface = NULL;
reed@google.com5957f472012-10-01 20:31:56 +0000106 if (fDevManager) {
reed0397e9f2014-09-18 11:29:01 -0700107 surface = fDevManager->createSurface(fDeviceType, this);
reed@google.com5957f472012-10-01 20:31:56 +0000108 }
reed0397e9f2014-09-18 11:29:01 -0700109 if (NULL == surface) {
110 surface = this->INHERITED::createSurface();
reed@google.com5957f472012-10-01 20:31:56 +0000111 }
reed0397e9f2014-09-18 11:29:01 -0700112 return surface;
reed@google.com5957f472012-10-01 20:31:56 +0000113 }
114
reed0397e9f2014-09-18 11:29:01 -0700115 virtual void draw(SkCanvas*) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000116
yangsu@google.com921091f2011-08-02 13:39:12 +0000117 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +0000118 void toggleRendering();
119 void toggleSlideshow();
120 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000121 void showOverview();
reed4302ae92014-10-06 12:29:56 -0700122 void toggleDistanceFieldFonts();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000123
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000124 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000125
Scroggo2c8208f2011-06-15 16:49:08 +0000126 void setZoomCenter(float x, float y);
127 void changeZoomLevel(float delta);
128 bool nextSample();
129 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000130 bool goToSample(int i);
131 SkString getSampleTitle(int i);
132 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000133 bool handleTouch(int ownerId, float x, float y,
134 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000135 void saveToPdf();
Scroggo62b65b02011-06-21 16:01:26 +0000136 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000137
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000138 DeviceType getDeviceType() const { return fDeviceType; }
139
Scroggo2c8208f2011-06-15 16:49:08 +0000140protected:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000141 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE;
142 virtual bool onHandleKey(SkKey key) SK_OVERRIDE;
143 virtual bool onHandleChar(SkUnichar) SK_OVERRIDE;
144 virtual void onSizeChange() SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000145
reed@google.com4d5c26d2013-01-08 16:17:50 +0000146 virtual SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE;
147 virtual void afterChildren(SkCanvas*) SK_OVERRIDE;
148 virtual void beforeChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
149 virtual void afterChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000150
reed@google.com4d5c26d2013-01-08 16:17:50 +0000151 virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE;
152 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000153
reed@google.com4d5c26d2013-01-08 16:17:50 +0000154 virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
155 unsigned modi) SK_OVERRIDE;
156 virtual bool onClick(Click* click) SK_OVERRIDE;
157 virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
158 unsigned modi) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000159
160private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000161 class DefaultDeviceManager;
162
Scroggo2c8208f2011-06-15 16:49:08 +0000163 int fCurrIndex;
164
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000165 SkPictureRecorder fRecorder;
Scroggo2c8208f2011-06-15 16:49:08 +0000166 SkPath fClipPath;
167
168 SkTouchGesture fGesture;
169 SkScalar fZoomLevel;
170 SkScalar fZoomScale;
171
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000172 DeviceType fDeviceType;
173 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000174
Scroggo8ac0d542011-06-21 14:44:57 +0000175 bool fSaveToPdf;
reedddb5eca2014-10-08 11:10:51 -0700176 SkAutoTUnref<SkDocument> fPDFDocument;
Scroggo8ac0d542011-06-21 14:44:57 +0000177
Scroggo2c8208f2011-06-15 16:49:08 +0000178 bool fUseClip;
179 bool fNClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000180 bool fAnimating;
181 bool fRotate;
bungeman@google.comb1785912013-07-09 21:58:56 +0000182 SkScalar fRotateAnimTime;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000183 bool fPerspAnim;
184 SkScalar fPerspAnimTime;
Scroggo2c8208f2011-06-15 16:49:08 +0000185 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000186 bool fMeasureFPS;
187 SkMSec fMeasureFPS_Time;
djsollen@google.com796763e2012-12-10 14:12:55 +0000188 SkMSec fMeasureFPS_StartTime;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000189 bool fMagnify;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000190 int fTilingMode;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000191
192
scroggo@google.comb073d922012-06-08 15:35:03 +0000193 SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe
194 // On uses a normal pipe
195 // Off uses no pipe
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000196 int fUsePipeMenuItemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000197
Scroggo2c8208f2011-06-15 16:49:08 +0000198 // The following are for the 'fatbits' drawing
199 // Latest position of the mouse.
200 int fMouseX, fMouseY;
201 int fFatBitsScale;
202 // Used by the text showing position and color values.
203 SkTypeface* fTypeface;
204 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000205
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000206 SkOSMenu::TriState fLCDState;
207 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000208 SkOSMenu::TriState fSubpixelState;
209 int fHintingState;
reed@google.com15bc13d2013-12-10 16:53:13 +0000210 int fFilterLevelIndex;
Scroggo2c8208f2011-06-15 16:49:08 +0000211 unsigned fFlipAxis;
212
bsalomon@google.com11959252012-04-06 20:13:38 +0000213 int fMSAASampleCount;
214
Scroggo2c8208f2011-06-15 16:49:08 +0000215 int fScrollTestX, fScrollTestY;
216 SkScalar fZoomCenterX, fZoomCenterY;
217
yangsu@google.com921091f2011-08-02 13:39:12 +0000218 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000219 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000220 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000221 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
222
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000223 int fTransitionNext;
224 int fTransitionPrev;
scroggo@google.com7dadc742012-04-18 14:07:57 +0000225
Scroggo2c8208f2011-06-15 16:49:08 +0000226 void loadView(SkView*);
227 void updateTitle();
reedddb5eca2014-10-08 11:10:51 -0700228 bool getRawTitle(SkString*);
Scroggo2c8208f2011-06-15 16:49:08 +0000229
Scroggo2c8208f2011-06-15 16:49:08 +0000230 bool zoomIn();
231 bool zoomOut();
232 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000233 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000234 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000235 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000236 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000237 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000238 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000239 void listTitles();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000240 SkSize tileSize() const;
Scroggo2c8208f2011-06-15 16:49:08 +0000241
Scroggo2c8208f2011-06-15 16:49:08 +0000242 typedef SkOSWindow INHERITED;
243};
244
245#endif