blob: 8fc1a905b1b7cba185fb8ed6148811af97aa4abd [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;
reedbeedb812015-05-05 12:14:45 -070025class SkDeferredCanvas;
reedddb5eca2014-10-08 11:10:51 -070026class SkDocument;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000027class SkEvent;
Scroggo2c8208f2011-06-15 16:49:08 +000028class SkTypeface;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000029class SkViewFactory;
Scroggo2c8208f2011-06-15 16:49:08 +000030
Scroggo2c8208f2011-06-15 16:49:08 +000031class SampleWindow : public SkOSWindow {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000032 SkTDArray<const SkViewFactory*> fSamples;
Scroggo2c8208f2011-06-15 16:49:08 +000033public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000034 enum DeviceType {
35 kRaster_DeviceType,
36 kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000037#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +000038 kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000039#if SK_ANGLE
40 kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000041#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000042#endif // SK_SUPPORT_GPU
reedbeedb812015-05-05 12:14:45 -070043 kDeferred_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000044 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
bsalomon@google.com82502e22013-01-24 20:47:18 +000054 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.
reed0397e9f2014-09-18 11:29:01 -070079 virtual SkSurface* createSurface(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
mtklein36352bf2015-03-25 18:17:31 -0700103 SkSurface* createSurface() override {
reed0397e9f2014-09-18 11:29:01 -0700104 SkSurface* surface = NULL;
reed@google.com5957f472012-10-01 20:31:56 +0000105 if (fDevManager) {
reed0397e9f2014-09-18 11:29:01 -0700106 surface = fDevManager->createSurface(fDeviceType, this);
reed@google.com5957f472012-10-01 20:31:56 +0000107 }
reed0397e9f2014-09-18 11:29:01 -0700108 if (NULL == surface) {
109 surface = this->INHERITED::createSurface();
reed@google.com5957f472012-10-01 20:31:56 +0000110 }
reed0397e9f2014-09-18 11:29:01 -0700111 return surface;
reed@google.com5957f472012-10-01 20:31:56 +0000112 }
113
mtklein36352bf2015-03-25 18:17:31 -0700114 void draw(SkCanvas*) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000115
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();
reed4302ae92014-10-06 12:29:56 -0700121 void toggleDistanceFieldFonts();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000122
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000123 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000124
Scroggo2c8208f2011-06-15 16:49:08 +0000125 void setZoomCenter(float x, float y);
126 void changeZoomLevel(float delta);
127 bool nextSample();
128 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000129 bool goToSample(int i);
130 SkString getSampleTitle(int i);
131 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000132 bool handleTouch(int ownerId, float x, float y,
133 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000134 void saveToPdf();
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:
mtklein36352bf2015-03-25 18:17:31 -0700140 void onDraw(SkCanvas* canvas) override;
141 bool onHandleKey(SkKey key) override;
142 bool onHandleChar(SkUnichar) override;
143 void onSizeChange() override;
Scroggo2c8208f2011-06-15 16:49:08 +0000144
mtklein36352bf2015-03-25 18:17:31 -0700145 SkCanvas* beforeChildren(SkCanvas*) override;
146 void afterChildren(SkCanvas*) override;
147 void beforeChild(SkView* child, SkCanvas* canvas) override;
148 void afterChild(SkView* child, SkCanvas* canvas) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000149
mtklein36352bf2015-03-25 18:17:31 -0700150 bool onEvent(const SkEvent& evt) override;
151 bool onQuery(SkEvent* evt) 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,
mtklein36352bf2015-03-25 18:17:31 -0700154 unsigned modi) override;
155 bool onClick(Click* click) override;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000156 virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700157 unsigned modi) 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;
reedbeedb812015-05-05 12:14:45 -0700165 SkAutoTDelete<SkSurface> fDeferredSurface;
166 SkAutoTDelete<SkDeferredCanvas> fDeferredCanvas;
Scroggo2c8208f2011-06-15 16:49:08 +0000167 SkPath fClipPath;
168
169 SkTouchGesture fGesture;
170 SkScalar fZoomLevel;
171 SkScalar fZoomScale;
172
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000173 DeviceType fDeviceType;
174 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000175
Scroggo8ac0d542011-06-21 14:44:57 +0000176 bool fSaveToPdf;
reedddb5eca2014-10-08 11:10:51 -0700177 SkAutoTUnref<SkDocument> fPDFDocument;
Scroggo8ac0d542011-06-21 14:44:57 +0000178
Scroggo2c8208f2011-06-15 16:49:08 +0000179 bool fUseClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000180 bool fAnimating;
181 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000182 bool fPerspAnim;
Scroggo2c8208f2011-06-15 16:49:08 +0000183 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000184 bool fMeasureFPS;
185 SkMSec fMeasureFPS_Time;
djsollen@google.com796763e2012-12-10 14:12:55 +0000186 SkMSec fMeasureFPS_StartTime;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000187 bool fMagnify;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000188 int fTilingMode;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000189
190
scroggo@google.comb073d922012-06-08 15:35:03 +0000191 SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe
192 // On uses a normal pipe
193 // Off uses no pipe
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000194 int fUsePipeMenuItemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000195
Scroggo2c8208f2011-06-15 16:49:08 +0000196 // The following are for the 'fatbits' drawing
197 // Latest position of the mouse.
198 int fMouseX, fMouseY;
199 int fFatBitsScale;
200 // Used by the text showing position and color values.
201 SkTypeface* fTypeface;
202 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000203
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000204 SkOSMenu::TriState fLCDState;
205 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000206 SkOSMenu::TriState fSubpixelState;
207 int fHintingState;
reed93a12152015-03-16 10:08:34 -0700208 int fFilterQualityIndex;
Scroggo2c8208f2011-06-15 16:49:08 +0000209 unsigned fFlipAxis;
210
bsalomon@google.com11959252012-04-06 20:13:38 +0000211 int fMSAASampleCount;
212
Scroggo2c8208f2011-06-15 16:49:08 +0000213 int fScrollTestX, fScrollTestY;
214 SkScalar fZoomCenterX, fZoomCenterY;
215
yangsu@google.com921091f2011-08-02 13:39:12 +0000216 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000217 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000218 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000219 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
220
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000221 int fTransitionNext;
222 int fTransitionPrev;
scroggo@google.com7dadc742012-04-18 14:07:57 +0000223
Scroggo2c8208f2011-06-15 16:49:08 +0000224 void loadView(SkView*);
225 void updateTitle();
reedddb5eca2014-10-08 11:10:51 -0700226 bool getRawTitle(SkString*);
Scroggo2c8208f2011-06-15 16:49:08 +0000227
Scroggo2c8208f2011-06-15 16:49:08 +0000228 bool zoomIn();
229 bool zoomOut();
230 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000231 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000232 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000233 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000234 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000235 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000236 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000237 void listTitles();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000238 SkSize tileSize() const;
reedd9adfe62015-02-01 19:01:04 -0800239 bool sendAnimatePulse();
Scroggo2c8208f2011-06-15 16:49:08 +0000240
Scroggo2c8208f2011-06-15 16:49:08 +0000241 typedef SkOSWindow INHERITED;
242};
243
244#endif