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/sk_app/WindowContext.h"
13
14 namespace sk_app {
15
Window()16 Window::Window() {}
17
~Window()18 Window::~Window() {}
19
detach()20 void Window::detach() { fWindowContext = nullptr; }
21
visitLayers(std::function<void (Layer *)> visitor)22 void Window::visitLayers(std::function<void(Layer*)> visitor) {
23 for (int i = 0; i < fLayers.size(); ++i) {
24 if (fLayers[i]->fActive) {
25 visitor(fLayers[i]);
26 }
27 }
28 }
29
signalLayers(std::function<bool (Layer *)> visitor)30 bool Window::signalLayers(std::function<bool(Layer*)> visitor) {
31 for (int i = fLayers.size() - 1; i >= 0; --i) {
32 if (fLayers[i]->fActive && visitor(fLayers[i])) {
33 return true;
34 }
35 }
36 return false;
37 }
38
onBackendCreated()39 void Window::onBackendCreated() {
40 this->visitLayers([](Layer* layer) { layer->onBackendCreated(); });
41 }
42
onChar(SkUnichar c,skui::ModifierKey modifiers)43 bool Window::onChar(SkUnichar c, skui::ModifierKey modifiers) {
44 return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
45 }
46
onKey(skui::Key key,skui::InputState state,skui::ModifierKey modifiers)47 bool Window::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
48 return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); });
49 }
50
onMouse(int x,int y,skui::InputState state,skui::ModifierKey modifiers)51 bool Window::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
52 return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); });
53 }
54
onMouseWheel(float delta,skui::ModifierKey modifiers)55 bool Window::onMouseWheel(float delta, skui::ModifierKey modifiers) {
56 return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); });
57 }
58
onTouch(intptr_t owner,skui::InputState state,float x,float y)59 bool Window::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
60 return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); });
61 }
62
onFling(skui::InputState state)63 bool Window::onFling(skui::InputState state) {
64 return this->signalLayers([=](Layer* layer) { return layer->onFling(state); });
65 }
66
onPinch(skui::InputState state,float scale,float x,float y)67 bool Window::onPinch(skui::InputState state, float scale, float x, float y) {
68 return this->signalLayers([=](Layer* layer) { return layer->onPinch(state, scale, x, y); });
69 }
70
onUIStateChanged(const SkString & stateName,const SkString & stateValue)71 void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
72 this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); });
73 }
74
onPaint()75 void Window::onPaint() {
76 if (!fWindowContext) {
77 return;
78 }
79 if (!fIsActive) {
80 return;
81 }
82 sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
83 if (backbuffer == nullptr) {
84 printf("no backbuffer!?\n");
85 // TODO: try recreating testcontext
86 return;
87 }
88
89 markInvalProcessed();
90
91 // draw into the canvas of this surface
92 this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
93 this->visitLayers([=](Layer* layer) { layer->onPaint(backbuffer.get()); });
94
95 backbuffer->flushAndSubmit();
96
97 fWindowContext->swapBuffers();
98 }
99
onResize(int w,int h)100 void Window::onResize(int w, int h) {
101 if (!fWindowContext) {
102 return;
103 }
104 fWindowContext->resize(w, h);
105 this->visitLayers([=](Layer* layer) { layer->onResize(w, h); });
106 }
107
onActivate(bool isActive)108 void Window::onActivate(bool isActive) {
109 if (fWindowContext) {
110 fWindowContext->activate(isActive);
111 }
112 fIsActive = isActive;
113 }
114
width() const115 int Window::width() const {
116 if (!fWindowContext) {
117 return 0;
118 }
119 return fWindowContext->width();
120 }
121
height() const122 int Window::height() const {
123 if (!fWindowContext) {
124 return 0;
125 }
126 return fWindowContext->height();
127 }
128
setRequestedDisplayParams(const DisplayParams & params,bool)129 void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
130 fRequestedDisplayParams = params;
131 if (fWindowContext) {
132 fWindowContext->setDisplayParams(fRequestedDisplayParams);
133 }
134 }
135
sampleCount() const136 int Window::sampleCount() const {
137 if (!fWindowContext) {
138 return 0;
139 }
140 return fWindowContext->sampleCount();
141 }
142
stencilBits() const143 int Window::stencilBits() const {
144 if (!fWindowContext) {
145 return -1;
146 }
147 return fWindowContext->stencilBits();
148 }
149
directContext() const150 GrDirectContext* Window::directContext() const {
151 if (!fWindowContext) {
152 return nullptr;
153 }
154 return fWindowContext->directContext();
155 }
156
inval()157 void Window::inval() {
158 if (!fWindowContext) {
159 return;
160 }
161 if (!fIsContentInvalidated) {
162 fIsContentInvalidated = true;
163 onInval();
164 }
165 }
166
markInvalProcessed()167 void Window::markInvalProcessed() {
168 fIsContentInvalidated = false;
169 }
170
171 } // namespace sk_app
172