blob: b4ac70d2d280f4f3fe74813ff3728c7cf16c56c2 [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"
Brian Osmanf847f312020-06-18 14:18:27 -040014#include "include/gpu/GrContextOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkScan.h"
16#include "src/sksl/SkSLString.h"
17#include "src/sksl/ir/SkSLProgram.h"
18#include "tools/gpu/MemoryCache.h"
19#include "tools/sk_app/Application.h"
20#include "tools/sk_app/CommandSet.h"
21#include "tools/sk_app/Window.h"
Hal Canary41248072019-07-11 16:32:53 -040022#include "tools/viewer/AnimTimer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tools/viewer/ImGuiLayer.h"
24#include "tools/viewer/Slide.h"
25#include "tools/viewer/StatsLayer.h"
26#include "tools/viewer/TouchGesture.h"
jvanverth9f372462016-04-06 06:08:59 -070027
28class SkCanvas;
Brian Osman0b8bb882019-04-12 11:47:19 -040029class SkData;
jvanverth9f372462016-04-06 06:08:59 -070030
Brian Osman80fc07e2017-12-08 16:45:43 -050031class Viewer : public sk_app::Application, sk_app::Window::Layer {
jvanverth9f372462016-04-06 06:08:59 -070032public:
jvanverth34524262016-05-04 13:49:13 -070033 Viewer(int argc, char** argv, void* platformData);
34 ~Viewer() override;
jvanverth9f372462016-04-06 06:08:59 -070035
liyuqian2edb0f42016-07-06 14:11:32 -070036 void onIdle() override;
Brian Osman80fc07e2017-12-08 16:45:43 -050037
38 void onBackendCreated() override;
Robert Phillips9882dae2019-03-04 11:00:10 -050039 void onPaint(SkSurface*) override;
Ben Wagnera1915972018-08-09 15:06:19 -040040 void onResize(int width, int height) override;
Hal Canaryb1f411a2019-08-29 10:39:22 -040041 bool onTouch(intptr_t owner, skui::InputState state, float x, float y) override;
42 bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) override;
Brian Osman80fc07e2017-12-08 16:45:43 -050043 void onUIStateChanged(const SkString& stateName, const SkString& stateValue) override;
Hal Canaryb1f411a2019-08-29 10:39:22 -040044 bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) override;
45 bool onChar(SkUnichar c, skui::ModifierKey modifiers) override;
Jim Van Verthd0cf5da2019-09-09 16:53:39 -040046 bool onPinch(skui::InputState state, float scale, float x, float y) override;
47 bool onFling(skui::InputState state) override;
jvanverth9f372462016-04-06 06:08:59 -070048
Brian Osmanf847f312020-06-18 14:18:27 -040049 static GrContextOptions::ShaderErrorHandler* ShaderErrorHandler();
50
Mike Reed3ae47332019-01-04 10:11:46 -050051 struct SkFontFields {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050052 bool fTypeface = false;
Ben Wagner15a8d572019-03-21 13:35:44 -040053 bool fSize = false;
54 SkScalar fSizeRange[2] = { 0, 20 };
55 bool fScaleX = false;
56 bool fSkewX = false;
Mike Reed3ae47332019-01-04 10:11:46 -050057 bool fHinting = false;
Mike Reede5f9cfa2019-01-10 13:55:35 -050058 bool fEdging = false;
59 bool fSubpixel = false;
Ben Wagner9613e452019-01-23 10:34:59 -050060 bool fForceAutoHinting = false;
61 bool fEmbeddedBitmaps = false;
62 bool fLinearMetrics = false;
63 bool fEmbolden = false;
Ben Wagnerc17de1d2019-08-26 16:59:09 -040064 bool fBaselineSnap = false;
Mike Reed3ae47332019-01-04 10:11:46 -050065 };
66 struct SkPaintFields {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050067 bool fPathEffect = false;
68 bool fShader = false;
69 bool fMaskFilter = false;
70 bool fColorFilter = false;
71 bool fDrawLooper = false;
72 bool fImageFilter = false;
73
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050074 bool fColor = false;
75 bool fWidth = false;
76 bool fMiterLimit = false;
77 bool fBlendMode = false;
78
Ben Wagner9613e452019-01-23 10:34:59 -050079 bool fAntiAlias = false;
80 bool fDither = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050081 enum class AntiAliasState {
82 Alias,
83 Normal,
84 AnalyticAAEnabled,
85 AnalyticAAForced,
Ben Wagner9613e452019-01-23 10:34:59 -050086 } fAntiAliasState = AntiAliasState::Alias;
Ben Wagnera580fb32018-04-17 11:16:32 -040087 const bool fOriginalSkUseAnalyticAA = gSkUseAnalyticAA;
88 const bool fOriginalSkForceAnalyticAA = gSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050089
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050090 bool fCapType = false;
91 bool fJoinType = false;
92 bool fStyle = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050093 };
Ben Wagnerae4bb982020-09-24 14:49:00 -040094 struct SkSurfacePropsFields {
95 bool fFlags = false;
96 bool fPixelGeometry = false;
97 };
98 struct DisplayFields {
99 bool fColorType = false;
100 bool fColorSpace = false;
101 bool fMSAASampleCount = false;
102 bool fGrContextOptions = false;
103 SkSurfacePropsFields fSurfaceProps;
104 bool fDisableVsync = false;
105 };
jvanverth9f372462016-04-06 06:08:59 -0700106private:
Brian Osman92004802017-03-06 11:47:26 -0500107 enum class ColorMode {
Brian Salomon8391bac2019-09-18 11:22:44 -0400108 kLegacy, // 8888, no color management
109 kColorManaged8888, // 8888 with color management
110 kColorManagedF16, // F16 with color management
111 kColorManagedF16Norm, // Normalized F16 with color management
Brian Osman92004802017-03-06 11:47:26 -0500112 };
113
jvanverth2bb3b6d2016-04-08 07:24:09 -0700114 void initSlides();
brianosman05de2162016-05-06 13:28:57 -0700115 void updateTitle();
Brian Osman621491e2017-02-28 15:45:01 -0500116 void setBackend(sk_app::Window::BackendType);
Brian Osman92004802017-03-06 11:47:26 -0500117 void setColorMode(ColorMode);
Florin Malitaab99c342018-01-16 16:23:03 -0500118 int startupSlide() const;
119 void setCurrentSlide(int);
120 void setupCurrentSlide();
121 void listNames() const;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700122
liyuqiane5a6cd92016-05-27 08:52:52 -0700123 void updateUIState();
124
Robert Phillips9882dae2019-03-04 11:00:10 -0500125 void drawSlide(SkSurface* surface);
Brian Osmand67e5182017-12-08 16:46:09 -0500126 void drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700127
jvanverthc265a922016-04-08 12:51:45 -0700128 void changeZoomLevel(float delta);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400129 void preTouchMatrixChanged();
Yuqian Li755778c2018-03-28 16:23:31 -0400130 SkMatrix computePreTouchMatrix();
Brian Osman805a7272018-05-02 15:40:20 -0400131 SkMatrix computePerspectiveMatrix();
liyuqiand3cdbca2016-05-17 12:44:20 -0700132 SkMatrix computeMatrix();
Florin Malitacefc1b92018-02-19 21:43:47 -0500133 SkPoint mapEvent(float x, float y);
jvanverthc265a922016-04-08 12:51:45 -0700134
jvanverth34524262016-05-04 13:49:13 -0700135 sk_app::Window* fWindow;
jvanverth9f372462016-04-06 06:08:59 -0700136
Brian Osman56a24812017-12-19 11:15:16 -0500137 StatsLayer fStatsLayer;
138 StatsLayer::Timer fPaintTimer;
139 StatsLayer::Timer fFlushTimer;
140 StatsLayer::Timer fAnimateTimer;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700141
Mike Kleincd5104e2019-03-20 11:55:08 -0500142 AnimTimer fAnimTimer;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700143 SkTArray<sk_sp<Slide>> fSlides;
144 int fCurrentSlide;
jvanverthc265a922016-04-08 12:51:45 -0700145
liyuqian2edb0f42016-07-06 14:11:32 -0700146 bool fRefresh; // whether to continuously refresh for measuring render time
jvanverthc265a922016-04-08 12:51:45 -0700147
Brian Osman3ac99cf2017-12-01 11:23:53 -0500148 bool fSaveToSKP;
Mike Reed376d8122019-03-14 11:39:02 -0400149 bool fShowSlideDimensions;
Brian Osman3ac99cf2017-12-01 11:23:53 -0500150
Brian Osmand67e5182017-12-08 16:46:09 -0500151 ImGuiLayer fImGuiLayer;
Brian Osmana109e392017-02-24 09:49:14 -0500152 SkPaint fImGuiGamutPaint;
Brian Osman79086b92017-02-10 13:36:16 -0500153 bool fShowImGuiDebugWindow;
Brian Osmanfce09c52017-11-14 15:32:20 -0500154 bool fShowSlidePicker;
Brian Osman79086b92017-02-10 13:36:16 -0500155 bool fShowImGuiTestWindow;
156
Brian Osmanf6877092017-02-13 09:39:57 -0500157 bool fShowZoomWindow;
Ben Wagner3627d2e2018-06-26 14:23:20 -0400158 bool fZoomWindowFixed;
159 SkPoint fZoomWindowLocation;
Brian Osmanf6877092017-02-13 09:39:57 -0500160 sk_sp<SkImage> fLastImage;
Brian Osmanb63f6002018-07-24 18:01:53 -0400161 bool fZoomUI;
Brian Osmanf6877092017-02-13 09:39:57 -0500162
jvanverthaf236b52016-05-20 06:01:06 -0700163 sk_app::Window::BackendType fBackendType;
164
Brian Osmanf750fbc2017-02-08 10:47:28 -0500165 // Color properties for slide rendering
Brian Osman92004802017-03-06 11:47:26 -0500166 ColorMode fColorMode;
Brian Osmana109e392017-02-24 09:49:14 -0500167 SkColorSpacePrimaries fColorSpacePrimaries;
Brian Osman82ebe042019-01-04 17:03:00 -0500168 skcms_TransferFunction fColorSpaceTransferFn;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500169
jvanverthc265a922016-04-08 12:51:45 -0700170 // transform data
Ben Wagnerf9a0f1a2021-02-01 15:38:58 -0500171 bool fApplyBackingScale;
jvanverthc265a922016-04-08 12:51:45 -0700172 SkScalar fZoomLevel;
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400173 SkScalar fRotation;
Ben Wagner3627d2e2018-06-26 14:23:20 -0400174 SkVector fOffset;
jvanverthc265a922016-04-08 12:51:45 -0700175
brianosman622c8d52016-05-10 06:50:49 -0700176 sk_app::CommandSet fCommands;
liyuqiand3cdbca2016-05-17 12:44:20 -0700177
Brian Osmanb53f48c2017-06-07 10:00:30 -0400178 enum class GestureDevice {
179 kNone,
180 kTouch,
181 kMouse,
182 };
183
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400184 TouchGesture fGesture;
Brian Osmanb53f48c2017-06-07 10:00:30 -0400185 GestureDevice fGestureDevice;
liyuqiane46e4f02016-05-20 07:32:19 -0700186
187 // identity unless the window initially scales the content to fit the screen.
188 SkMatrix fDefaultMatrix;
liyuqian1f508fd2016-06-07 06:57:40 -0700189
Brian Osmane9ed0f02018-11-26 14:50:05 -0500190 bool fTiled;
191 bool fDrawTileBoundaries;
192 SkSize fTileScale;
Mike Reed59295352020-03-12 13:56:34 -0400193 bool fDrawViaSerialize = false;
Brian Osmane9ed0f02018-11-26 14:50:05 -0500194
Brian Osman805a7272018-05-02 15:40:20 -0400195 enum PerspectiveMode {
196 kPerspective_Off,
197 kPerspective_Real,
198 kPerspective_Fake,
199 };
200 PerspectiveMode fPerspectiveMode;
Brian Osman9bb47cf2018-04-26 15:55:00 -0400201 SkPoint fPerspectivePoints[4];
202
Brian Osmanfd8f4d52017-02-24 11:57:23 -0500203 SkTArray<std::function<void(void)>> fDeferredActions;
204
Ben Wagnerae4bb982020-09-24 14:49:00 -0400205 // fPaint contains override values, fPaintOverrides controls if overrides are applied.
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500206 SkPaint fPaint;
207 SkPaintFields fPaintOverrides;
Ben Wagnerae4bb982020-09-24 14:49:00 -0400208
209 // fFont contains override values, fFontOverrides controls if overrides are applied.
Mike Reed3ae47332019-01-04 10:11:46 -0500210 SkFont fFont;
211 SkFontFields fFontOverrides;
Ben Wagnerae4bb982020-09-24 14:49:00 -0400212
213 // fDisplay contains default values (fWindow.fRequestedDisplayParams contains the overrides),
214 // fDisplayOverrides controls if overrides are applied.
215 sk_app::DisplayParams fDisplay;
216 DisplayFields fDisplayOverrides;
jvanverth9f372462016-04-06 06:08:59 -0700217
Brian Osmanc85f1fa2020-06-16 15:11:34 -0400218 struct CachedShader {
Brian Osman0b8bb882019-04-12 11:47:19 -0400219 bool fHovered = false;
220
221 sk_sp<const SkData> fKey;
222 SkString fKeyString;
223
Brian Osmana085a412019-04-25 09:44:43 -0400224 SkFourByteTag fShaderType;
225 SkSL::String fShader[kGrShaderTypeCount];
226 SkSL::Program::Inputs fInputs[kGrShaderTypeCount];
Brian Osman0b8bb882019-04-12 11:47:19 -0400227 };
228
229 sk_gpu_test::MemoryCache fPersistentCache;
Brian Osmanc85f1fa2020-06-16 15:11:34 -0400230 SkTArray<CachedShader> fCachedShaders;
Brian Osman0b8bb882019-04-12 11:47:19 -0400231};
jvanverth9f372462016-04-06 06:08:59 -0700232
233#endif