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