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 "Window.h"
9
10 #include "SkSurface.h"
11 #include "SkCanvas.h"
12 #include "WindowContext.h"
13
14 namespace sk_app {
15
Window()16 Window::Window() {}
17
detach()18 void Window::detach() {
19 delete fWindowContext;
20 fWindowContext = nullptr;
21 }
22
visitLayers(std::function<void (Layer *)> visitor)23 void Window::visitLayers(std::function<void(Layer*)> visitor) {
24 for (int i = 0; i < fLayers.count(); ++i) {
25 if (fLayers[i]->fActive) {
26 visitor(fLayers[i]);
27 }
28 }
29 }
30
signalLayers(std::function<bool (Layer *)> visitor)31 bool Window::signalLayers(std::function<bool(Layer*)> visitor) {
32 for (int i = fLayers.count() - 1; i >= 0; --i) {
33 if (fLayers[i]->fActive && visitor(fLayers[i])) {
34 return true;
35 }
36 }
37 return false;
38 }
39
onBackendCreated()40 void Window::onBackendCreated() {
41 this->visitLayers([](Layer* layer) { layer->onBackendCreated(); });
42 }
43
onChar(SkUnichar c,uint32_t modifiers)44 bool Window::onChar(SkUnichar c, uint32_t modifiers) {
45 return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
46 }
47
onKey(Key key,InputState state,uint32_t modifiers)48 bool Window::onKey(Key key, InputState state, uint32_t modifiers) {
49 return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); });
50 }
51
onMouse(int x,int y,InputState state,uint32_t modifiers)52 bool Window::onMouse(int x, int y, InputState state, uint32_t modifiers) {
53 return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); });
54 }
55
onMouseWheel(float delta,uint32_t modifiers)56 bool Window::onMouseWheel(float delta, uint32_t modifiers) {
57 return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); });
58 }
59
onTouch(intptr_t owner,InputState state,float x,float y)60 bool Window::onTouch(intptr_t owner, InputState state, float x, float y) {
61 return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); });
62 }
63
onUIStateChanged(const SkString & stateName,const SkString & stateValue)64 void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
65 this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); });
66 }
67
onPaint()68 void Window::onPaint() {
69 if (!fWindowContext) {
70 return;
71 }
72 markInvalProcessed();
73 sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
74 if (backbuffer) {
75 // draw into the canvas of this surface
76 SkCanvas* canvas = backbuffer->getCanvas();
77
78 this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
79 this->visitLayers([=](Layer* layer) { layer->onPaint(canvas); });
80
81 canvas->flush();
82
83 fWindowContext->swapBuffers();
84 } else {
85 printf("no backbuffer!?\n");
86 // try recreating testcontext
87 }
88 }
89
onResize(int w,int h)90 void Window::onResize(int w, int h) {
91 if (!fWindowContext) {
92 return;
93 }
94 fWindowContext->resize(w, h);
95 this->visitLayers([=](Layer* layer) { layer->onResize(w, h); });
96 }
97
width()98 int Window::width() {
99 if (!fWindowContext) {
100 return 0;
101 }
102 return fWindowContext->width();
103 }
104
height()105 int Window::height() {
106 if (!fWindowContext) {
107 return 0;
108 }
109 return fWindowContext->height();
110 }
111
setRequestedDisplayParams(const DisplayParams & params,bool)112 void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
113 fRequestedDisplayParams = params;
114 if (fWindowContext) {
115 fWindowContext->setDisplayParams(fRequestedDisplayParams);
116 }
117 }
118
sampleCount() const119 int Window::sampleCount() const {
120 if (!fWindowContext) {
121 return 0;
122 }
123 return fWindowContext->sampleCount();
124 }
125
stencilBits() const126 int Window::stencilBits() const {
127 if (!fWindowContext) {
128 return -1;
129 }
130 return fWindowContext->stencilBits();
131 }
132
getGrContext() const133 GrContext* Window::getGrContext() const {
134 if (!fWindowContext) {
135 return nullptr;
136 }
137 return fWindowContext->getGrContext();
138 }
139
inval()140 void Window::inval() {
141 if (!fWindowContext) {
142 return;
143 }
144 if (!fIsContentInvalidated) {
145 fIsContentInvalidated = true;
146 onInval();
147 }
148 }
149
markInvalProcessed()150 void Window::markInvalProcessed() {
151 fIsContentInvalidated = false;
152 }
153
154 } // namespace sk_app
155