blob: be25ef01c4be211d7bcac5f020ea123d4e5dd401 [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 Canaryb1f411a2019-08-29 10:39:22 -040040 bool onTouch(intptr_t owner, skui::InputState state, float x, float y) override;
41 bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) override;
Brian Osman80fc07e2017-12-08 16:45:43 -050042 void onUIStateChanged(const SkString& stateName, const SkString& stateValue) override;
Hal Canaryb1f411a2019-08-29 10:39:22 -040043 bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) override;
44 bool onChar(SkUnichar c, skui::ModifierKey modifiers) override;
Jim Van Verthd0cf5da2019-09-09 16:53:39 -040045 bool onPinch(skui::InputState state, float scale, float x, float y) override;
46 bool onFling(skui::InputState state) override;
jvanverth9f372462016-04-06 06:08:59 -070047
Mike Reed3ae47332019-01-04 10:11:46 -050048 struct SkFontFields {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050049 bool fTypeface = false;
Ben Wagner15a8d572019-03-21 13:35:44 -040050 bool fSize = false;
51 SkScalar fSizeRange[2] = { 0, 20 };
52 bool fScaleX = false;
53 bool fSkewX = false;
Mike Reed3ae47332019-01-04 10:11:46 -050054 bool fHinting = false;
Mike Reede5f9cfa2019-01-10 13:55:35 -050055 bool fEdging = false;
56 bool fSubpixel = false;
Ben Wagner9613e452019-01-23 10:34:59 -050057 bool fForceAutoHinting = false;
58 bool fEmbeddedBitmaps = false;
59 bool fLinearMetrics = false;
60 bool fEmbolden = false;
Ben Wagnerc17de1d2019-08-26 16:59:09 -040061 bool fBaselineSnap = false;
Mike Reed3ae47332019-01-04 10:11:46 -050062 };
63 struct SkPaintFields {
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050064 bool fPathEffect = false;
65 bool fShader = false;
66 bool fMaskFilter = false;
67 bool fColorFilter = false;
68 bool fDrawLooper = false;
69 bool fImageFilter = false;
70
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050071 bool fColor = false;
72 bool fWidth = false;
73 bool fMiterLimit = false;
74 bool fBlendMode = false;
75
Ben Wagner9613e452019-01-23 10:34:59 -050076 bool fAntiAlias = false;
77 bool fDither = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050078 enum class AntiAliasState {
79 Alias,
80 Normal,
81 AnalyticAAEnabled,
82 AnalyticAAForced,
Ben Wagner9613e452019-01-23 10:34:59 -050083 } fAntiAliasState = AntiAliasState::Alias;
Ben Wagnera580fb32018-04-17 11:16:32 -040084 const bool fOriginalSkUseAnalyticAA = gSkUseAnalyticAA;
85 const bool fOriginalSkForceAnalyticAA = gSkForceAnalyticAA;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050086
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050087 bool fCapType = false;
88 bool fJoinType = false;
89 bool fStyle = false;
Ben Wagnerabdcc5f2018-02-12 16:37:28 -050090 bool fFilterQuality = false;
91 };
jvanverth9f372462016-04-06 06:08:59 -070092private:
Brian Osman92004802017-03-06 11:47:26 -050093 enum class ColorMode {
Brian Osman03115dc2018-11-26 13:55:19 -050094 kLegacy, // 8888, no color management
95 kColorManaged8888, // 8888 with color management
96 kColorManagedF16, // F16 with color management
Brian Osman92004802017-03-06 11:47:26 -050097 };
98
jvanverth2bb3b6d2016-04-08 07:24:09 -070099 void initSlides();
brianosman05de2162016-05-06 13:28:57 -0700100 void updateTitle();
Brian Osman621491e2017-02-28 15:45:01 -0500101 void setBackend(sk_app::Window::BackendType);
Brian Osman92004802017-03-06 11:47:26 -0500102 void setColorMode(ColorMode);
Florin Malitaab99c342018-01-16 16:23:03 -0500103 int startupSlide() const;
104 void setCurrentSlide(int);
105 void setupCurrentSlide();
106 void listNames() const;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700107
liyuqiane5a6cd92016-05-27 08:52:52 -0700108 void updateUIState();
109
Robert Phillips9882dae2019-03-04 11:00:10 -0500110 void drawSlide(SkSurface* surface);
Brian Osmand67e5182017-12-08 16:46:09 -0500111 void drawImGui();
jvanverth3d6ed3a2016-04-07 11:09:51 -0700112
jvanverthc265a922016-04-08 12:51:45 -0700113 void changeZoomLevel(float delta);
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400114 void preTouchMatrixChanged();
Yuqian Li755778c2018-03-28 16:23:31 -0400115 SkMatrix computePreTouchMatrix();
Brian Osman805a7272018-05-02 15:40:20 -0400116 SkMatrix computePerspectiveMatrix();
liyuqiand3cdbca2016-05-17 12:44:20 -0700117 SkMatrix computeMatrix();
Florin Malitacefc1b92018-02-19 21:43:47 -0500118 SkPoint mapEvent(float x, float y);
jvanverthc265a922016-04-08 12:51:45 -0700119
jvanverth34524262016-05-04 13:49:13 -0700120 sk_app::Window* fWindow;
jvanverth9f372462016-04-06 06:08:59 -0700121
Brian Osman56a24812017-12-19 11:15:16 -0500122 StatsLayer fStatsLayer;
123 StatsLayer::Timer fPaintTimer;
124 StatsLayer::Timer fFlushTimer;
125 StatsLayer::Timer fAnimateTimer;
jvanverth3d6ed3a2016-04-07 11:09:51 -0700126
Mike Kleincd5104e2019-03-20 11:55:08 -0500127 AnimTimer fAnimTimer;
jvanverth2bb3b6d2016-04-08 07:24:09 -0700128 SkTArray<sk_sp<Slide>> fSlides;
129 int fCurrentSlide;
jvanverthc265a922016-04-08 12:51:45 -0700130
liyuqian2edb0f42016-07-06 14:11:32 -0700131 bool fRefresh; // whether to continuously refresh for measuring render time
jvanverthc265a922016-04-08 12:51:45 -0700132
Brian Osman3ac99cf2017-12-01 11:23:53 -0500133 bool fSaveToSKP;
Mike Reed376d8122019-03-14 11:39:02 -0400134 bool fShowSlideDimensions;
Brian Osman3ac99cf2017-12-01 11:23:53 -0500135
Brian Osmand67e5182017-12-08 16:46:09 -0500136 ImGuiLayer fImGuiLayer;
Brian Osmana109e392017-02-24 09:49:14 -0500137 SkPaint fImGuiGamutPaint;
Brian Osman79086b92017-02-10 13:36:16 -0500138 bool fShowImGuiDebugWindow;
Brian Osmanfce09c52017-11-14 15:32:20 -0500139 bool fShowSlidePicker;
Brian Osman79086b92017-02-10 13:36:16 -0500140 bool fShowImGuiTestWindow;
141
Brian Osmanf6877092017-02-13 09:39:57 -0500142 bool fShowZoomWindow;
Ben Wagner3627d2e2018-06-26 14:23:20 -0400143 bool fZoomWindowFixed;
144 SkPoint fZoomWindowLocation;
Brian Osmanf6877092017-02-13 09:39:57 -0500145 sk_sp<SkImage> fLastImage;
Brian Osmanb63f6002018-07-24 18:01:53 -0400146 bool fZoomUI;
Brian Osmanf6877092017-02-13 09:39:57 -0500147
jvanverthaf236b52016-05-20 06:01:06 -0700148 sk_app::Window::BackendType fBackendType;
149
Brian Osmanf750fbc2017-02-08 10:47:28 -0500150 // Color properties for slide rendering
Brian Osman92004802017-03-06 11:47:26 -0500151 ColorMode fColorMode;
Brian Osmana109e392017-02-24 09:49:14 -0500152 SkColorSpacePrimaries fColorSpacePrimaries;
Brian Osman82ebe042019-01-04 17:03:00 -0500153 skcms_TransferFunction fColorSpaceTransferFn;
Brian Osmanf750fbc2017-02-08 10:47:28 -0500154
jvanverthc265a922016-04-08 12:51:45 -0700155 // transform data
jvanverthc265a922016-04-08 12:51:45 -0700156 SkScalar fZoomLevel;
Ben Wagnerd02a74d2018-04-23 12:55:06 -0400157 SkScalar fRotation;
Ben Wagner3627d2e2018-06-26 14:23:20 -0400158 SkVector fOffset;
jvanverthc265a922016-04-08 12:51:45 -0700159
brianosman622c8d52016-05-10 06:50:49 -0700160 sk_app::CommandSet fCommands;
liyuqiand3cdbca2016-05-17 12:44:20 -0700161
Brian Osmanb53f48c2017-06-07 10:00:30 -0400162 enum class GestureDevice {
163 kNone,
164 kTouch,
165 kMouse,
166 };
167
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400168 TouchGesture fGesture;
Brian Osmanb53f48c2017-06-07 10:00:30 -0400169 GestureDevice fGestureDevice;
liyuqiane46e4f02016-05-20 07:32:19 -0700170
171 // identity unless the window initially scales the content to fit the screen.
172 SkMatrix fDefaultMatrix;
liyuqian1f508fd2016-06-07 06:57:40 -0700173
Brian Osmane9ed0f02018-11-26 14:50:05 -0500174 bool fTiled;
175 bool fDrawTileBoundaries;
176 SkSize fTileScale;
177
Brian Osman805a7272018-05-02 15:40:20 -0400178 enum PerspectiveMode {
179 kPerspective_Off,
180 kPerspective_Real,
181 kPerspective_Fake,
182 };
183 PerspectiveMode fPerspectiveMode;
Brian Osman9bb47cf2018-04-26 15:55:00 -0400184 SkPoint fPerspectivePoints[4];
185
Brian Osmanfd8f4d52017-02-24 11:57:23 -0500186 SkTArray<std::function<void(void)>> fDeferredActions;
187
Ben Wagnerabdcc5f2018-02-12 16:37:28 -0500188 SkPaint fPaint;
189 SkPaintFields fPaintOverrides;
Mike Reed3ae47332019-01-04 10:11:46 -0500190 SkFont fFont;
191 SkFontFields fFontOverrides;
Ben Wagner37c54032018-04-13 14:30:23 -0400192 bool fPixelGeometryOverrides = false;
jvanverth9f372462016-04-06 06:08:59 -0700193
Brian Osman0b8bb882019-04-12 11:47:19 -0400194 struct CachedGLSL {
195 bool fHovered = false;
196
197 sk_sp<const SkData> fKey;
198 SkString fKeyString;
199
Brian Osmana085a412019-04-25 09:44:43 -0400200 SkFourByteTag fShaderType;
201 SkSL::String fShader[kGrShaderTypeCount];
202 SkSL::Program::Inputs fInputs[kGrShaderTypeCount];
Brian Osman0b8bb882019-04-12 11:47:19 -0400203 };
204
205 sk_gpu_test::MemoryCache fPersistentCache;
206 SkTArray<CachedGLSL> fCachedGLSL;
207};
jvanverth9f372462016-04-06 06:08:59 -0700208
209#endif