blob: 2ccad5580b0c60844341070307f8890678a158c1 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/sk_app/Window.h"
jvanverth9f372462016-04-06 06:08:59 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkSurface.h"
12#include "tools/sk_app/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
Hal Canary149f3f12019-08-01 16:21:49 -040018Window::~Window() {}
19
20void Window::detach() { fWindowContext = nullptr; }
jvanverth9f372462016-04-06 06:08:59 -070021
Brian Osman56a24812017-12-19 11:15:16 -050022void Window::visitLayers(std::function<void(Layer*)> visitor) {
Brian Osman80fc07e2017-12-08 16:45:43 -050023 for (int i = 0; i < fLayers.count(); ++i) {
Brian Osman56a24812017-12-19 11:15:16 -050024 if (fLayers[i]->fActive) {
25 visitor(fLayers[i]);
26 }
Brian Osman80fc07e2017-12-08 16:45:43 -050027 }
Christopher Dalton443ec1b2017-02-24 13:22:53 -070028}
29
Brian Osman56a24812017-12-19 11:15:16 -050030bool 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
39void Window::onBackendCreated() {
40 this->visitLayers([](Layer* layer) { layer->onBackendCreated(); });
41}
42
Hal Canaryb1f411a2019-08-29 10:39:22 -040043bool Window::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osman56a24812017-12-19 11:15:16 -050044 return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
jvanverth9fab59d2016-04-06 12:08:51 -070045}
46
Hal Canaryb1f411a2019-08-29 10:39:22 -040047bool Window::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman56a24812017-12-19 11:15:16 -050048 return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); });
jvanverth9fab59d2016-04-06 12:08:51 -070049}
50
Hal Canaryb1f411a2019-08-29 10:39:22 -040051bool Window::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
Brian Osman56a24812017-12-19 11:15:16 -050052 return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); });
jvanverth9fab59d2016-04-06 12:08:51 -070053}
54
Hal Canaryb1f411a2019-08-29 10:39:22 -040055bool Window::onMouseWheel(float delta, skui::ModifierKey modifiers) {
Brian Osman56a24812017-12-19 11:15:16 -050056 return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); });
Brian Osman79086b92017-02-10 13:36:16 -050057}
58
Hal Canaryb1f411a2019-08-29 10:39:22 -040059bool Window::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
Brian Osman56a24812017-12-19 11:15:16 -050060 return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); });
liyuqiand3cdbca2016-05-17 12:44:20 -070061}
62
Jim Van Verthd0cf5da2019-09-09 16:53:39 -040063bool Window::onFling(skui::InputState state) {
64 return this->signalLayers([=](Layer* layer) { return layer->onFling(state); });
65}
66
67bool Window::onPinch(skui::InputState state, float scale, float x, float y) {
68 return this->signalLayers([=](Layer* layer) { return layer->onPinch(state, scale, x, y); });
69}
70
liyuqiane5a6cd92016-05-27 08:52:52 -070071void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
Brian Osman56a24812017-12-19 11:15:16 -050072 this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); });
liyuqiane5a6cd92016-05-27 08:52:52 -070073}
74
jvanverth9f372462016-04-06 06:08:59 -070075void Window::onPaint() {
Christopher Dalton443ec1b2017-02-24 13:22:53 -070076 if (!fWindowContext) {
77 return;
78 }
liyuqian566c8e42016-05-23 10:52:34 -070079 markInvalProcessed();
Greg Daniel655002b2019-03-21 14:43:00 -040080 this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
jvanverthaf236b52016-05-20 06:01:06 -070081 sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
jvanverth9f372462016-04-06 06:08:59 -070082 if (backbuffer) {
83 // draw into the canvas of this surface
Robert Phillips9882dae2019-03-04 11:00:10 -050084 this->visitLayers([=](Layer* layer) { layer->onPaint(backbuffer.get()); });
jvanverth9f372462016-04-06 06:08:59 -070085
Robert Phillips9882dae2019-03-04 11:00:10 -050086 backbuffer->flush();
jvanverth9f372462016-04-06 06:08:59 -070087
jvanvertha8d0d6c2016-05-05 12:32:03 -070088 fWindowContext->swapBuffers();
jvanverth3d6ed3a2016-04-07 11:09:51 -070089 } else {
jvanverth85f758c2016-05-27 06:47:08 -070090 printf("no backbuffer!?\n");
jvanverth3d6ed3a2016-04-07 11:09:51 -070091 // try recreating testcontext
jvanverth9f372462016-04-06 06:08:59 -070092 }
jvanverth9f372462016-04-06 06:08:59 -070093}
94
bsalomonccde4ab2016-07-27 08:50:12 -070095void Window::onResize(int w, int h) {
Christopher Dalton443ec1b2017-02-24 13:22:53 -070096 if (!fWindowContext) {
97 return;
98 }
jvanvertha8d0d6c2016-05-05 12:32:03 -070099 fWindowContext->resize(w, h);
Ben Wagnera1915972018-08-09 15:06:19 -0400100 this->visitLayers([=](Layer* layer) { layer->onResize(w, h); });
jvanverth9f372462016-04-06 06:08:59 -0700101}
jvanverth34524262016-05-04 13:49:13 -0700102
Jim Van Verth98385ba2019-02-06 11:23:34 -0500103int Window::width() const {
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700104 if (!fWindowContext) {
105 return 0;
106 }
107 return fWindowContext->width();
108}
109
Jim Van Verth98385ba2019-02-06 11:23:34 -0500110int Window::height() const {
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700111 if (!fWindowContext) {
112 return 0;
113 }
114 return fWindowContext->height();
115}
116
Brian Osman2ac96dc2017-06-23 13:32:29 -0400117void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
csmartdalton578f0642017-02-24 16:04:47 -0700118 fRequestedDisplayParams = params;
119 if (fWindowContext) {
120 fWindowContext->setDisplayParams(fRequestedDisplayParams);
121 }
brianosman05de2162016-05-06 13:28:57 -0700122}
123
csmartdalton578f0642017-02-24 16:04:47 -0700124int Window::sampleCount() const {
125 if (!fWindowContext) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500126 return 0;
csmartdalton578f0642017-02-24 16:04:47 -0700127 }
128 return fWindowContext->sampleCount();
129}
130
131int Window::stencilBits() const {
132 if (!fWindowContext) {
133 return -1;
134 }
135 return fWindowContext->stencilBits();
brianosman05de2162016-05-06 13:28:57 -0700136}
137
Chris Dalton89305752018-11-01 10:52:34 -0600138GrContext* Window::getGrContext() const {
csmartdalton61cd31a2017-02-27 17:00:53 -0700139 if (!fWindowContext) {
140 return nullptr;
141 }
142 return fWindowContext->getGrContext();
143}
144
liyuqian566c8e42016-05-23 10:52:34 -0700145void Window::inval() {
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700146 if (!fWindowContext) {
147 return;
148 }
liyuqian566c8e42016-05-23 10:52:34 -0700149 if (!fIsContentInvalidated) {
150 fIsContentInvalidated = true;
151 onInval();
152 }
153}
154
155void Window::markInvalProcessed() {
156 fIsContentInvalidated = false;
157}
158
jvanverth34524262016-05-04 13:49:13 -0700159} // namespace sk_app