blob: 3011f5f75743c5982dd2097428ad885c6eb2ee8e [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
Scroggo2c8208f2011-06-15 16:49:08 +000025class GrContext;
reed@google.com29038ed2011-07-06 17:56:47 +000026class GrRenderTarget;
Scroggo2c8208f2011-06-15 16:49:08 +000027
Scroggo2c8208f2011-06-15 16:49:08 +000028class SkCanvas;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000029class SkData;
reedddb5eca2014-10-08 11:10:51 -070030class SkDocument;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000031class SkEvent;
Scroggo2c8208f2011-06-15 16:49:08 +000032class SkTypeface;
tfarina@chromium.orgf726a1c2012-09-29 12:40:30 +000033class SkViewFactory;
Scroggo2c8208f2011-06-15 16:49:08 +000034
Scroggo2c8208f2011-06-15 16:49:08 +000035class SampleWindow : public SkOSWindow {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000036 SkTDArray<const SkViewFactory*> fSamples;
Scroggo2c8208f2011-06-15 16:49:08 +000037public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000038 enum DeviceType {
39 kRaster_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000040#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +000041 kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000042#if SK_ANGLE
43 kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000044#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000045#endif // SK_SUPPORT_GPU
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000046 kDeviceTypeCnt
bsalomon@google.com098e96d2011-07-14 14:30:46 +000047 };
bsalomon@google.com82502e22013-01-24 20:47:18 +000048
49 static bool IsGpuDeviceType(DeviceType devType) {
50 #if SK_SUPPORT_GPU
51 switch (devType) {
52 case kGPU_DeviceType:
53 #if SK_ANGLE
54 case kANGLE_DeviceType:
55 #endif // SK_ANGLE
bsalomon@google.com82502e22013-01-24 20:47:18 +000056 return true;
reed@google.com58c0aaa2013-01-24 22:09:06 +000057 default:
58 return false;
bsalomon@google.com82502e22013-01-24 20:47:18 +000059 }
60 #endif // SK_SUPPORT_GPU
61 return false;
62 }
63
bsalomon@google.com098e96d2011-07-14 14:30:46 +000064 /**
65 * SampleApp ports can subclass this manager class if they want to:
66 * * filter the types of devices supported
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000067 * * customize plugging of SkBaseDevice objects into an SkCanvas
bsalomon@google.com098e96d2011-07-14 14:30:46 +000068 * * customize publishing the results of draw to the OS window
69 * * manage GrContext / GrRenderTarget lifetimes
70 */
71 class DeviceManager : public SkRefCnt {
72 public:
halcanary9d524f22016-03-29 09:03:52 -070073
robertphillips@google.coma22e2112012-08-16 14:58:06 +000074
brianosman2d1ee792016-05-05 12:24:31 -070075 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount, bool deepColor) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000076
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000077 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000078
79 // called before drawing. should install correct device
80 // type on the canvas. Will skip drawing if returns false.
robertphillipsecf3dbe2016-07-28 15:17:34 -070081 virtual sk_sp<SkSurface> makeSurface(DeviceType dType, SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000082
83 // called after drawing, should get the results onto the
84 // screen.
85 virtual void publishCanvas(DeviceType dType,
86 SkCanvas* canvas,
87 SampleWindow* win) = 0;
88
89 // called when window changes size, guaranteed to be called
90 // at least once before first draw (after init)
91 virtual void windowSizeChanged(SampleWindow* win) = 0;
92
halcanary96fcdcc2015-08-27 07:41:13 -070093 // return the GrContext backing gpu devices (nullptr if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000094 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000095
halcanary96fcdcc2015-08-27 07:41:13 -070096 // return the GrRenderTarget backing gpu devices (nullptr if not built with GPU support)
robertphillipsecf3dbe2016-07-28 15:17:34 -070097 virtual int numColorSamples() const = 0;
brianosman2d1ee792016-05-05 12:24:31 -070098
99 // return the color depth of the output device
100 virtual int getColorBits() = 0;
101
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000102 private:
103 typedef SkRefCnt INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000104 };
105
106 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +0000107 virtual ~SampleWindow();
108
robertphillipsecf3dbe2016-07-28 15:17:34 -0700109 sk_sp<SkSurface> makeSurface() override {
110 sk_sp<SkSurface> surface;
reed@google.com5957f472012-10-01 20:31:56 +0000111 if (fDevManager) {
robertphillipsecf3dbe2016-07-28 15:17:34 -0700112 surface = fDevManager->makeSurface(fDeviceType, this);
reed@google.com5957f472012-10-01 20:31:56 +0000113 }
robertphillipsecf3dbe2016-07-28 15:17:34 -0700114 if (!surface) {
115 surface = this->INHERITED::makeSurface();
reed@google.com5957f472012-10-01 20:31:56 +0000116 }
reed0397e9f2014-09-18 11:29:01 -0700117 return surface;
reed@google.com5957f472012-10-01 20:31:56 +0000118 }
119
mtklein36352bf2015-03-25 18:17:31 -0700120 void draw(SkCanvas*) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000121
yangsu@google.com921091f2011-08-02 13:39:12 +0000122 void setDeviceType(DeviceType type);
brianosmanb109b8c2016-06-16 13:03:24 -0700123 void setDeviceColorType(SkColorType, sk_sp<SkColorSpace>);
Scroggo2c8208f2011-06-15 16:49:08 +0000124 void toggleRendering();
125 void toggleSlideshow();
126 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000127 void showOverview();
reed4302ae92014-10-06 12:29:56 -0700128 void toggleDistanceFieldFonts();
bungeman5a59a422016-03-31 08:30:38 -0700129 void setPixelGeometry(int pixelGeometryIndex);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000130
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000131 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000132
Scroggo2c8208f2011-06-15 16:49:08 +0000133 void setZoomCenter(float x, float y);
134 void changeZoomLevel(float delta);
bungemance560262016-03-03 13:32:38 -0800135 void changeOffset(SkVector delta);
Scroggo2c8208f2011-06-15 16:49:08 +0000136 bool nextSample();
137 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000138 bool goToSample(int i);
139 SkString getSampleTitle(int i);
140 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000141 bool handleTouch(int ownerId, float x, float y,
142 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000143 void saveToPdf();
Scroggo62b65b02011-06-21 16:01:26 +0000144 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000145
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000146 DeviceType getDeviceType() const { return fDeviceType; }
brianosman35a02a82016-09-29 14:37:02 -0700147 int getColorConfigIndex() const { return fColorConfigIndex; }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000148
Scroggo2c8208f2011-06-15 16:49:08 +0000149protected:
mtklein36352bf2015-03-25 18:17:31 -0700150 void onDraw(SkCanvas* canvas) override;
151 bool onHandleKey(SkKey key) override;
152 bool onHandleChar(SkUnichar) override;
153 void onSizeChange() override;
Scroggo2c8208f2011-06-15 16:49:08 +0000154
mtklein36352bf2015-03-25 18:17:31 -0700155 SkCanvas* beforeChildren(SkCanvas*) override;
156 void afterChildren(SkCanvas*) override;
157 void beforeChild(SkView* child, SkCanvas* canvas) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000158
mtklein36352bf2015-03-25 18:17:31 -0700159 bool onEvent(const SkEvent& evt) override;
160 bool onQuery(SkEvent* evt) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000161
reed@google.com4d5c26d2013-01-08 16:17:50 +0000162 virtual bool onDispatchClick(int x, int y, Click::State, void* owner,
mtklein36352bf2015-03-25 18:17:31 -0700163 unsigned modi) override;
164 bool onClick(Click* click) override;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000165 virtual Click* onFindClickHandler(SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700166 unsigned modi) override;
Scroggo2c8208f2011-06-15 16:49:08 +0000167
168private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000169 class DefaultDeviceManager;
170
Scroggo2c8208f2011-06-15 16:49:08 +0000171 int fCurrIndex;
172
reed54dc4872016-09-13 08:09:45 -0700173 std::unique_ptr<SkDynamicMemoryWStream> fPipeStream;
174 SkPipeSerializer fPipeSerializer;
175 SkPipeDeserializer fPipeDeserializer;
176
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000177 SkPictureRecorder fRecorder;
fmalita822ace92015-06-15 07:07:32 -0700178 SkAutoTDelete<SkCanvas> fFlagsFilterCanvas;
Scroggo2c8208f2011-06-15 16:49:08 +0000179 SkPath fClipPath;
180
181 SkTouchGesture fGesture;
182 SkScalar fZoomLevel;
183 SkScalar fZoomScale;
bungemance560262016-03-03 13:32:38 -0800184 SkVector fOffset;
Scroggo2c8208f2011-06-15 16:49:08 +0000185
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000186 DeviceType fDeviceType;
187 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000188
Scroggo8ac0d542011-06-21 14:44:57 +0000189 bool fSaveToPdf;
reed093b4e82015-08-04 18:46:38 -0700190 bool fSaveToSKP;
halcanary676ab682016-05-03 12:10:04 -0700191 sk_sp<SkDocument> fPDFDocument;
Scroggo8ac0d542011-06-21 14:44:57 +0000192
Scroggo2c8208f2011-06-15 16:49:08 +0000193 bool fUseClip;
reed91b4dfb2016-01-08 08:23:19 -0800194 bool fUsePicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000195 bool fAnimating;
196 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000197 bool fPerspAnim;
Scroggo2c8208f2011-06-15 16:49:08 +0000198 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000199 bool fMeasureFPS;
reedbabc3de2016-07-08 08:43:27 -0700200 bool fUseDeferredCanvas;
jvanverth4d0d81a2015-08-11 11:07:07 -0700201 WallTimer fTimer;
202 double fMeasureFPS_Time;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000203 bool fMagnify;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000204 int fTilingMode;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000205
Scroggo2c8208f2011-06-15 16:49:08 +0000206 // The following are for the 'fatbits' drawing
207 // Latest position of the mouse.
208 int fMouseX, fMouseY;
209 int fFatBitsScale;
210 // Used by the text showing position and color values.
bungeman13b9c952016-05-12 10:09:30 -0700211 sk_sp<SkTypeface> fTypeface;
Scroggo2c8208f2011-06-15 16:49:08 +0000212 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000213
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000214 SkOSMenu::TriState fLCDState;
215 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000216 SkOSMenu::TriState fSubpixelState;
217 int fHintingState;
bungeman5a59a422016-03-31 08:30:38 -0700218 int fPixelGeometryIndex;
reed93a12152015-03-16 10:08:34 -0700219 int fFilterQualityIndex;
Scroggo2c8208f2011-06-15 16:49:08 +0000220 unsigned fFlipAxis;
221
bsalomon@google.com11959252012-04-06 20:13:38 +0000222 int fMSAASampleCount;
brianosman2d1ee792016-05-05 12:24:31 -0700223 bool fDeepColor;
brianosman35a02a82016-09-29 14:37:02 -0700224 int fColorConfigIndex;
bsalomon@google.com11959252012-04-06 20:13:38 +0000225
Scroggo2c8208f2011-06-15 16:49:08 +0000226 SkScalar fZoomCenterX, fZoomCenterY;
227
yangsu@google.com921091f2011-08-02 13:39:12 +0000228 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000229 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000230 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000231 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
232
Scroggo2c8208f2011-06-15 16:49:08 +0000233 void loadView(SkView*);
234 void updateTitle();
reedddb5eca2014-10-08 11:10:51 -0700235 bool getRawTitle(SkString*);
Scroggo2c8208f2011-06-15 16:49:08 +0000236
Scroggo2c8208f2011-06-15 16:49:08 +0000237 bool zoomIn();
238 bool zoomOut();
239 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000240 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000241 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000242 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000243 void postAnimatingEvent();
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000244 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000245 void listTitles();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000246 SkSize tileSize() const;
reedd9adfe62015-02-01 19:01:04 -0800247 bool sendAnimatePulse();
Scroggo2c8208f2011-06-15 16:49:08 +0000248
Scroggo2c8208f2011-06-15 16:49:08 +0000249 typedef SkOSWindow INHERITED;
250};
251
252#endif