1 /* 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 11 #include "include/core/SkRect.h" 12 #include "include/core/SkTypes.h" 13 #include "include/private/base/SkTDArray.h" 14 #include "tools/sk_app/DisplayParams.h" 15 #include "tools/skui/InputState.h" 16 #include "tools/skui/Key.h" 17 #include "tools/skui/ModifierKey.h" 18 19 #include <functional> 20 21 class GrDirectContext; 22 class SkCanvas; 23 class SkSurface; 24 class SkSurfaceProps; 25 class SkString; 26 27 namespace skgpu::graphite { 28 class Context; 29 } 30 31 namespace sk_app { 32 33 class WindowContext; 34 35 class Window { 36 public: 37 static Window* CreateNativeWindow(void* platformData); 38 39 virtual ~Window(); 40 41 virtual void setTitle(const char*) = 0; 42 virtual void show() = 0; 43 44 // JSON-formatted UI state for Android. Do nothing by default setUIState(const char *)45 virtual void setUIState(const char*) {} 46 47 // Interface to the system clipboard. Only implemented on UNIX. getClipboardText()48 virtual const char* getClipboardText() { return nullptr; } setClipboardText(const char *)49 virtual void setClipboardText(const char*) {} 50 51 // Schedules an invalidation event for window if one is not currently pending. 52 // Make sure that either onPaint or markInvalReceived is called when the client window consumes 53 // the the inval event. They unset fIsContentInvalided which allow future onInval. 54 void inval(); 55 scaleContentToFit()56 virtual bool scaleContentToFit() const { return false; } 57 58 enum BackendType { 59 #ifdef SK_GL 60 kNativeGL_BackendType, 61 #endif 62 #if SK_ANGLE && defined(SK_BUILD_FOR_WIN) 63 kANGLE_BackendType, 64 #endif 65 #ifdef SK_DAWN 66 kDawn_BackendType, 67 #if defined(SK_GRAPHITE) 68 kGraphiteDawn_BackendType, 69 #endif 70 #endif 71 #ifdef SK_VULKAN 72 kVulkan_BackendType, 73 #endif 74 #ifdef SK_METAL 75 kMetal_BackendType, 76 #if defined(SK_GRAPHITE) 77 kGraphiteMetal_BackendType, 78 #endif 79 #endif 80 #ifdef SK_DIRECT3D 81 kDirect3D_BackendType, 82 #endif 83 kRaster_BackendType, 84 85 kLast_BackendType = kRaster_BackendType 86 }; 87 enum { 88 kBackendTypeCount = kLast_BackendType + 1 89 }; 90 91 virtual bool attach(BackendType) = 0; 92 void detach(); 93 94 // input handling 95 96 class Layer { 97 public: Layer()98 Layer() : fActive(true) {} 99 virtual ~Layer() = default; 100 getActive()101 bool getActive() { return fActive; } setActive(bool active)102 void setActive(bool active) { fActive = active; } 103 104 // return value of 'true' means 'I have handled this event' onBackendCreated()105 virtual void onBackendCreated() {} onAttach(Window * window)106 virtual void onAttach(Window* window) {} onChar(SkUnichar c,skui::ModifierKey)107 virtual bool onChar(SkUnichar c, skui::ModifierKey) { return false; } onKey(skui::Key,skui::InputState,skui::ModifierKey)108 virtual bool onKey(skui::Key, skui::InputState, skui::ModifierKey) { return false; } onMouse(int x,int y,skui::InputState,skui::ModifierKey)109 virtual bool onMouse(int x, int y, skui::InputState, skui::ModifierKey) { return false; } onMouseWheel(float delta,skui::ModifierKey)110 virtual bool onMouseWheel(float delta, skui::ModifierKey) { return false; } onTouch(intptr_t owner,skui::InputState,float x,float y)111 virtual bool onTouch(intptr_t owner, skui::InputState, float x, float y) { return false; } 112 // Platform-detected gesture events onFling(skui::InputState state)113 virtual bool onFling(skui::InputState state) { return false; } onPinch(skui::InputState state,float scale,float x,float y)114 virtual bool onPinch(skui::InputState state, float scale, float x, float y) { return false; } onUIStateChanged(const SkString & stateName,const SkString & stateValue)115 virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {} onPrePaint()116 virtual void onPrePaint() {} onPaint(SkSurface *)117 virtual void onPaint(SkSurface*) {} onResize(int width,int height)118 virtual void onResize(int width, int height) {} 119 120 private: 121 friend class Window; 122 bool fActive; 123 }; 124 pushLayer(Layer * layer)125 void pushLayer(Layer* layer) { 126 layer->onAttach(this); 127 fLayers.push_back(layer); 128 } 129 130 void onBackendCreated(); 131 bool onChar(SkUnichar c, skui::ModifierKey modifiers); 132 bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers); 133 bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers); 134 bool onMouseWheel(float delta, skui::ModifierKey modifiers); 135 bool onTouch(intptr_t owner, skui::InputState state, float x, float y); // multi-owner = multi-touch 136 // Platform-detected gesture events 137 bool onFling(skui::InputState state); 138 bool onPinch(skui::InputState state, float scale, float x, float y); 139 void onUIStateChanged(const SkString& stateName, const SkString& stateValue); 140 void onPaint(); 141 void onResize(int width, int height); 142 void onActivate(bool isActive); 143 144 int width() const; 145 int height() const; scaleFactor()146 virtual float scaleFactor() const { return 1.0f; } 147 getRequestedDisplayParams()148 virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; } 149 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true); 150 151 // Actual parameters in effect, obtained from the native window. 152 int sampleCount() const; 153 int stencilBits() const; 154 155 // Returns null if there is not a GPU backend or if the backend is not yet created. 156 GrDirectContext* directContext() const; 157 skgpu::graphite::Context* graphiteContext() const; 158 159 protected: 160 Window(); 161 162 SkTDArray<Layer*> fLayers; 163 DisplayParams fRequestedDisplayParams; 164 bool fIsActive = true; 165 166 std::unique_ptr<WindowContext> fWindowContext; 167 168 virtual void onInval() = 0; 169 170 // Uncheck fIsContentInvalided to allow future inval/onInval. 171 void markInvalProcessed(); 172 173 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events 174 175 void visitLayers(std::function<void(Layer*)> visitor); 176 bool signalLayers(std::function<bool(Layer*)> visitor); 177 }; 178 179 } // namespace sk_app 180 #endif 181