blob: e08ff8a8f4ff325fc8567fe027b0c0e57f88a6db [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;
25class SkEvent;
Scroggo2c8208f2011-06-15 16:49:08 +000026class SkTypeface;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000027class SkViewFactory;
Scroggo2c8208f2011-06-15 16:49:08 +000028
Scroggo2c8208f2011-06-15 16:49:08 +000029class SampleWindow : public SkOSWindow {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000030 SkTDArray<const SkViewFactory*> fSamples;
Scroggo2c8208f2011-06-15 16:49:08 +000031public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000032 enum DeviceType {
33 kRaster_DeviceType,
34 kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000035#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +000036 kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000037#if SK_ANGLE
38 kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000039#endif // SK_ANGLE
40 kNullGPU_DeviceType,
41#endif // SK_SUPPORT_GPU
42
43 kDeviceTypeCnt
bsalomon@google.com098e96d2011-07-14 14:30:46 +000044 };
bsalomon@google.com82502e22013-01-24 20:47:18 +000045
46 static bool IsGpuDeviceType(DeviceType devType) {
47 #if SK_SUPPORT_GPU
48 switch (devType) {
49 case kGPU_DeviceType:
50 #if SK_ANGLE
51 case kANGLE_DeviceType:
52 #endif // SK_ANGLE
53 case kNullGPU_DeviceType:
54 return true;
reed@google.com58c0aaa2013-01-24 22:09:06 +000055 default:
56 return false;
bsalomon@google.com82502e22013-01-24 20:47:18 +000057 }
58 #endif // SK_SUPPORT_GPU
59 return false;
60 }
61
bsalomon@google.com098e96d2011-07-14 14:30:46 +000062 /**
63 * SampleApp ports can subclass this manager class if they want to:
64 * * filter the types of devices supported
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000065 * * customize plugging of SkBaseDevice objects into an SkCanvas
bsalomon@google.com098e96d2011-07-14 14:30:46 +000066 * * customize publishing the results of draw to the OS window
67 * * manage GrContext / GrRenderTarget lifetimes
68 */
69 class DeviceManager : public SkRefCnt {
70 public:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000071 SK_DECLARE_INST_COUNT(DeviceManager)
72
bsalomon@google.com11959252012-04-06 20:13:38 +000073 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000074
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000075 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000076
77 // called before drawing. should install correct device
78 // type on the canvas. Will skip drawing if returns false.
reed@google.com5957f472012-10-01 20:31:56 +000079 virtual SkCanvas* createCanvas(DeviceType dType, SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000080
81 // called after drawing, should get the results onto the
82 // screen.
83 virtual void publishCanvas(DeviceType dType,
84 SkCanvas* canvas,
85 SampleWindow* win) = 0;
86
87 // called when window changes size, guaranteed to be called
88 // at least once before first draw (after init)
89 virtual void windowSizeChanged(SampleWindow* win) = 0;
90
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000091 // return the GrContext backing gpu devices (NULL if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000092 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000093
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000094 // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support)
bsalomon@google.com11959252012-04-06 20:13:38 +000095 virtual GrRenderTarget* getGrRenderTarget() = 0;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000096 private:
97 typedef SkRefCnt INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000098 };
99
100 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +0000101 virtual ~SampleWindow();
102
reed@google.com5957f472012-10-01 20:31:56 +0000103 virtual SkCanvas* createCanvas() SK_OVERRIDE {
104 SkCanvas* canvas = NULL;
105 if (fDevManager) {
106 canvas = fDevManager->createCanvas(fDeviceType, this);
107 }
108 if (NULL == canvas) {
109 canvas = this->INHERITED::createCanvas();
110 }
111 return canvas;
112 }
113
Scroggo2c8208f2011-06-15 16:49:08 +0000114 virtual void draw(SkCanvas* canvas);
115
yangsu@google.com921091f2011-08-02 13:39:12 +0000116 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +0000117 void toggleRendering();
118 void toggleSlideshow();
119 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000120 void showOverview();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000121
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000122 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000123
Scroggo2c8208f2011-06-15 16:49:08 +0000124 void setZoomCenter(float x, float y);
125 void changeZoomLevel(float delta);
126 bool nextSample();
127 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000128 bool goToSample(int i);
129 SkString getSampleTitle(int i);
130 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000131 bool handleTouch(int ownerId, float x, float y,
132 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000133 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000134 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000135 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000136
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000137 DeviceType getDeviceType() const { return fDeviceType; }
138
Scroggo2c8208f2011-06-15 16:49:08 +0000139protected:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000140 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE;
141 virtual bool onHandleKey(SkKey key) SK_OVERRIDE;
142 virtual bool onHandleChar(SkUnichar) SK_OVERRIDE;
143 virtual void onSizeChange() SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000144
reed@google.com4d5c26d2013-01-08 16:17:50 +0000145 virtual SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE;
146 virtual void afterChildren(SkCanvas*) SK_OVERRIDE;
147 virtual void beforeChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
148 virtual void afterChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000149
reed@google.com4d5c26d2013-01-08 16:17:50 +0000150 virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE;
151 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000152
reed@google.com4d5c26d2013-01-08 16:17:50 +0000153 virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
154 unsigned modi) SK_OVERRIDE;
155 virtual bool onClick(Click* click) SK_OVERRIDE;
156 virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
157 unsigned modi) SK_OVERRIDE;
Scroggo2c8208f2011-06-15 16:49:08 +0000158
159private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000160 class DefaultDeviceManager;
161
Scroggo2c8208f2011-06-15 16:49:08 +0000162 int fCurrIndex;
163
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000164 SkPictureRecorder fRecorder;
Scroggo2c8208f2011-06-15 16:49:08 +0000165 SkPath fClipPath;
166
167 SkTouchGesture fGesture;
168 SkScalar fZoomLevel;
169 SkScalar fZoomScale;
170
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000171 DeviceType fDeviceType;
172 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000173
Scroggo8ac0d542011-06-21 14:44:57 +0000174 bool fSaveToPdf;
175 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000176 SkData* fPDFData;
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();
228
Scroggo2c8208f2011-06-15 16:49:08 +0000229 bool zoomIn();
230 bool zoomOut();
231 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000232 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000233 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000234 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000235 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000236 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000237 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000238 void listTitles();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000239 SkSize tileSize() const;
Scroggo2c8208f2011-06-15 16:49:08 +0000240
Scroggo2c8208f2011-06-15 16:49:08 +0000241 typedef SkOSWindow INHERITED;
242};
243
244#endif