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 16 Window::Window() {} 17 18 void Window::detach() { 19 delete fWindowContext; 20 fWindowContext = nullptr; 21 } 22 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 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 40 void Window::onBackendCreated() { 41 this->visitLayers([](Layer* layer) { layer->onBackendCreated(); }); 42 } 43 44 bool Window::onChar(SkUnichar c, uint32_t modifiers) { 45 return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); }); 46 } 47 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 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 56 bool Window::onMouseWheel(float delta, uint32_t modifiers) { 57 return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); }); 58 } 59 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 64 void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) { 65 this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); }); 66 } 67 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 this->visitLayers([](Layer* layer) { layer->onPrePaint(); }); 77 this->visitLayers([=](Layer* layer) { layer->onPaint(backbuffer.get()); }); 78 79 backbuffer->flush(); 80 81 fWindowContext->swapBuffers(); 82 } else { 83 printf("no backbuffer!?\n"); 84 // try recreating testcontext 85 } 86 } 87 88 void Window::onResize(int w, int h) { 89 if (!fWindowContext) { 90 return; 91 } 92 fWindowContext->resize(w, h); 93 this->visitLayers([=](Layer* layer) { layer->onResize(w, h); }); 94 } 95 96 int Window::width() const { 97 if (!fWindowContext) { 98 return 0; 99 } 100 return fWindowContext->width(); 101 } 102 103 int Window::height() const { 104 if (!fWindowContext) { 105 return 0; 106 } 107 return fWindowContext->height(); 108 } 109 110 void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) { 111 fRequestedDisplayParams = params; 112 if (fWindowContext) { 113 fWindowContext->setDisplayParams(fRequestedDisplayParams); 114 } 115 } 116 117 int Window::sampleCount() const { 118 if (!fWindowContext) { 119 return 0; 120 } 121 return fWindowContext->sampleCount(); 122 } 123 124 int Window::stencilBits() const { 125 if (!fWindowContext) { 126 return -1; 127 } 128 return fWindowContext->stencilBits(); 129 } 130 131 GrContext* Window::getGrContext() const { 132 if (!fWindowContext) { 133 return nullptr; 134 } 135 return fWindowContext->getGrContext(); 136 } 137 138 void Window::inval() { 139 if (!fWindowContext) { 140 return; 141 } 142 if (!fIsContentInvalidated) { 143 fIsContentInvalidated = true; 144 onInval(); 145 } 146 } 147 148 void Window::markInvalProcessed() { 149 fIsContentInvalidated = false; 150 } 151 152 } // namespace sk_app 153