blob: 76be66206edbdfa4285d3f20d7da1f9b7f121123 [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
8#ifndef Window_DEFINED
9#define Window_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRect.h"
12#include "include/core/SkTypes.h"
13#include "include/private/SkTDArray.h"
14#include "tools/sk_app/DisplayParams.h"
Hal Canaryb1f411a2019-08-29 10:39:22 -040015#include "tools/skui/InputState.h"
16#include "tools/skui/Key.h"
17#include "tools/skui/ModifierKey.h"
jvanverth9fab59d2016-04-06 12:08:51 -070018
csmartdalton61cd31a2017-02-27 17:00:53 -070019class GrContext;
jvanverth9f372462016-04-06 06:08:59 -070020class SkCanvas;
liyuqian74959a12016-06-16 14:10:34 -070021class SkSurface;
Ben Wagner37c54032018-04-13 14:30:23 -040022class SkSurfaceProps;
jvanverth9f372462016-04-06 06:08:59 -070023
jvanverth34524262016-05-04 13:49:13 -070024namespace sk_app {
25
jvanvertha8d0d6c2016-05-05 12:32:03 -070026class WindowContext;
27
jvanverth9f372462016-04-06 06:08:59 -070028class Window {
29public:
30 static Window* CreateNativeWindow(void* platformData);
31
Hal Canary149f3f12019-08-01 16:21:49 -040032 virtual ~Window();
jvanverth9f372462016-04-06 06:08:59 -070033
34 virtual void setTitle(const char*) = 0;
35 virtual void show() = 0;
Brian Osmaneff04b52017-11-21 13:18:02 -050036
37 // JSON-formatted UI state for Android. Do nothing by default
38 virtual void setUIState(const char*) {}
liyuqian566c8e42016-05-23 10:52:34 -070039
40 // Shedules an invalidation event for window if one is not currently pending.
41 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
42 // the the inval event. They unset fIsContentInvalided which allow future onInval.
43 void inval();
jvanverth9f372462016-04-06 06:08:59 -070044
djsollen12d62a72016-04-21 07:59:44 -070045 virtual bool scaleContentToFit() const { return false; }
djsollen12d62a72016-04-21 07:59:44 -070046
jvanverthaf236b52016-05-20 06:01:06 -070047 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070048 kNativeGL_BackendType,
Brian Salomon194db172017-08-17 14:37:06 -040049#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
50 kANGLE_BackendType,
51#endif
Stephen Whitea800ec92019-08-02 15:04:52 -040052#ifdef SK_DAWN
53 kDawn_BackendType,
54#endif
jvanverth063ece72016-06-17 09:29:14 -070055#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070056 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070057#endif
Jim Van Verthe58d5322019-09-03 09:42:57 -040058#ifdef SK_METAL
Jim Van Verthbe39f712019-02-08 15:36:14 -050059 kMetal_BackendType,
60#endif
liyuqiand94ad582016-06-07 14:22:37 -070061 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070062
liyuqiand94ad582016-06-07 14:22:37 -070063 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070064 };
65 enum {
66 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070067 };
68
csmartdalton578f0642017-02-24 16:04:47 -070069 virtual bool attach(BackendType) = 0;
jvanverth9f372462016-04-06 06:08:59 -070070 void detach();
71
72 // input handling
jvanverth9fab59d2016-04-06 12:08:51 -070073
Brian Osman80fc07e2017-12-08 16:45:43 -050074 class Layer {
75 public:
Brian Osman56a24812017-12-19 11:15:16 -050076 Layer() : fActive(true) {}
Brian Osman80fc07e2017-12-08 16:45:43 -050077 virtual ~Layer() = default;
jvanverth9f372462016-04-06 06:08:59 -070078
Brian Osman56a24812017-12-19 11:15:16 -050079 bool getActive() { return fActive; }
80 void setActive(bool active) { fActive = active; }
81
Brian Osman80fc07e2017-12-08 16:45:43 -050082 // return value of 'true' means 'I have handled this event'
83 virtual void onBackendCreated() {}
Brian Osmand67e5182017-12-08 16:46:09 -050084 virtual void onAttach(Window* window) {}
Hal Canaryb1f411a2019-08-29 10:39:22 -040085 virtual bool onChar(SkUnichar c, skui::ModifierKey) { return false; }
86 virtual bool onKey(skui::Key, skui::InputState, skui::ModifierKey) { return false; }
87 virtual bool onMouse(int x, int y, skui::InputState, skui::ModifierKey) { return false; }
88 virtual bool onMouseWheel(float delta, skui::ModifierKey) { return false; }
89 virtual bool onTouch(intptr_t owner, skui::InputState, float x, float y) { return false; }
Jim Van Verthd0cf5da2019-09-09 16:53:39 -040090 // Platform-detected gesture events
91 virtual bool onFling(skui::InputState state) { return false; }
92 virtual bool onPinch(skui::InputState state, float scale, float x, float y) { return false; }
Brian Osman80fc07e2017-12-08 16:45:43 -050093 virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
Brian Osmand67e5182017-12-08 16:46:09 -050094 virtual void onPrePaint() {}
Robert Phillips9882dae2019-03-04 11:00:10 -050095 virtual void onPaint(SkSurface*) {}
Ben Wagnera1915972018-08-09 15:06:19 -040096 virtual void onResize(int width, int height) {}
Brian Osman56a24812017-12-19 11:15:16 -050097
98 private:
99 friend class Window;
100 bool fActive;
Brian Osman80fc07e2017-12-08 16:45:43 -0500101 };
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700102
Brian Osman80fc07e2017-12-08 16:45:43 -0500103 void pushLayer(Layer* layer) {
Brian Osmand67e5182017-12-08 16:46:09 -0500104 layer->onAttach(this);
Mike Reed5edcd312018-08-08 11:23:41 -0400105 fLayers.push_back(layer);
liyuqiane5a6cd92016-05-27 08:52:52 -0700106 }
107
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700108 void onBackendCreated();
Hal Canaryb1f411a2019-08-29 10:39:22 -0400109 bool onChar(SkUnichar c, skui::ModifierKey modifiers);
110 bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers);
111 bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers);
112 bool onMouseWheel(float delta, skui::ModifierKey modifiers);
113 bool onTouch(intptr_t owner, skui::InputState state, float x, float y); // multi-owner = multi-touch
Jim Van Verthd0cf5da2019-09-09 16:53:39 -0400114 // Platform-detected gesture events
115 bool onFling(skui::InputState state);
116 bool onPinch(skui::InputState state, float scale, float x, float y);
liyuqiane5a6cd92016-05-27 08:52:52 -0700117 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700118 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700119 void onResize(int width, int height);
jvanverth9f372462016-04-06 06:08:59 -0700120
Jim Van Verth98385ba2019-02-06 11:23:34 -0500121 int width() const;
122 int height() const;
jvanverthc265a922016-04-08 12:51:45 -0700123
csmartdalton578f0642017-02-24 16:04:47 -0700124 virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
Brian Osman2ac96dc2017-06-23 13:32:29 -0400125 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
csmartdalton578f0642017-02-24 16:04:47 -0700126
127 // Actual parameters in effect, obtained from the native window.
128 int sampleCount() const;
129 int stencilBits() const;
brianosman05de2162016-05-06 13:28:57 -0700130
csmartdalton61cd31a2017-02-27 17:00:53 -0700131 // Returns null if there is not a GPU backend or if the backend is not yet created.
Chris Dalton89305752018-11-01 10:52:34 -0600132 GrContext* getGrContext() const;
csmartdalton61cd31a2017-02-27 17:00:53 -0700133
jvanverth9f372462016-04-06 06:08:59 -0700134protected:
135 Window();
136
Brian Osman80fc07e2017-12-08 16:45:43 -0500137 SkTDArray<Layer*> fLayers;
csmartdalton578f0642017-02-24 16:04:47 -0700138 DisplayParams fRequestedDisplayParams;
jvanverth9f372462016-04-06 06:08:59 -0700139
Hal Canary149f3f12019-08-01 16:21:49 -0400140 std::unique_ptr<WindowContext> fWindowContext;
liyuqian566c8e42016-05-23 10:52:34 -0700141
142 virtual void onInval() = 0;
143
144 // Uncheck fIsContentInvalided to allow future inval/onInval.
145 void markInvalProcessed();
146
147 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
Brian Osman56a24812017-12-19 11:15:16 -0500148
149 void visitLayers(std::function<void(Layer*)> visitor);
150 bool signalLayers(std::function<bool(Layer*)> visitor);
jvanverth9f372462016-04-06 06:08:59 -0700151};
152
jvanverth34524262016-05-04 13:49:13 -0700153} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700154#endif