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