| 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 |
#include "tools/sk_app/Window.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkSurface.h" |
| 12 |
#include "tools/window/DisplayParams.h" |
| 13 |
#include "tools/window/WindowContext.h" |
| 14 |
|
| 15 |
#if defined(SK_GANESH) |
| 16 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 17 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 18 |
#endif |
| 19 |
|
| 20 |
#if defined(SK_GRAPHITE) |
| 21 |
#include "include/gpu/graphite/Recorder.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
using skwindow::DisplayParams; |
| 25 |
|
| 26 |
namespace sk_app { |
| 27 |
|
| 28 |
// Use the default DisplayParams |
| 29 |
Window::Window() : fRequestedDisplayParams(std::make_unique<DisplayParams>()) {} |
| 30 |
|
| 31 |
Window::~Window() {} |
| 32 |
|
| 33 |
void Window::detach() { fWindowContext = nullptr; } |
| 34 |
|
| 35 |
void Window::visitLayers(const std::function<void(Layer*)>& visitor) { |
| 36 |
for (int i = 0; i < fLayers.size(); ++i) { |
| 37 |
if (fLayers[i]->fActive) { |
| 38 |
visitor(fLayers[i]); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
bool Window::signalLayers(const std::function<bool(Layer*)>& visitor) { |
| 44 |
for (int i = fLayers.size() - 1; i >= 0; --i) { |
| 45 |
if (fLayers[i]->fActive && visitor(fLayers[i])) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
} |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
void Window::onBackendCreated() { |
| 53 |
this->visitLayers([](Layer* layer) { layer->onBackendCreated(); }); |
| 54 |
} |
| 55 |
|
| 56 |
bool Window::onChar(SkUnichar c, skui::ModifierKey modifiers) { |
| 57 |
return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); }); |
| 58 |
} |
| 59 |
|
| 60 |
bool Window::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) { |
| 61 |
return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); }); |
| 62 |
} |
| 63 |
|
| 64 |
bool Window::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) { |
| 65 |
return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); }); |
| 66 |
} |
| 67 |
|
| 68 |
bool Window::onMouseWheel(float delta, int x, int y, skui::ModifierKey modifiers) { |
| 69 |
return this->signalLayers( |
| 70 |
[=](Layer* layer) { return layer->onMouseWheel(delta, x, y, modifiers); }); |
| 71 |
} |
| 72 |
|
| 73 |
bool Window::onTouch(intptr_t owner, skui::InputState state, float x, float y) { |
| 74 |
return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); }); |
| 75 |
} |
| 76 |
|
| 77 |
bool Window::onFling(skui::InputState state) { |
| 78 |
return this->signalLayers([=](Layer* layer) { return layer->onFling(state); }); |
| 79 |
} |
| 80 |
|
| 81 |
bool Window::onPinch(skui::InputState state, float scale, float x, float y) { |
| 82 |
return this->signalLayers([=](Layer* layer) { return layer->onPinch(state, scale, x, y); }); |
| 83 |
} |
| 84 |
|
| 85 |
void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) { |
| 86 |
this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); }); |
| 87 |
} |
| 88 |
|
| 89 |
void Window::onPaint() { |
| 90 |
if (!fWindowContext) { |
| 91 |
return; |
| 92 |
} |
| 93 |
if (!fIsActive) { |
| 94 |
return; |
| 95 |
} |
| 96 |
sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface(); |
| 97 |
if (backbuffer == nullptr) { |
| 98 |
printf("no backbuffer!?\n"); |
| 99 |
// TODO: try recreating testcontext |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
markInvalProcessed(); |
| 104 |
|
| 105 |
// draw into the canvas of this surface |
| 106 |
this->visitLayers([](Layer* layer) { layer->onPrePaint(); }); |
| 107 |
this->visitLayers([=](Layer* layer) { layer->onPaint(backbuffer.get()); }); |
| 108 |
|
| 109 |
#if defined(SK_GANESH) |
| 110 |
if (auto dContext = this->directContext()) { |
| 111 |
dContext->flushAndSubmit(backbuffer.get(), GrSyncCpu::kNo); |
| 112 |
} |
| 113 |
#endif |
| 114 |
|
| 115 |
fWindowContext->swapBuffers(); |
| 116 |
} |
| 117 |
|
| 118 |
void Window::onResize(int w, int h) { |
| 119 |
if (!fWindowContext) { |
| 120 |
return; |
| 121 |
} |
| 122 |
fWindowContext->resize(w, h); |
| 123 |
this->visitLayers([=](Layer* layer) { layer->onResize(w, h); }); |
| 124 |
} |
| 125 |
|
| 126 |
void Window::onActivate(bool isActive) { |
| 127 |
if (fWindowContext) { |
| 128 |
fWindowContext->activate(isActive); |
| 129 |
} |
| 130 |
fIsActive = isActive; |
| 131 |
} |
| 132 |
|
| 133 |
int Window::width() const { |
| 134 |
if (!fWindowContext) { |
| 135 |
return 0; |
| 136 |
} |
| 137 |
return fWindowContext->width(); |
| 138 |
} |
| 139 |
|
| 140 |
int Window::height() const { |
| 141 |
if (!fWindowContext) { |
| 142 |
return 0; |
| 143 |
} |
| 144 |
return fWindowContext->height(); |
| 145 |
} |
| 146 |
|
| 147 |
void Window::setRequestedDisplayParams(std::unique_ptr<const DisplayParams> params, |
| 148 |
bool /* allowReattach */) { |
| 149 |
fRequestedDisplayParams = std::move(params); |
| 150 |
if (fWindowContext) { |
| 151 |
fWindowContext->setDisplayParams(fRequestedDisplayParams->clone()); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
int Window::sampleCount() const { |
| 156 |
if (!fWindowContext) { |
| 157 |
return 0; |
| 158 |
} |
| 159 |
return fWindowContext->sampleCount(); |
| 160 |
} |
| 161 |
|
| 162 |
int Window::stencilBits() const { |
| 163 |
if (!fWindowContext) { |
| 164 |
return -1; |
| 165 |
} |
| 166 |
return fWindowContext->stencilBits(); |
| 167 |
} |
| 168 |
|
| 169 |
GrDirectContext* Window::directContext() const { |
| 170 |
#if defined(SK_GANESH) |
| 171 |
if (!fWindowContext) { |
| 172 |
return nullptr; |
| 173 |
} |
| 174 |
return fWindowContext->directContext(); |
| 175 |
#else |
| 176 |
return nullptr; |
| 177 |
#endif |
| 178 |
} |
| 179 |
|
| 180 |
skgpu::graphite::Context* Window::graphiteContext() const { |
| 181 |
#if defined(SK_GRAPHITE) |
| 182 |
if (!fWindowContext) { |
| 183 |
return nullptr; |
| 184 |
} |
| 185 |
return fWindowContext->graphiteContext(); |
| 186 |
#else |
| 187 |
return nullptr; |
| 188 |
#endif |
| 189 |
} |
| 190 |
|
| 191 |
skgpu::graphite::Recorder* Window::graphiteRecorder() const { |
| 192 |
#if defined(SK_GRAPHITE) |
| 193 |
if (!fWindowContext) { |
| 194 |
return nullptr; |
| 195 |
} |
| 196 |
return fWindowContext->graphiteRecorder(); |
| 197 |
#else |
| 198 |
return nullptr; |
| 199 |
#endif |
| 200 |
} |
| 201 |
|
| 202 |
SkRecorder* Window::baseRecorder() const { |
| 203 |
#if defined(SK_GRAPHITE) |
| 204 |
if (auto r = this->graphiteRecorder()) { |
| 205 |
return r; |
| 206 |
} |
| 207 |
#endif |
| 208 |
#if defined(SK_GANESH) |
| 209 |
if (auto direct = this->directContext()) { |
| 210 |
return direct->asRecorder(); |
| 211 |
} |
| 212 |
#endif |
| 213 |
return nullptr; |
| 214 |
} |
| 215 |
|
| 216 |
bool Window::supportsGpuTimer() const { |
| 217 |
return fWindowContext ? fWindowContext->supportsGpuTimer() : false; |
| 218 |
} |
| 219 |
|
| 220 |
void Window::submitToGpu(GpuTimerCallback callback) { |
| 221 |
if (fWindowContext) { |
| 222 |
fWindowContext->submitToGpu(std::move(callback)); |
| 223 |
return; |
| 224 |
} |
| 225 |
if (callback) { |
| 226 |
callback(0); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
void Window::inval() { |
| 231 |
if (!fWindowContext) { |
| 232 |
return; |
| 233 |
} |
| 234 |
if (!fIsContentInvalidated) { |
| 235 |
fIsContentInvalidated = true; |
| 236 |
onInval(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void Window::markInvalProcessed() { |
| 241 |
fIsContentInvalidated = false; |
| 242 |
} |
| 243 |
|
| 244 |
} // namespace sk_app |