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.count(); ++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.count() - 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,ModifierKey modifiers)43 bool Window::onChar(SkUnichar c, ModifierKey modifiers) {
44 return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
45 }
46
onKey(Key key,InputState state,ModifierKey modifiers)47 bool Window::onKey(Key key, InputState state, ModifierKey modifiers) {
48 return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); });
49 }
50
onMouse(int x,int y,InputState state,ModifierKey modifiers)51 bool Window::onMouse(int x, int y, InputState state, ModifierKey modifiers) {
52 return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); });
53 }
54
onMouseWheel(float delta,ModifierKey modifiers)55 bool Window::onMouseWheel(float delta, ModifierKey modifiers) {
56 return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); });
57 }
58
onTouch(intptr_t owner,InputState state,float x,float y)59 bool Window::onTouch(intptr_t owner, InputState state, float x, float y) {
60 return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); });
61 }
62
onUIStateChanged(const SkString & stateName,const SkString & stateValue)63 void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
64 this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); });
65 }
66
onPaint()67 void Window::onPaint() {
68 if (!fWindowContext) {
69 return;
70 }
71 markInvalProcessed();
72 this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
73 sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
74 if (backbuffer) {
75 // draw into the canvas of this surface
76 this->visitLayers([=](Layer* layer) { layer->onPaint(backbuffer.get()); });
77
78 backbuffer->flush();
79
80 fWindowContext->swapBuffers();
81 } else {
82 printf("no backbuffer!?\n");
83 // try recreating testcontext
84 }
85 }
86
onResize(int w,int h)87 void Window::onResize(int w, int h) {
88 if (!fWindowContext) {
89 return;
90 }
91 fWindowContext->resize(w, h);
92 this->visitLayers([=](Layer* layer) { layer->onResize(w, h); });
93 }
94
width() const95 int Window::width() const {
96 if (!fWindowContext) {
97 return 0;
98 }
99 return fWindowContext->width();
100 }
101
height() const102 int Window::height() const {
103 if (!fWindowContext) {
104 return 0;
105 }
106 return fWindowContext->height();
107 }
108
setRequestedDisplayParams(const DisplayParams & params,bool)109 void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
110 fRequestedDisplayParams = params;
111 if (fWindowContext) {
112 fWindowContext->setDisplayParams(fRequestedDisplayParams);
113 }
114 }
115
sampleCount() const116 int Window::sampleCount() const {
117 if (!fWindowContext) {
118 return 0;
119 }
120 return fWindowContext->sampleCount();
121 }
122
stencilBits() const123 int Window::stencilBits() const {
124 if (!fWindowContext) {
125 return -1;
126 }
127 return fWindowContext->stencilBits();
128 }
129
getGrContext() const130 GrContext* Window::getGrContext() const {
131 if (!fWindowContext) {
132 return nullptr;
133 }
134 return fWindowContext->getGrContext();
135 }
136
inval()137 void Window::inval() {
138 if (!fWindowContext) {
139 return;
140 }
141 if (!fIsContentInvalidated) {
142 fIsContentInvalidated = true;
143 onInval();
144 }
145 }
146
markInvalProcessed()147 void Window::markInvalProcessed() {
148 fIsContentInvalidated = false;
149 }
150
151 } // namespace sk_app
152