blob: 082208cd5a4b2507085c91132ca299c0792aa8e6 [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"
jvanverth4d0d81a2015-08-11 11:07:07 -070019#include "timer/Timer.h"
Scroggo2c8208f2011-06-15 16:49:08 +000020
21class GrContext;
reed@google.com29038ed2011-07-06 17:56:47 +000022class GrRenderTarget;
Scroggo2c8208f2011-06-15 16:49:08 +000023
Scroggo2c8208f2011-06-15 16:49:08 +000024class SkCanvas;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000025class SkData;
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,
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
hendrikw885bf092015-08-27 10:38:39 -070041#if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -080042 kCommandBuffer_DeviceType,
hendrikw885bf092015-08-27 10:38:39 -070043#endif // SK_COMMAND_BUFFER
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000044#endif // SK_SUPPORT_GPU
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000045 kDeviceTypeCnt
bsalomon@google.com098e96d2011-07-14 14:30:46 +000046 };
bsalomon@google.com82502e22013-01-24 20:47:18 +000047
48 static bool IsGpuDeviceType(DeviceType devType) {
49 #if SK_SUPPORT_GPU
50 switch (devType) {
51 case kGPU_DeviceType:
52 #if SK_ANGLE
53 case kANGLE_DeviceType:
54 #endif // SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -070055 #if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -080056 case kCommandBuffer_DeviceType:
hendrikw885bf092015-08-27 10:38:39 -070057 #endif // SK_COMMAND_BUFFER
bsalomon@google.com82502e22013-01-24 20:47:18 +000058 return true;
reed@google.com58c0aaa2013-01-24 22:09:06 +000059 default:
60 return false;
bsalomon@google.com82502e22013-01-24 20:47:18 +000061 }
62 #endif // SK_SUPPORT_GPU
63 return false;
64 }
65
bsalomon@google.com098e96d2011-07-14 14:30:46 +000066 /**
67 * SampleApp ports can subclass this manager class if they want to:
68 * * filter the types of devices supported
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000069 * * customize plugging of SkBaseDevice objects into an SkCanvas
bsalomon@google.com098e96d2011-07-14 14:30:46 +000070 * * customize publishing the results of draw to the OS window
71 * * manage GrContext / GrRenderTarget lifetimes
72 */
73 class DeviceManager : public SkRefCnt {
74 public:
halcanary9d524f22016-03-29 09:03:52 -070075
robertphillips@google.coma22e2112012-08-16 14:58:06 +000076
bsalomon@google.com11959252012-04-06 20:13:38 +000077 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000078
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000079 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000080
81 // called before drawing. should install correct device
82 // type on the canvas. Will skip drawing if returns false.
reed0397e9f2014-09-18 11:29:01 -070083 virtual SkSurface* createSurface(DeviceType dType, SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000084
85 // called after drawing, should get the results onto the
86 // screen.
87 virtual void publishCanvas(DeviceType dType,
88 SkCanvas* canvas,
89 SampleWindow* win) = 0;
90
91 // called when window changes size, guaranteed to be called
92 // at least once before first draw (after init)
93 virtual void windowSizeChanged(SampleWindow* win) = 0;
94
halcanary96fcdcc2015-08-27 07:41:13 -070095 // return the GrContext backing gpu devices (nullptr if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000096 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000097
halcanary96fcdcc2015-08-27 07:41:13 -070098 // return the GrRenderTarget backing gpu devices (nullptr if not built with GPU support)
bsalomon@google.com11959252012-04-06 20:13:38 +000099 virtual GrRenderTarget* getGrRenderTarget() = 0;
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000100 private:
101 typedef SkRefCnt INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000102 };
103
104 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +0000105 virtual ~SampleWindow();
106
mtklein36352bf2015-03-25 18:17:31 -0700107 SkSurface* createSurface() override {
halcanary96fcdcc2015-08-27 07:41:13 -0700108 SkSurface* surface = nullptr;
reed@google.com5957f472012-10-01 20:31:56 +0000109 if (fDevManager) {
reed0397e9f2014-09-18 11:29:01 -0700110 surface = fDevManager->createSurface(fDeviceType, this);
reed@google.com5957f472012-10-01 20:31:56 +0000111 }
halcanary96fcdcc2015-08-27 07:41:13 -0700112 if (nullptr == surface) {
reed0397e9f2014-09-18 11:29:01 -0700113 surface = this->INHERITED::createSurface();
reed@google.com5957f472012-10-01 20:31:56 +0000114 }
reed0397e9f2014-09-18 11:29:01 -0700115 return surface;
reed@google.com5957f472012-10-01 20:31:56 +0000116 }
117
mtklein36352bf2015-03-25 18:17:31 -0700118 void draw(SkCanvas*) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000119
yangsu@google.com921091f2011-08-02 13:39:12 +0000120 void setDeviceType(DeviceType type);
reeda34be682016-02-15 07:48:35 -0800121 void setDeviceColorType(SkColorType, SkColorProfileType);
Scroggo2c8208f2011-06-15 16:49:08 +0000122 void toggleRendering();
123 void toggleSlideshow();
124 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000125 void showOverview();
reed4302ae92014-10-06 12:29:56 -0700126 void toggleDistanceFieldFonts();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000127
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000128 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000129
Scroggo2c8208f2011-06-15 16:49:08 +0000130 void setZoomCenter(float x, float y);
131 void changeZoomLevel(float delta);
bungemance560262016-03-03 13:32:38 -0800132 void changeOffset(SkVector delta);
Scroggo2c8208f2011-06-15 16:49:08 +0000133 bool nextSample();
134 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000135 bool goToSample(int i);
136 SkString getSampleTitle(int i);
137 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000138 bool handleTouch(int ownerId, float x, float y,
139 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000140 void saveToPdf();
Scroggo62b65b02011-06-21 16:01:26 +0000141 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000142
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000143 DeviceType getDeviceType() const { return fDeviceType; }
144
Scroggo2c8208f2011-06-15 16:49:08 +0000145protected:
mtklein36352bf2015-03-25 18:17:31 -0700146 void onDraw(SkCanvas* canvas) override;
147 bool onHandleKey(SkKey key) override;
148 bool onHandleChar(SkUnichar) override;
149 void onSizeChange() override;
Scroggo2c8208f2011-06-15 16:49:08 +0000150
mtklein36352bf2015-03-25 18:17:31 -0700151 SkCanvas* beforeChildren(SkCanvas*) override;
152 void afterChildren(SkCanvas*) override;
153 void beforeChild(SkView* child, SkCanvas* canvas) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000154
mtklein36352bf2015-03-25 18:17:31 -0700155 bool onEvent(const SkEvent& evt) override;
156 bool onQuery(SkEvent* evt) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000157
reed@google.com4d5c26d2013-01-08 16:17:50 +0000158 virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
mtklein36352bf2015-03-25 18:17:31 -0700159 unsigned modi) override;
160 bool onClick(Click* click) override;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000161 virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700162 unsigned modi) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000163
164private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000165 class DefaultDeviceManager;
166
Scroggo2c8208f2011-06-15 16:49:08 +0000167 int fCurrIndex;
168
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000169 SkPictureRecorder fRecorder;
fmalita822ace92015-06-15 07:07:32 -0700170 SkAutoTDelete<SkCanvas> fFlagsFilterCanvas;
Scroggo2c8208f2011-06-15 16:49:08 +0000171 SkPath fClipPath;
172
173 SkTouchGesture fGesture;
174 SkScalar fZoomLevel;
175 SkScalar fZoomScale;
bungemance560262016-03-03 13:32:38 -0800176 SkVector fOffset;
Scroggo2c8208f2011-06-15 16:49:08 +0000177
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000178 DeviceType fDeviceType;
179 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000180
Scroggo8ac0d542011-06-21 14:44:57 +0000181 bool fSaveToPdf;
reed093b4e82015-08-04 18:46:38 -0700182 bool fSaveToSKP;
reedddb5eca2014-10-08 11:10:51 -0700183 SkAutoTUnref<SkDocument> fPDFDocument;
Scroggo8ac0d542011-06-21 14:44:57 +0000184
Scroggo2c8208f2011-06-15 16:49:08 +0000185 bool fUseClip;
reed91b4dfb2016-01-08 08:23:19 -0800186 bool fUsePicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000187 bool fAnimating;
188 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000189 bool fPerspAnim;
Scroggo2c8208f2011-06-15 16:49:08 +0000190 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000191 bool fMeasureFPS;
jvanverth4d0d81a2015-08-11 11:07:07 -0700192 WallTimer fTimer;
193 double fMeasureFPS_Time;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000194 bool fMagnify;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000195 int fTilingMode;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000196
Scroggo2c8208f2011-06-15 16:49:08 +0000197 // The following are for the 'fatbits' drawing
198 // Latest position of the mouse.
199 int fMouseX, fMouseY;
200 int fFatBitsScale;
201 // Used by the text showing position and color values.
202 SkTypeface* fTypeface;
203 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000204
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000205 SkOSMenu::TriState fLCDState;
206 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000207 SkOSMenu::TriState fSubpixelState;
208 int fHintingState;
reed93a12152015-03-16 10:08:34 -0700209 int fFilterQualityIndex;
Scroggo2c8208f2011-06-15 16:49:08 +0000210 unsigned fFlipAxis;
211
bsalomon@google.com11959252012-04-06 20:13:38 +0000212 int fMSAASampleCount;
213
Scroggo2c8208f2011-06-15 16:49:08 +0000214 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
Scroggo2c8208f2011-06-15 16:49:08 +0000221 void loadView(SkView*);
222 void updateTitle();
reedddb5eca2014-10-08 11:10:51 -0700223 bool getRawTitle(SkString*);
Scroggo2c8208f2011-06-15 16:49:08 +0000224
Scroggo2c8208f2011-06-15 16:49:08 +0000225 bool zoomIn();
226 bool zoomOut();
227 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000228 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000229 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000230 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000231 void postAnimatingEvent();
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000232 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000233 void listTitles();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000234 SkSize tileSize() const;
reedd9adfe62015-02-01 19:01:04 -0800235 bool sendAnimatePulse();
Scroggo2c8208f2011-06-15 16:49:08 +0000236
Scroggo2c8208f2011-06-15 16:49:08 +0000237 typedef SkOSWindow INHERITED;
238};
239
240#endif