blob: 1ebc927002d6628e64b1bd9a0f9041117f49105d [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
jvanverth34524262016-05-04 13:49:13 -07008#ifndef Viewer_DEFINED
9#define Viewer_DEFINED
jvanverth9f372462016-04-06 06:08:59 -070010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "gm/gm.h"
12#include "include/core/SkExecutor.h"
13#include "include/core/SkFont.h"
14#include "src/core/SkScan.h"
15#include "src/sksl/SkSLString.h"
16#include "src/sksl/ir/SkSLProgram.h"
17#include "tools/gpu/MemoryCache.h"
18#include "tools/sk_app/Application.h"
19#include "tools/sk_app/CommandSet.h"
20#include "tools/sk_app/Window.h"
Hal Canary41248072019-07-11 16:32:53 -040021#include "tools/viewer/AnimTimer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "tools/viewer/ImGuiLayer.h"
23#include "tools/viewer/Slide.h"
24#include "tools/viewer/StatsLayer.h"
25#include "tools/viewer/TouchGesture.h"
jvanverth9f372462016-04-06 06:08:59 -070026
27class SkCanvas;
Brian Osman0b8bb882019-04-12 11:47:19 -040028class SkData;
jvanverth9f372462016-04-06 06:08:59 -070029
Brian Osman80fc07e2017-12-08 16:45:43 -050030class Viewer : public sk_app::Application, sk_app::Window::Layer {
jvanverth9f372462016-04-06 06:08:59 -070031public:
jvanverth34524262016-05-04 13:49:13 -070032 Viewer(int argc, char** argv, void* platformData);
33 ~Viewer() override;
jvanverth9f372462016-04-06 06:08:59 -070034
liyuqian2edb0f42016-07-06 14:11:32 -070035 void onIdle() override;
Brian Osman80fc07e2017-12-08 16:45:43 -050036
37 void onBackendCreated() override;
Robert Phillips9882dae2019-03-04 11:00:10 -050038 void onPaint(SkSurface*) override;
Ben Wagnera1915972018-08-09 15:06:19 -040039 void onResize(int width, int height) override;
Hal Canaryff2e8fe2019-07-16 09:58:43 -040040 bool onTouch(intptr_t owner, InputState state, float x, float y) override;
41 bool onMouse(int x, int y, InputState state, ModifierKey modifiers) override;
Brian Osman80fc07e2017-12-08 16:45:43 -050042 void onUIStateChanged(const SkString& stateName, const SkString& stateValue) override;
Hal Canaryff2e8fe2019-07-16 09:58:43 -040043 bool onKey(sk_app::Window::Key key, InputState state, ModifierKey modifiers) override;
Hal Canary3a85ed12019-07-08 16:07:57 -040044 bool onChar(SkUnichar c, ModifierKey modifiers) override;
jvanverth9f372462016-04-06 06:08:59 -070045
Mike Reed3ae47332019-01-04 10:11:46 -050046 struct SkFontFields {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050047 bool fTypeface = false;
Ben Wagner15a8d572019-03-21 13:35:44 -040048 bool fSize = false;
49 SkScalar fSizeRange[2] = { 0, 20 };
50 bool fScaleX = false;
51 bool fSkewX = false;
Mike Reed3ae47332019-01-04 10:11:46 -050052 bool fHinting = false;
Mike Reede5f9cfa2019-01-10 13:55:35 -050053 bool fEdging = false;
54 bool fSubpixel = false;
Ben Wagner9613e452019-01-23 10:34:59 -050055 bool fForceAutoHinting = false;
56 bool fEmbeddedBitmaps = false;
57 bool fLinearMetrics = false;
58 bool fEmbolden = false;
Mike Reed3ae47332019-01-04 10:11:46 -050059 };
60 struct SkPaintFields {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050061 bool fPathEffect = false;
62 bool fShader = false;
63 bool fMaskFilter = false;
64 bool fColorFilter = false;
65 bool fDrawLooper = false;
66 bool fImageFilter = false;
67
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050068 bool fColor = false;
69 bool fWidth = false;
70 bool fMiterLimit = false;
71 bool fBlendMode = false;
72
Ben Wagner9613e452019-01-23 10:34:59 -050073 bool fAntiAlias = false;
74 bool fDither = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050075 enum class AntiAliasState {
76 Alias,
77 Normal,
78 AnalyticAAEnabled,
79 AnalyticAAForced,
Ben Wagner9613e452019-01-23 10:34:59 -050080 } fAntiAliasState = AntiAliasState::Alias;
Ben Wagnera580fb32018-04-17 11:16:32 -040081 const bool fOriginalSkUseAnalyticAA = gSkUseAnalyticAA;
82 const bool fOriginalSkForceAnalyticAA = gSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050083
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050084 bool fCapType = false;
85 bool fJoinType = false;
86 bool fStyle = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050087 bool fFilterQuality = false;
88 };
jvanverth9f372462016-04-06 06:08:59 -070089private:
Brian Osman92004802017-03-06 11:47:26 -050090 enum class ColorMode {
Brian Osman03115dc2018-11-26 13:55:19 -050091 kLegacy, // 8888, no color management
92 kColorManaged8888, // 8888 with color management
93 kColorManagedF16, // F16 with color management
Brian Osman92004802017-03-06 11:47:26 -050094 };
95
jvanverth2bb3b6d2016-04-08 07:24:09 -070096 void initSlides();
brianosman05de2162016-05-06 13:28:57 -070097 void updateTitle();
Brian Osman621491e2017-02-28 15:45:01 -050098 void setBackend(sk_app::Window::BackendType);
Brian Osman92004802017-03-06 11:47:26 -050099 void setColorMode(ColorMode);
Florin Malitaab99c342018-01-16 16:23:03 -0500100 int startupSlide() const;
101 void setCurrentSlide(int);
102 void setupCurrentSlide();
103 void listNames() const;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700104
liyuqiane5a6cd92016-05-27 08:52:52 -0700105 void updateUIState();
106
Robert Phillips9882dae2019-03-04 11:00:10 -0500107 void drawSlide(SkSurface* surface);
Brian Osmand67e5182017-12-08 16:46:09 -0500108 void drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700109
jvanverthc265a922016-04-08 12:51:45 -0700110 void changeZoomLevel(float delta);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400111 void preTouchMatrixChanged();
Yuqian Li755778c2018-03-28 16:23:31 -0400112 SkMatrix computePreTouchMatrix();
Brian Osman805a7272018-05-02 15:40:20 -0400113 SkMatrix computePerspectiveMatrix();
liyuqiand3cdbca2016-05-17 12:44:20 -0700114 SkMatrix computeMatrix();
Florin Malitacefc1b92018-02-19 21:43:47 -0500115 SkPoint mapEvent(float x, float y);
jvanverthc265a922016-04-08 12:51:45 -0700116
jvanverth34524262016-05-04 13:49:13 -0700117 sk_app::Window* fWindow;
jvanverth9f372462016-04-06 06:08:59 -0700118
Brian Osman56a24812017-12-19 11:15:16 -0500119 StatsLayer fStatsLayer;
120 StatsLayer::Timer fPaintTimer;
121 StatsLayer::Timer fFlushTimer;
122 StatsLayer::Timer fAnimateTimer;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700123
Mike Kleincd5104e2019-03-20 11:55:08 -0500124 AnimTimer fAnimTimer;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700125 SkTArray<sk_sp<Slide>> fSlides;
126 int fCurrentSlide;
jvanverthc265a922016-04-08 12:51:45 -0700127
liyuqian2edb0f42016-07-06 14:11:32 -0700128 bool fRefresh; // whether to continuously refresh for measuring render time
jvanverthc265a922016-04-08 12:51:45 -0700129
Brian Osman3ac99cf2017-12-01 11:23:53 -0500130 bool fSaveToSKP;
Mike Reed376d8122019-03-14 11:39:02 -0400131 bool fShowSlideDimensions;
Brian Osman3ac99cf2017-12-01 11:23:53 -0500132
Brian Osmand67e5182017-12-08 16:46:09 -0500133 ImGuiLayer fImGuiLayer;
Brian Osmana109e392017-02-24 09:49:14 -0500134 SkPaint fImGuiGamutPaint;
Brian Osman79086b92017-02-10 13:36:16 -0500135 bool fShowImGuiDebugWindow;
Brian Osmanfce09c52017-11-14 15:32:20 -0500136 bool fShowSlidePicker;
Brian Osman79086b92017-02-10 13:36:16 -0500137 bool fShowImGuiTestWindow;
138
Brian Osmanf6877092017-02-13 09:39:57 -0500139 bool fShowZoomWindow;
Ben Wagner3627d2e2018-06-26 14:23:20 -0400140 bool fZoomWindowFixed;
141 SkPoint fZoomWindowLocation;
Brian Osmanf6877092017-02-13 09:39:57 -0500142 sk_sp<SkImage> fLastImage;
Brian Osmanb63f6002018-07-24 18:01:53 -0400143 bool fZoomUI;
Brian Osmanf6877092017-02-13 09:39:57 -0500144
jvanverthaf236b52016-05-20 06:01:06 -0700145 sk_app::Window::BackendType fBackendType;
146
Brian Osmanf750fbc2017-02-08 10:47:28 -0500147 // Color properties for slide rendering
Brian Osman92004802017-03-06 11:47:26 -0500148 ColorMode fColorMode;
Brian Osmana109e392017-02-24 09:49:14 -0500149 SkColorSpacePrimaries fColorSpacePrimaries;
Brian Osman82ebe042019-01-04 17:03:00 -0500150 skcms_TransferFunction fColorSpaceTransferFn;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500151
jvanverthc265a922016-04-08 12:51:45 -0700152 // transform data
jvanverthc265a922016-04-08 12:51:45 -0700153 SkScalar fZoomLevel;
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400154 SkScalar fRotation;
Ben Wagner3627d2e2018-06-26 14:23:20 -0400155 SkVector fOffset;
jvanverthc265a922016-04-08 12:51:45 -0700156
brianosman622c8d52016-05-10 06:50:49 -0700157 sk_app::CommandSet fCommands;
liyuqiand3cdbca2016-05-17 12:44:20 -0700158
Brian Osmanb53f48c2017-06-07 10:00:30 -0400159 enum class GestureDevice {
160 kNone,
161 kTouch,
162 kMouse,
163 };
164
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400165 TouchGesture fGesture;
Brian Osmanb53f48c2017-06-07 10:00:30 -0400166 GestureDevice fGestureDevice;
liyuqiane46e4f02016-05-20 07:32:19 -0700167
168 // identity unless the window initially scales the content to fit the screen.
169 SkMatrix fDefaultMatrix;
liyuqian1f508fd2016-06-07 06:57:40 -0700170
Brian Osmane9ed0f02018-11-26 14:50:05 -0500171 bool fTiled;
172 bool fDrawTileBoundaries;
173 SkSize fTileScale;
174
Brian Osman805a7272018-05-02 15:40:20 -0400175 enum PerspectiveMode {
176 kPerspective_Off,
177 kPerspective_Real,
178 kPerspective_Fake,
179 };
180 PerspectiveMode fPerspectiveMode;
Brian Osman9bb47cf2018-04-26 15:55:00 -0400181 SkPoint fPerspectivePoints[4];
182
Brian Osmanfd8f4d52017-02-24 11:57:23 -0500183 SkTArray<std::function<void(void)>> fDeferredActions;
184
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500185 SkPaint fPaint;
186 SkPaintFields fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -0500187 SkFont fFont;
188 SkFontFields fFontOverrides;
Ben Wagner37c54032018-04-13 14:30:23 -0400189 bool fPixelGeometryOverrides = false;
jvanverth9f372462016-04-06 06:08:59 -0700190
Brian Osman0b8bb882019-04-12 11:47:19 -0400191 struct CachedGLSL {
192 bool fHovered = false;
193
194 sk_sp<const SkData> fKey;
195 SkString fKeyString;
196
Brian Osmana085a412019-04-25 09:44:43 -0400197 SkFourByteTag fShaderType;
198 SkSL::String fShader[kGrShaderTypeCount];
199 SkSL::Program::Inputs fInputs[kGrShaderTypeCount];
Brian Osman0b8bb882019-04-12 11:47:19 -0400200 };
201
202 sk_gpu_test::MemoryCache fPersistentCache;
203 SkTArray<CachedGLSL> fCachedGLSL;
204};
jvanverth9f372462016-04-06 06:08:59 -0700205
206#endif