blob: 6d49c9491510fac4b23e96526e194157b8635783 [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;
Ben Wagnerc17de1d2019-08-26 16:59:09 -040059 bool fBaselineSnap = false;
Mike Reed3ae47332019-01-04 10:11:46 -050060 };
61 struct SkPaintFields {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050062 bool fPathEffect = false;
63 bool fShader = false;
64 bool fMaskFilter = false;
65 bool fColorFilter = false;
66 bool fDrawLooper = false;
67 bool fImageFilter = false;
68
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050069 bool fColor = false;
70 bool fWidth = false;
71 bool fMiterLimit = false;
72 bool fBlendMode = false;
73
Ben Wagner9613e452019-01-23 10:34:59 -050074 bool fAntiAlias = false;
75 bool fDither = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050076 enum class AntiAliasState {
77 Alias,
78 Normal,
79 AnalyticAAEnabled,
80 AnalyticAAForced,
Ben Wagner9613e452019-01-23 10:34:59 -050081 } fAntiAliasState = AntiAliasState::Alias;
Ben Wagnera580fb32018-04-17 11:16:32 -040082 const bool fOriginalSkUseAnalyticAA = gSkUseAnalyticAA;
83 const bool fOriginalSkForceAnalyticAA = gSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050084
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050085 bool fCapType = false;
86 bool fJoinType = false;
87 bool fStyle = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050088 bool fFilterQuality = false;
89 };
jvanverth9f372462016-04-06 06:08:59 -070090private:
Brian Osman92004802017-03-06 11:47:26 -050091 enum class ColorMode {
Brian Osman03115dc2018-11-26 13:55:19 -050092 kLegacy, // 8888, no color management
93 kColorManaged8888, // 8888 with color management
94 kColorManagedF16, // F16 with color management
Brian Osman92004802017-03-06 11:47:26 -050095 };
96
jvanverth2bb3b6d2016-04-08 07:24:09 -070097 void initSlides();
brianosman05de2162016-05-06 13:28:57 -070098 void updateTitle();
Brian Osman621491e2017-02-28 15:45:01 -050099 void setBackend(sk_app::Window::BackendType);
Brian Osman92004802017-03-06 11:47:26 -0500100 void setColorMode(ColorMode);
Florin Malitaab99c342018-01-16 16:23:03 -0500101 int startupSlide() const;
102 void setCurrentSlide(int);
103 void setupCurrentSlide();
104 void listNames() const;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700105
liyuqiane5a6cd92016-05-27 08:52:52 -0700106 void updateUIState();
107
Robert Phillips9882dae2019-03-04 11:00:10 -0500108 void drawSlide(SkSurface* surface);
Brian Osmand67e5182017-12-08 16:46:09 -0500109 void drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700110
jvanverthc265a922016-04-08 12:51:45 -0700111 void changeZoomLevel(float delta);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400112 void preTouchMatrixChanged();
Yuqian Li755778c2018-03-28 16:23:31 -0400113 SkMatrix computePreTouchMatrix();
Brian Osman805a7272018-05-02 15:40:20 -0400114 SkMatrix computePerspectiveMatrix();
liyuqiand3cdbca2016-05-17 12:44:20 -0700115 SkMatrix computeMatrix();
Florin Malitacefc1b92018-02-19 21:43:47 -0500116 SkPoint mapEvent(float x, float y);
jvanverthc265a922016-04-08 12:51:45 -0700117
jvanverth34524262016-05-04 13:49:13 -0700118 sk_app::Window* fWindow;
jvanverth9f372462016-04-06 06:08:59 -0700119
Brian Osman56a24812017-12-19 11:15:16 -0500120 StatsLayer fStatsLayer;
121 StatsLayer::Timer fPaintTimer;
122 StatsLayer::Timer fFlushTimer;
123 StatsLayer::Timer fAnimateTimer;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700124
Mike Kleincd5104e2019-03-20 11:55:08 -0500125 AnimTimer fAnimTimer;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700126 SkTArray<sk_sp<Slide>> fSlides;
127 int fCurrentSlide;
jvanverthc265a922016-04-08 12:51:45 -0700128
liyuqian2edb0f42016-07-06 14:11:32 -0700129 bool fRefresh; // whether to continuously refresh for measuring render time
jvanverthc265a922016-04-08 12:51:45 -0700130
Brian Osman3ac99cf2017-12-01 11:23:53 -0500131 bool fSaveToSKP;
Mike Reed376d8122019-03-14 11:39:02 -0400132 bool fShowSlideDimensions;
Brian Osman3ac99cf2017-12-01 11:23:53 -0500133
Brian Osmand67e5182017-12-08 16:46:09 -0500134 ImGuiLayer fImGuiLayer;
Brian Osmana109e392017-02-24 09:49:14 -0500135 SkPaint fImGuiGamutPaint;
Brian Osman79086b92017-02-10 13:36:16 -0500136 bool fShowImGuiDebugWindow;
Brian Osmanfce09c52017-11-14 15:32:20 -0500137 bool fShowSlidePicker;
Brian Osman79086b92017-02-10 13:36:16 -0500138 bool fShowImGuiTestWindow;
139
Brian Osmanf6877092017-02-13 09:39:57 -0500140 bool fShowZoomWindow;
Ben Wagner3627d2e2018-06-26 14:23:20 -0400141 bool fZoomWindowFixed;
142 SkPoint fZoomWindowLocation;
Brian Osmanf6877092017-02-13 09:39:57 -0500143 sk_sp<SkImage> fLastImage;
Brian Osmanb63f6002018-07-24 18:01:53 -0400144 bool fZoomUI;
Brian Osmanf6877092017-02-13 09:39:57 -0500145
jvanverthaf236b52016-05-20 06:01:06 -0700146 sk_app::Window::BackendType fBackendType;
147
Brian Osmanf750fbc2017-02-08 10:47:28 -0500148 // Color properties for slide rendering
Brian Osman92004802017-03-06 11:47:26 -0500149 ColorMode fColorMode;
Brian Osmana109e392017-02-24 09:49:14 -0500150 SkColorSpacePrimaries fColorSpacePrimaries;
Brian Osman82ebe042019-01-04 17:03:00 -0500151 skcms_TransferFunction fColorSpaceTransferFn;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500152
jvanverthc265a922016-04-08 12:51:45 -0700153 // transform data
jvanverthc265a922016-04-08 12:51:45 -0700154 SkScalar fZoomLevel;
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400155 SkScalar fRotation;
Ben Wagner3627d2e2018-06-26 14:23:20 -0400156 SkVector fOffset;
jvanverthc265a922016-04-08 12:51:45 -0700157
brianosman622c8d52016-05-10 06:50:49 -0700158 sk_app::CommandSet fCommands;
liyuqiand3cdbca2016-05-17 12:44:20 -0700159
Brian Osmanb53f48c2017-06-07 10:00:30 -0400160 enum class GestureDevice {
161 kNone,
162 kTouch,
163 kMouse,
164 };
165
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400166 TouchGesture fGesture;
Brian Osmanb53f48c2017-06-07 10:00:30 -0400167 GestureDevice fGestureDevice;
liyuqiane46e4f02016-05-20 07:32:19 -0700168
169 // identity unless the window initially scales the content to fit the screen.
170 SkMatrix fDefaultMatrix;
liyuqian1f508fd2016-06-07 06:57:40 -0700171
Brian Osmane9ed0f02018-11-26 14:50:05 -0500172 bool fTiled;
173 bool fDrawTileBoundaries;
174 SkSize fTileScale;
175
Brian Osman805a7272018-05-02 15:40:20 -0400176 enum PerspectiveMode {
177 kPerspective_Off,
178 kPerspective_Real,
179 kPerspective_Fake,
180 };
181 PerspectiveMode fPerspectiveMode;
Brian Osman9bb47cf2018-04-26 15:55:00 -0400182 SkPoint fPerspectivePoints[4];
183
Brian Osmanfd8f4d52017-02-24 11:57:23 -0500184 SkTArray<std::function<void(void)>> fDeferredActions;
185
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500186 SkPaint fPaint;
187 SkPaintFields fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -0500188 SkFont fFont;
189 SkFontFields fFontOverrides;
Ben Wagner37c54032018-04-13 14:30:23 -0400190 bool fPixelGeometryOverrides = false;
jvanverth9f372462016-04-06 06:08:59 -0700191
Brian Osman0b8bb882019-04-12 11:47:19 -0400192 struct CachedGLSL {
193 bool fHovered = false;
194
195 sk_sp<const SkData> fKey;
196 SkString fKeyString;
197
Brian Osmana085a412019-04-25 09:44:43 -0400198 SkFourByteTag fShaderType;
199 SkSL::String fShader[kGrShaderTypeCount];
200 SkSL::Program::Inputs fInputs[kGrShaderTypeCount];
Brian Osman0b8bb882019-04-12 11:47:19 -0400201 };
202
203 sk_gpu_test::MemoryCache fPersistentCache;
204 SkTArray<CachedGLSL> fCachedGLSL;
205};
jvanverth9f372462016-04-06 06:08:59 -0700206
207#endif