blob: 6ddb5c5a0d34a50522115c83cd8540eb9e78e509 [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"
reed54dc4872016-09-13 08:09:45 -070016#include "SkStream.h"
jvanverth38c72152016-10-10 07:39:38 -070017#include "SkSurface.h"
Scroggo2c8208f2011-06-15 16:49:08 +000018#include "SkTDArray.h"
19#include "SkTouchGesture.h"
20#include "SkWindow.h"
jvanverth4d0d81a2015-08-11 11:07:07 -070021#include "timer/Timer.h"
Scroggo2c8208f2011-06-15 16:49:08 +000022
reed54dc4872016-09-13 08:09:45 -070023#include "SkPipe.h"
24
csmartdalton008b9d82017-02-22 12:00:42 -070025#if SK_SUPPORT_GPU
26#include "GrContextOptions.h"
27#endif
28
Scroggo2c8208f2011-06-15 16:49:08 +000029class GrContext;
reed@google.com29038ed2011-07-06 17:56:47 +000030class GrRenderTarget;
Scroggo2c8208f2011-06-15 16:49:08 +000031
Scroggo2c8208f2011-06-15 16:49:08 +000032class SkCanvas;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000033class SkData;
reedddb5eca2014-10-08 11:10:51 -070034class SkDocument;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000035class SkEvent;
Scroggo2c8208f2011-06-15 16:49:08 +000036class SkTypeface;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000037class SkViewFactory;
Scroggo2c8208f2011-06-15 16:49:08 +000038
Scroggo2c8208f2011-06-15 16:49:08 +000039class SampleWindow : public SkOSWindow {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000040 SkTDArray<const SkViewFactory*> fSamples;
Scroggo2c8208f2011-06-15 16:49:08 +000041public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000042 enum DeviceType {
43 kRaster_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000044#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +000045 kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000046#if SK_ANGLE
47 kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000048#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000049#endif // SK_SUPPORT_GPU
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000050 kDeviceTypeCnt
bsalomon@google.com098e96d2011-07-14 14:30:46 +000051 };
bsalomon@google.com82502e22013-01-24 20:47:18 +000052
53 static bool IsGpuDeviceType(DeviceType devType) {
54 #if SK_SUPPORT_GPU
55 switch (devType) {
56 case kGPU_DeviceType:
57 #if SK_ANGLE
58 case kANGLE_DeviceType:
59 #endif // SK_ANGLE
bsalomon@google.com82502e22013-01-24 20:47:18 +000060 return true;
reed@google.com58c0aaa2013-01-24 22:09:06 +000061 default:
62 return false;
bsalomon@google.com82502e22013-01-24 20:47:18 +000063 }
64 #endif // SK_SUPPORT_GPU
65 return false;
66 }
67
bsalomon@google.com098e96d2011-07-14 14:30:46 +000068 /**
69 * SampleApp ports can subclass this manager class if they want to:
70 * * filter the types of devices supported
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000071 * * customize plugging of SkBaseDevice objects into an SkCanvas
bsalomon@google.com098e96d2011-07-14 14:30:46 +000072 * * customize publishing the results of draw to the OS window
73 * * manage GrContext / GrRenderTarget lifetimes
74 */
75 class DeviceManager : public SkRefCnt {
76 public:
csmartdalton008b9d82017-02-22 12:00:42 -070077 struct BackendOptions {
78#if SK_SUPPORT_GPU
79 GrContextOptions fGrContextOptions;
80 int fMSAASampleCount;
81 bool fDeepColor;
82#endif
83 };
halcanary9d524f22016-03-29 09:03:52 -070084
csmartdalton008b9d82017-02-22 12:00:42 -070085 virtual void setUpBackend(SampleWindow* win, const BackendOptions&) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000086
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000087 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000088
89 // called before drawing. should install correct device
90 // type on the canvas. Will skip drawing if returns false.
robertphillipsecf3dbe2016-07-28 15:17:34 -070091 virtual sk_sp<SkSurface> makeSurface(DeviceType dType, SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000092
93 // called after drawing, should get the results onto the
94 // screen.
95 virtual void publishCanvas(DeviceType dType,
96 SkCanvas* canvas,
97 SampleWindow* win) = 0;
98
99 // called when window changes size, guaranteed to be called
100 // at least once before first draw (after init)
101 virtual void windowSizeChanged(SampleWindow* win) = 0;
102
halcanary96fcdcc2015-08-27 07:41:13 -0700103 // return the GrContext backing gpu devices (nullptr if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000104 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +0000105
halcanary96fcdcc2015-08-27 07:41:13 -0700106 // return the GrRenderTarget backing gpu devices (nullptr if not built with GPU support)
robertphillipsecf3dbe2016-07-28 15:17:34 -0700107 virtual int numColorSamples() const = 0;
brianosman2d1ee792016-05-05 12:24:31 -0700108
109 // return the color depth of the output device
110 virtual int getColorBits() = 0;
111
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000112 private:
113 typedef SkRefCnt INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000114 };
115
116 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Brian Salomond3b65972017-03-22 12:05:03 -0400117 ~SampleWindow() override;
Scroggo2c8208f2011-06-15 16:49:08 +0000118
robertphillipsecf3dbe2016-07-28 15:17:34 -0700119 sk_sp<SkSurface> makeSurface() override {
120 sk_sp<SkSurface> surface;
reed@google.com5957f472012-10-01 20:31:56 +0000121 if (fDevManager) {
robertphillipsecf3dbe2016-07-28 15:17:34 -0700122 surface = fDevManager->makeSurface(fDeviceType, this);
reed@google.com5957f472012-10-01 20:31:56 +0000123 }
robertphillipsecf3dbe2016-07-28 15:17:34 -0700124 if (!surface) {
125 surface = this->INHERITED::makeSurface();
reed@google.com5957f472012-10-01 20:31:56 +0000126 }
reed0397e9f2014-09-18 11:29:01 -0700127 return surface;
reed@google.com5957f472012-10-01 20:31:56 +0000128 }
129
mtklein36352bf2015-03-25 18:17:31 -0700130 void draw(SkCanvas*) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000131
yangsu@google.com921091f2011-08-02 13:39:12 +0000132 void setDeviceType(DeviceType type);
brianosmanb109b8c2016-06-16 13:03:24 -0700133 void setDeviceColorType(SkColorType, sk_sp<SkColorSpace>);
Scroggo2c8208f2011-06-15 16:49:08 +0000134 void toggleRendering();
135 void toggleSlideshow();
136 void toggleFPS();
Yuqian Li5458cf82017-05-24 09:58:07 -0400137 void resetFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000138 void showOverview();
reed4302ae92014-10-06 12:29:56 -0700139 void toggleDistanceFieldFonts();
bungeman5a59a422016-03-31 08:30:38 -0700140 void setPixelGeometry(int pixelGeometryIndex);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000141
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000142 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000143
Scroggo2c8208f2011-06-15 16:49:08 +0000144 void setZoomCenter(float x, float y);
145 void changeZoomLevel(float delta);
bungemance560262016-03-03 13:32:38 -0800146 void changeOffset(SkVector delta);
Scroggo2c8208f2011-06-15 16:49:08 +0000147 bool nextSample();
148 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000149 bool goToSample(int i);
150 SkString getSampleTitle(int i);
151 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000152 bool handleTouch(int ownerId, float x, float y,
153 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000154 void saveToPdf();
Scroggo62b65b02011-06-21 16:01:26 +0000155 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000156
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000157 DeviceType getDeviceType() const { return fDeviceType; }
brianosman35a02a82016-09-29 14:37:02 -0700158 int getColorConfigIndex() const { return fColorConfigIndex; }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000159
Yuqian Li70898af2017-06-05 13:36:32 -0400160 int getTiles() const { return fTiles; }
161 void setTiles(int tiles) { fTiles = tiles; }
Yuqian Lib55dd552017-04-03 10:52:48 -0400162 int getThreads() const { return fThreads; }
163 void setThreads(int threads) { fThreads = threads; }
164
Scroggo2c8208f2011-06-15 16:49:08 +0000165protected:
mtklein36352bf2015-03-25 18:17:31 -0700166 void onDraw(SkCanvas* canvas) override;
167 bool onHandleKey(SkKey key) override;
168 bool onHandleChar(SkUnichar) override;
169 void onSizeChange() override;
Scroggo2c8208f2011-06-15 16:49:08 +0000170
mtklein36352bf2015-03-25 18:17:31 -0700171 SkCanvas* beforeChildren(SkCanvas*) override;
172 void afterChildren(SkCanvas*) override;
173 void beforeChild(SkView* child, SkCanvas* canvas) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000174
mtklein36352bf2015-03-25 18:17:31 -0700175 bool onEvent(const SkEvent& evt) override;
176 bool onQuery(SkEvent* evt) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000177
reed@google.com4d5c26d2013-01-08 16:17:50 +0000178 virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
mtklein36352bf2015-03-25 18:17:31 -0700179 unsigned modi) override;
180 bool onClick(Click* click) override;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000181 virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700182 unsigned modi) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000183
184private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000185 class DefaultDeviceManager;
186
Scroggo2c8208f2011-06-15 16:49:08 +0000187 int fCurrIndex;
188
reed54dc4872016-09-13 08:09:45 -0700189 std::unique_ptr<SkDynamicMemoryWStream> fPipeStream;
190 SkPipeSerializer fPipeSerializer;
191 SkPipeDeserializer fPipeDeserializer;
192
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000193 SkPictureRecorder fRecorder;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400194 std::unique_ptr<SkCanvas> fFlagsFilterCanvas;
Scroggo2c8208f2011-06-15 16:49:08 +0000195 SkPath fClipPath;
196
197 SkTouchGesture fGesture;
198 SkScalar fZoomLevel;
199 SkScalar fZoomScale;
bungemance560262016-03-03 13:32:38 -0800200 SkVector fOffset;
Scroggo2c8208f2011-06-15 16:49:08 +0000201
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000202 DeviceType fDeviceType;
203 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000204
Scroggo8ac0d542011-06-21 14:44:57 +0000205 bool fSaveToPdf;
reed093b4e82015-08-04 18:46:38 -0700206 bool fSaveToSKP;
halcanary676ab682016-05-03 12:10:04 -0700207 sk_sp<SkDocument> fPDFDocument;
Scroggo8ac0d542011-06-21 14:44:57 +0000208
Scroggo2c8208f2011-06-15 16:49:08 +0000209 bool fUseClip;
reed91b4dfb2016-01-08 08:23:19 -0800210 bool fUsePicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000211 bool fAnimating;
212 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000213 bool fPerspAnim;
Scroggo2c8208f2011-06-15 16:49:08 +0000214 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000215 bool fMeasureFPS;
reedbabc3de2016-07-08 08:43:27 -0700216 bool fUseDeferredCanvas;
jvanverth4d0d81a2015-08-11 11:07:07 -0700217 WallTimer fTimer;
218 double fMeasureFPS_Time;
Yuqian Li5458cf82017-05-24 09:58:07 -0400219 double fCumulativeFPS_Time;
220 int fCumulativeFPS_Count;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000221 bool fMagnify;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000222 int fTilingMode;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000223
Scroggo2c8208f2011-06-15 16:49:08 +0000224 // The following are for the 'fatbits' drawing
225 // Latest position of the mouse.
226 int fMouseX, fMouseY;
227 int fFatBitsScale;
228 // Used by the text showing position and color values.
bungeman13b9c952016-05-12 10:09:30 -0700229 sk_sp<SkTypeface> fTypeface;
Scroggo2c8208f2011-06-15 16:49:08 +0000230 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000231
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000232 SkOSMenu::TriState fLCDState;
233 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000234 SkOSMenu::TriState fSubpixelState;
235 int fHintingState;
bungeman5a59a422016-03-31 08:30:38 -0700236 int fPixelGeometryIndex;
reed93a12152015-03-16 10:08:34 -0700237 int fFilterQualityIndex;
Scroggo2c8208f2011-06-15 16:49:08 +0000238 unsigned fFlipAxis;
239
csmartdalton008b9d82017-02-22 12:00:42 -0700240 DeviceManager::BackendOptions fBackendOptions;
241
brianosman35a02a82016-09-29 14:37:02 -0700242 int fColorConfigIndex;
bsalomon@google.com11959252012-04-06 20:13:38 +0000243
Scroggo2c8208f2011-06-15 16:49:08 +0000244 SkScalar fZoomCenterX, fZoomCenterY;
245
yangsu@google.com921091f2011-08-02 13:39:12 +0000246 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000247 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000248 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000249 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
250
Yuqian Li70898af2017-06-05 13:36:32 -0400251 int fTiles = 0;
Yuqian Lib55dd552017-04-03 10:52:48 -0400252 int fThreads = 0;
253
Scroggo2c8208f2011-06-15 16:49:08 +0000254 void loadView(SkView*);
255 void updateTitle();
reedddb5eca2014-10-08 11:10:51 -0700256 bool getRawTitle(SkString*);
Scroggo2c8208f2011-06-15 16:49:08 +0000257
Scroggo2c8208f2011-06-15 16:49:08 +0000258 bool zoomIn();
259 bool zoomOut();
260 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000261 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000262 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000263 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000264 void postAnimatingEvent();
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000265 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000266 void listTitles();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000267 SkSize tileSize() const;
reedd9adfe62015-02-01 19:01:04 -0800268 bool sendAnimatePulse();
Scroggo2c8208f2011-06-15 16:49:08 +0000269
Scroggo2c8208f2011-06-15 16:49:08 +0000270 typedef SkOSWindow INHERITED;
271};
272
273#endif