Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Skia |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
tfarina@chromium.org | f726a1c | 2012-09-29 12:40:30 +0000 | [diff] [blame] | 8 | #ifndef SampleApp_DEFINED |
| 9 | #define SampleApp_DEFINED |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
tfarina@chromium.org | f726a1c | 2012-09-29 12:40:30 +0000 | [diff] [blame] | 11 | #include "SkOSMenu.h" |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 12 | #include "SkPath.h" |
| 13 | #include "SkScalar.h" |
| 14 | #include "SkTDArray.h" |
| 15 | #include "SkTouchGesture.h" |
| 16 | #include "SkWindow.h" |
| 17 | |
| 18 | class GrContext; |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 19 | class GrRenderTarget; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 20 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 21 | class SkCanvas; |
tfarina@chromium.org | f726a1c | 2012-09-29 12:40:30 +0000 | [diff] [blame] | 22 | class SkData; |
| 23 | class SkEvent; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 24 | class SkPicture; |
| 25 | class SkTypeface; |
tfarina@chromium.org | f726a1c | 2012-09-29 12:40:30 +0000 | [diff] [blame] | 26 | class SkViewFactory; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 27 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 28 | class SampleWindow : public SkOSWindow { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 29 | SkTDArray<const SkViewFactory*> fSamples; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 30 | public: |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 31 | enum DeviceType { |
| 32 | kRaster_DeviceType, |
| 33 | kPicture_DeviceType, |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 34 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 7491372 | 2011-10-27 20:44:19 +0000 | [diff] [blame] | 35 | kGPU_DeviceType, |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 36 | #if SK_ANGLE |
| 37 | kANGLE_DeviceType, |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 38 | #endif // SK_ANGLE |
| 39 | kNullGPU_DeviceType, |
| 40 | #endif // SK_SUPPORT_GPU |
| 41 | |
| 42 | kDeviceTypeCnt |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 43 | }; |
bsalomon@google.com | 82502e2 | 2013-01-24 20:47:18 +0000 | [diff] [blame] | 44 | |
| 45 | static bool IsGpuDeviceType(DeviceType devType) { |
| 46 | #if SK_SUPPORT_GPU |
| 47 | switch (devType) { |
| 48 | case kGPU_DeviceType: |
| 49 | #if SK_ANGLE |
| 50 | case kANGLE_DeviceType: |
| 51 | #endif // SK_ANGLE |
| 52 | case kNullGPU_DeviceType: |
| 53 | return true; |
reed@google.com | 58c0aaa | 2013-01-24 22:09:06 +0000 | [diff] [blame] | 54 | default: |
| 55 | return false; |
bsalomon@google.com | 82502e2 | 2013-01-24 20:47:18 +0000 | [diff] [blame] | 56 | } |
| 57 | #endif // SK_SUPPORT_GPU |
| 58 | return false; |
| 59 | } |
| 60 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 61 | /** |
| 62 | * SampleApp ports can subclass this manager class if they want to: |
| 63 | * * filter the types of devices supported |
robertphillips@google.com | 9b051a3 | 2013-08-20 20:06:40 +0000 | [diff] [blame] | 64 | * * customize plugging of SkDevice objects into an SkCanvas |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 65 | * * customize publishing the results of draw to the OS window |
| 66 | * * manage GrContext / GrRenderTarget lifetimes |
| 67 | */ |
| 68 | class DeviceManager : public SkRefCnt { |
| 69 | public: |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 70 | SK_DECLARE_INST_COUNT(DeviceManager) |
| 71 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 72 | virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 73 | |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 74 | virtual void tearDownBackend(SampleWindow* win) = 0; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 75 | |
| 76 | // called before drawing. should install correct device |
| 77 | // type on the canvas. Will skip drawing if returns false. |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 78 | virtual SkCanvas* createCanvas(DeviceType dType, SampleWindow* win) = 0; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 79 | |
| 80 | // called after drawing, should get the results onto the |
| 81 | // screen. |
| 82 | virtual void publishCanvas(DeviceType dType, |
| 83 | SkCanvas* canvas, |
| 84 | SampleWindow* win) = 0; |
| 85 | |
| 86 | // called when window changes size, guaranteed to be called |
| 87 | // at least once before first draw (after init) |
| 88 | virtual void windowSizeChanged(SampleWindow* win) = 0; |
| 89 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 90 | // return the GrContext backing gpu devices (NULL if not built with GPU support) |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 91 | virtual GrContext* getGrContext() = 0; |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 92 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 93 | // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support) |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 94 | virtual GrRenderTarget* getGrRenderTarget() = 0; |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 95 | private: |
| 96 | typedef SkRefCnt INHERITED; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 100 | virtual ~SampleWindow(); |
| 101 | |
reed@google.com | 5957f47 | 2012-10-01 20:31:56 +0000 | [diff] [blame] | 102 | virtual SkCanvas* createCanvas() SK_OVERRIDE { |
| 103 | SkCanvas* canvas = NULL; |
| 104 | if (fDevManager) { |
| 105 | canvas = fDevManager->createCanvas(fDeviceType, this); |
| 106 | } |
| 107 | if (NULL == canvas) { |
| 108 | canvas = this->INHERITED::createCanvas(); |
| 109 | } |
| 110 | return canvas; |
| 111 | } |
| 112 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 113 | virtual void draw(SkCanvas* canvas); |
| 114 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 115 | void setDeviceType(DeviceType type); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 116 | void toggleRendering(); |
| 117 | void toggleSlideshow(); |
| 118 | void toggleFPS(); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 119 | void showOverview(); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 120 | |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 121 | GrContext* getGrContext() const { return fDevManager->getGrContext(); } |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 122 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 123 | void setZoomCenter(float x, float y); |
| 124 | void changeZoomLevel(float delta); |
| 125 | bool nextSample(); |
| 126 | bool previousSample(); |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 127 | bool goToSample(int i); |
| 128 | SkString getSampleTitle(int i); |
| 129 | int sampleCount(); |
Scroggo | a54e2f6 | 2011-06-17 12:46:17 +0000 | [diff] [blame] | 130 | bool handleTouch(int ownerId, float x, float y, |
| 131 | SkView::Click::State state); |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 132 | void saveToPdf(); |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 133 | SkData* getPDFData() { return fPDFData; } |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 134 | void postInvalDelay(); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 135 | |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 136 | DeviceType getDeviceType() const { return fDeviceType; } |
| 137 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 138 | protected: |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 139 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE; |
| 140 | virtual bool onHandleKey(SkKey key) SK_OVERRIDE; |
| 141 | virtual bool onHandleChar(SkUnichar) SK_OVERRIDE; |
| 142 | virtual void onSizeChange() SK_OVERRIDE; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 143 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 144 | virtual SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE; |
| 145 | virtual void afterChildren(SkCanvas*) SK_OVERRIDE; |
| 146 | virtual void beforeChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE; |
| 147 | virtual void afterChild(SkView* child, SkCanvas* canvas) SK_OVERRIDE; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 148 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 149 | virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE; |
| 150 | virtual bool onQuery(SkEvent* evt) SK_OVERRIDE; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 151 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 152 | virtual bool onDispatchClick(int x, int y, Click::State, void* owner, |
| 153 | unsigned modi) SK_OVERRIDE; |
| 154 | virtual bool onClick(Click* click) SK_OVERRIDE; |
| 155 | virtual Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 156 | unsigned modi) SK_OVERRIDE; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 157 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 158 | void registerPictFileSamples(char** argv, int argc); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 159 | void registerPictFileSample(char** argv, int argc); |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 160 | |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 161 | #ifdef SAMPLE_PDF_FILE_VIEWER |
| 162 | void registerPdfFileViewerSamples(char** argv, int argc); |
| 163 | #endif // SAMPLE_PDF_FILE_VIEWER |
| 164 | |
| 165 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 166 | private: |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 167 | class DefaultDeviceManager; |
| 168 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 169 | int fCurrIndex; |
| 170 | |
| 171 | SkPicture* fPicture; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 172 | SkPath fClipPath; |
| 173 | |
| 174 | SkTouchGesture fGesture; |
| 175 | SkScalar fZoomLevel; |
| 176 | SkScalar fZoomScale; |
| 177 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 178 | DeviceType fDeviceType; |
| 179 | DeviceManager* fDevManager; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 180 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 181 | bool fSaveToPdf; |
| 182 | SkCanvas* fPdfCanvas; |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 183 | SkData* fPDFData; |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 184 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 185 | bool fUseClip; |
| 186 | bool fNClip; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 187 | bool fAnimating; |
| 188 | bool fRotate; |
bungeman@google.com | b178591 | 2013-07-09 21:58:56 +0000 | [diff] [blame] | 189 | SkScalar fRotateAnimTime; |
bsalomon@google.com | e8f0910 | 2011-09-08 18:48:12 +0000 | [diff] [blame] | 190 | bool fPerspAnim; |
| 191 | SkScalar fPerspAnimTime; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 192 | bool fRequestGrabImage; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 193 | bool fMeasureFPS; |
| 194 | SkMSec fMeasureFPS_Time; |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 195 | SkMSec fMeasureFPS_StartTime; |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 196 | bool fMagnify; |
scroggo@google.com | 85cade0 | 2012-08-17 13:29:50 +0000 | [diff] [blame] | 197 | SkISize fTileCount; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 198 | |
| 199 | |
scroggo@google.com | b073d92 | 2012-06-08 15:35:03 +0000 | [diff] [blame] | 200 | SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe |
| 201 | // On uses a normal pipe |
| 202 | // Off uses no pipe |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 203 | int fUsePipeMenuItemID; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 204 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 205 | // The following are for the 'fatbits' drawing |
| 206 | // Latest position of the mouse. |
| 207 | int fMouseX, fMouseY; |
| 208 | int fFatBitsScale; |
| 209 | // Used by the text showing position and color values. |
| 210 | SkTypeface* fTypeface; |
| 211 | bool fShowZoomer; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 212 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 213 | SkOSMenu::TriState fLCDState; |
| 214 | SkOSMenu::TriState fAAState; |
| 215 | SkOSMenu::TriState fFilterState; |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 216 | SkOSMenu::TriState fSubpixelState; |
| 217 | int fHintingState; |
reed@google.com | 67b89ee | 2012-08-15 14:41:58 +0000 | [diff] [blame] | 218 | SkOSMenu::TriState fTilingState; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 219 | unsigned fFlipAxis; |
| 220 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 221 | int fMSAASampleCount; |
| 222 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 223 | int fScrollTestX, fScrollTestY; |
| 224 | SkScalar fZoomCenterX, fZoomCenterY; |
| 225 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 226 | //Stores global settings |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 227 | SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 228 | //Stores slide specific settings |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 229 | SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu |
| 230 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 231 | int fTransitionNext; |
| 232 | int fTransitionPrev; |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 233 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 234 | void loadView(SkView*); |
| 235 | void updateTitle(); |
| 236 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 237 | bool zoomIn(); |
| 238 | bool zoomOut(); |
| 239 | void updatePointer(int x, int y); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 240 | void magnify(SkCanvas* canvas); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 241 | void showZoomer(SkCanvas* canvas); |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 242 | void updateMatrix(); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 243 | void postAnimatingEvent(); |
reed@google.com | e23f194 | 2011-12-08 19:36:00 +0000 | [diff] [blame] | 244 | void installDrawFilter(SkCanvas*); |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 245 | int findByTitle(const char*); |
robertphillips@google.com | 7265e72 | 2012-05-03 18:22:28 +0000 | [diff] [blame] | 246 | void listTitles(); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 247 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 248 | typedef SkOSWindow INHERITED; |
| 249 | }; |
| 250 | |
| 251 | #endif |