blob: 1694beaac84e4a912107d20e0b16ed7330817b71 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001/*
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"
jvanverth063ece72016-06-17 09:29:14 -070012#include "WindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070013
jvanverth34524262016-05-04 13:49:13 -070014namespace sk_app {
15
Brian Osman80fc07e2017-12-08 16:45:43 -050016Window::Window() {}
jvanverth9f372462016-04-06 06:08:59 -070017
18void Window::detach() {
jvanvertha8d0d6c2016-05-05 12:32:03 -070019 delete fWindowContext;
20 fWindowContext = nullptr;
jvanverth9f372462016-04-06 06:08:59 -070021}
22
Brian Osman56a24812017-12-19 11:15:16 -050023void Window::visitLayers(std::function<void(Layer*)> visitor) {
Brian Osman80fc07e2017-12-08 16:45:43 -050024 for (int i = 0; i < fLayers.count(); ++i) {
Brian Osman56a24812017-12-19 11:15:16 -050025 if (fLayers[i]->fActive) {
26 visitor(fLayers[i]);
27 }
Brian Osman80fc07e2017-12-08 16:45:43 -050028 }
Christopher Dalton443ec1b2017-02-24 13:22:53 -070029}
30
Brian Osman56a24812017-12-19 11:15:16 -050031bool 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
40void Window::onBackendCreated() {
41 this->visitLayers([](Layer* layer) { layer->onBackendCreated(); });
42}
43
jvanverth9fab59d2016-04-06 12:08:51 -070044bool Window::onChar(SkUnichar c, uint32_t modifiers) {
Brian Osman56a24812017-12-19 11:15:16 -050045 return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
jvanverth9fab59d2016-04-06 12:08:51 -070046}
47
48bool Window::onKey(Key key, InputState state, uint32_t modifiers) {
Brian Osman56a24812017-12-19 11:15:16 -050049 return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); });
jvanverth9fab59d2016-04-06 12:08:51 -070050}
51
52bool Window::onMouse(int x, int y, InputState state, uint32_t modifiers) {
Brian Osman56a24812017-12-19 11:15:16 -050053 return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); });
jvanverth9fab59d2016-04-06 12:08:51 -070054}
55
Brian Osman79086b92017-02-10 13:36:16 -050056bool Window::onMouseWheel(float delta, uint32_t modifiers) {
Brian Osman56a24812017-12-19 11:15:16 -050057 return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); });
Brian Osman79086b92017-02-10 13:36:16 -050058}
59
jvanverth814e38d2016-06-06 08:48:47 -070060bool Window::onTouch(intptr_t owner, InputState state, float x, float y) {
Brian Osman56a24812017-12-19 11:15:16 -050061 return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); });
liyuqiand3cdbca2016-05-17 12:44:20 -070062}
63
liyuqiane5a6cd92016-05-27 08:52:52 -070064void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
Brian Osman56a24812017-12-19 11:15:16 -050065 this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); });
liyuqiane5a6cd92016-05-27 08:52:52 -070066}
67
jvanverth9f372462016-04-06 06:08:59 -070068void Window::onPaint() {
Christopher Dalton443ec1b2017-02-24 13:22:53 -070069 if (!fWindowContext) {
70 return;
71 }
liyuqian566c8e42016-05-23 10:52:34 -070072 markInvalProcessed();
jvanverthaf236b52016-05-20 06:01:06 -070073 sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
jvanverth9f372462016-04-06 06:08:59 -070074 if (backbuffer) {
75 // draw into the canvas of this surface
76 SkCanvas* canvas = backbuffer->getCanvas();
77
Brian Osman56a24812017-12-19 11:15:16 -050078 this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
79 this->visitLayers([=](Layer* layer) { layer->onPaint(canvas); });
jvanverth9f372462016-04-06 06:08:59 -070080
81 canvas->flush();
82
jvanvertha8d0d6c2016-05-05 12:32:03 -070083 fWindowContext->swapBuffers();
jvanverth3d6ed3a2016-04-07 11:09:51 -070084 } else {
jvanverth85f758c2016-05-27 06:47:08 -070085 printf("no backbuffer!?\n");
jvanverth3d6ed3a2016-04-07 11:09:51 -070086 // try recreating testcontext
jvanverth9f372462016-04-06 06:08:59 -070087 }
jvanverth9f372462016-04-06 06:08:59 -070088}
89
bsalomonccde4ab2016-07-27 08:50:12 -070090void Window::onResize(int w, int h) {
Christopher Dalton443ec1b2017-02-24 13:22:53 -070091 if (!fWindowContext) {
92 return;
93 }
jvanvertha8d0d6c2016-05-05 12:32:03 -070094 fWindowContext->resize(w, h);
jvanverth9f372462016-04-06 06:08:59 -070095}
jvanverth34524262016-05-04 13:49:13 -070096
Christopher Dalton443ec1b2017-02-24 13:22:53 -070097int Window::width() {
98 if (!fWindowContext) {
99 return 0;
100 }
101 return fWindowContext->width();
102}
103
104int Window::height() {
105 if (!fWindowContext) {
106 return 0;
107 }
108 return fWindowContext->height();
109}
110
Brian Osman2ac96dc2017-06-23 13:32:29 -0400111void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
csmartdalton578f0642017-02-24 16:04:47 -0700112 fRequestedDisplayParams = params;
113 if (fWindowContext) {
114 fWindowContext->setDisplayParams(fRequestedDisplayParams);
115 }
brianosman05de2162016-05-06 13:28:57 -0700116}
117
csmartdalton578f0642017-02-24 16:04:47 -0700118int Window::sampleCount() const {
119 if (!fWindowContext) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500120 return 0;
csmartdalton578f0642017-02-24 16:04:47 -0700121 }
122 return fWindowContext->sampleCount();
123}
124
125int Window::stencilBits() const {
126 if (!fWindowContext) {
127 return -1;
128 }
129 return fWindowContext->stencilBits();
brianosman05de2162016-05-06 13:28:57 -0700130}
131
csmartdalton61cd31a2017-02-27 17:00:53 -0700132const GrContext* Window::getGrContext() const {
133 if (!fWindowContext) {
134 return nullptr;
135 }
136 return fWindowContext->getGrContext();
137}
138
liyuqian566c8e42016-05-23 10:52:34 -0700139void Window::inval() {
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700140 if (!fWindowContext) {
141 return;
142 }
liyuqian566c8e42016-05-23 10:52:34 -0700143 if (!fIsContentInvalidated) {
144 fIsContentInvalidated = true;
145 onInval();
146 }
147}
148
149void Window::markInvalProcessed() {
150 fIsContentInvalidated = false;
151}
152
jvanverth34524262016-05-04 13:49:13 -0700153} // namespace sk_app