jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 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" |
jvanverth | 063ece7 | 2016-06-17 09:29:14 -0700 | [diff] [blame] | 12 | #include "WindowContext.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 13 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 14 | namespace sk_app { |
| 15 | |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 16 | Window::Window() {} |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 17 | |
| 18 | void Window::detach() { |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 19 | delete fWindowContext; |
| 20 | fWindowContext = nullptr; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 23 | void Window::visitLayers(std::function<void(Layer*)> visitor) { |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 24 | for (int i = 0; i < fLayers.count(); ++i) { |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 25 | if (fLayers[i]->fActive) { |
| 26 | visitor(fLayers[i]); |
| 27 | } |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 28 | } |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 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 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 44 | bool Window::onChar(SkUnichar c, uint32_t modifiers) { |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 45 | return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); }); |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | bool Window::onKey(Key key, InputState state, uint32_t modifiers) { |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 49 | return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); }); |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool Window::onMouse(int x, int y, InputState state, uint32_t modifiers) { |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 53 | return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); }); |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 56 | bool Window::onMouseWheel(float delta, uint32_t modifiers) { |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 57 | return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); }); |
Brian Osman | 79086b9 | 2017-02-10 13:36:16 -0500 | [diff] [blame] | 58 | } |
| 59 | |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 60 | bool Window::onTouch(intptr_t owner, InputState state, float x, float y) { |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 61 | return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); }); |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 62 | } |
| 63 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 64 | void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) { |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 65 | this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); }); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 66 | } |
| 67 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 68 | void Window::onPaint() { |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 69 | if (!fWindowContext) { |
| 70 | return; |
| 71 | } |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 72 | markInvalProcessed(); |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 73 | sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 74 | if (backbuffer) { |
| 75 | // draw into the canvas of this surface |
| 76 | SkCanvas* canvas = backbuffer->getCanvas(); |
| 77 | |
Brian Osman | 56a2481 | 2017-12-19 11:15:16 -0500 | [diff] [blame] | 78 | this->visitLayers([](Layer* layer) { layer->onPrePaint(); }); |
| 79 | this->visitLayers([=](Layer* layer) { layer->onPaint(canvas); }); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 80 | |
| 81 | canvas->flush(); |
| 82 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 83 | fWindowContext->swapBuffers(); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 84 | } else { |
jvanverth | 85f758c | 2016-05-27 06:47:08 -0700 | [diff] [blame] | 85 | printf("no backbuffer!?\n"); |
jvanverth | 3d6ed3a | 2016-04-07 11:09:51 -0700 | [diff] [blame] | 86 | // try recreating testcontext |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 87 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 88 | } |
| 89 | |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 90 | void Window::onResize(int w, int h) { |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 91 | if (!fWindowContext) { |
| 92 | return; |
| 93 | } |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 94 | fWindowContext->resize(w, h); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 95 | } |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 96 | |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 97 | int Window::width() { |
| 98 | if (!fWindowContext) { |
| 99 | return 0; |
| 100 | } |
| 101 | return fWindowContext->width(); |
| 102 | } |
| 103 | |
| 104 | int Window::height() { |
| 105 | if (!fWindowContext) { |
| 106 | return 0; |
| 107 | } |
| 108 | return fWindowContext->height(); |
| 109 | } |
| 110 | |
Brian Osman | 2ac96dc | 2017-06-23 13:32:29 -0400 | [diff] [blame] | 111 | void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) { |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 112 | fRequestedDisplayParams = params; |
| 113 | if (fWindowContext) { |
| 114 | fWindowContext->setDisplayParams(fRequestedDisplayParams); |
| 115 | } |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 116 | } |
| 117 | |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 118 | int Window::sampleCount() const { |
| 119 | if (!fWindowContext) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame^] | 120 | return 0; |
csmartdalton | 578f064 | 2017-02-24 16:04:47 -0700 | [diff] [blame] | 121 | } |
| 122 | return fWindowContext->sampleCount(); |
| 123 | } |
| 124 | |
| 125 | int Window::stencilBits() const { |
| 126 | if (!fWindowContext) { |
| 127 | return -1; |
| 128 | } |
| 129 | return fWindowContext->stencilBits(); |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 130 | } |
| 131 | |
csmartdalton | 61cd31a | 2017-02-27 17:00:53 -0700 | [diff] [blame] | 132 | const GrContext* Window::getGrContext() const { |
| 133 | if (!fWindowContext) { |
| 134 | return nullptr; |
| 135 | } |
| 136 | return fWindowContext->getGrContext(); |
| 137 | } |
| 138 | |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 139 | void Window::inval() { |
Christopher Dalton | 443ec1b | 2017-02-24 13:22:53 -0700 | [diff] [blame] | 140 | if (!fWindowContext) { |
| 141 | return; |
| 142 | } |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 143 | if (!fIsContentInvalidated) { |
| 144 | fIsContentInvalidated = true; |
| 145 | onInval(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void Window::markInvalProcessed() { |
| 150 | fIsContentInvalidated = false; |
| 151 | } |
| 152 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 153 | } // namespace sk_app |