1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci* Copyright 2016 Google Inc.
3cb93a386Sopenharmony_ci*
4cb93a386Sopenharmony_ci* Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci* found in the LICENSE file.
6cb93a386Sopenharmony_ci*/
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "tools/sk_app/Window.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
11cb93a386Sopenharmony_ci#include "include/core/SkSurface.h"
12cb93a386Sopenharmony_ci#include "tools/sk_app/WindowContext.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_cinamespace sk_app {
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ciWindow::Window() {}
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ciWindow::~Window() {}
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_civoid Window::detach() { fWindowContext = nullptr; }
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_civoid Window::visitLayers(std::function<void(Layer*)> visitor) {
23cb93a386Sopenharmony_ci    for (int i = 0; i < fLayers.count(); ++i) {
24cb93a386Sopenharmony_ci        if (fLayers[i]->fActive) {
25cb93a386Sopenharmony_ci            visitor(fLayers[i]);
26cb93a386Sopenharmony_ci        }
27cb93a386Sopenharmony_ci    }
28cb93a386Sopenharmony_ci}
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_cibool Window::signalLayers(std::function<bool(Layer*)> visitor) {
31cb93a386Sopenharmony_ci    for (int i = fLayers.count() - 1; i >= 0; --i) {
32cb93a386Sopenharmony_ci        if (fLayers[i]->fActive && visitor(fLayers[i])) {
33cb93a386Sopenharmony_ci            return true;
34cb93a386Sopenharmony_ci        }
35cb93a386Sopenharmony_ci    }
36cb93a386Sopenharmony_ci    return false;
37cb93a386Sopenharmony_ci}
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_civoid Window::onBackendCreated() {
40cb93a386Sopenharmony_ci    this->visitLayers([](Layer* layer) { layer->onBackendCreated(); });
41cb93a386Sopenharmony_ci}
42cb93a386Sopenharmony_ci
43cb93a386Sopenharmony_cibool Window::onChar(SkUnichar c, skui::ModifierKey modifiers) {
44cb93a386Sopenharmony_ci    return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
45cb93a386Sopenharmony_ci}
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_cibool Window::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
48cb93a386Sopenharmony_ci    return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); });
49cb93a386Sopenharmony_ci}
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_cibool Window::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
52cb93a386Sopenharmony_ci    return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); });
53cb93a386Sopenharmony_ci}
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_cibool Window::onMouseWheel(float delta, skui::ModifierKey modifiers) {
56cb93a386Sopenharmony_ci    return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); });
57cb93a386Sopenharmony_ci}
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_cibool Window::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
60cb93a386Sopenharmony_ci    return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); });
61cb93a386Sopenharmony_ci}
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_cibool Window::onFling(skui::InputState state) {
64cb93a386Sopenharmony_ci    return this->signalLayers([=](Layer* layer) { return layer->onFling(state); });
65cb93a386Sopenharmony_ci}
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_cibool Window::onPinch(skui::InputState state, float scale, float x, float y) {
68cb93a386Sopenharmony_ci    return this->signalLayers([=](Layer* layer) { return layer->onPinch(state, scale, x, y); });
69cb93a386Sopenharmony_ci}
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_civoid Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
72cb93a386Sopenharmony_ci    this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); });
73cb93a386Sopenharmony_ci}
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_civoid Window::onPaint() {
76cb93a386Sopenharmony_ci    if (!fWindowContext) {
77cb93a386Sopenharmony_ci        return;
78cb93a386Sopenharmony_ci    }
79cb93a386Sopenharmony_ci    if (!fIsActive) {
80cb93a386Sopenharmony_ci        return;
81cb93a386Sopenharmony_ci    }
82cb93a386Sopenharmony_ci    sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
83cb93a386Sopenharmony_ci    if (backbuffer == nullptr) {
84cb93a386Sopenharmony_ci        printf("no backbuffer!?\n");
85cb93a386Sopenharmony_ci        // TODO: try recreating testcontext
86cb93a386Sopenharmony_ci        return;
87cb93a386Sopenharmony_ci    }
88cb93a386Sopenharmony_ci
89cb93a386Sopenharmony_ci    markInvalProcessed();
90cb93a386Sopenharmony_ci
91cb93a386Sopenharmony_ci    // draw into the canvas of this surface
92cb93a386Sopenharmony_ci    this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
93cb93a386Sopenharmony_ci    this->visitLayers([=](Layer* layer) { layer->onPaint(backbuffer.get()); });
94cb93a386Sopenharmony_ci
95cb93a386Sopenharmony_ci    backbuffer->flushAndSubmit();
96cb93a386Sopenharmony_ci
97cb93a386Sopenharmony_ci    fWindowContext->swapBuffers();
98cb93a386Sopenharmony_ci}
99cb93a386Sopenharmony_ci
100cb93a386Sopenharmony_civoid Window::onResize(int w, int h) {
101cb93a386Sopenharmony_ci    if (!fWindowContext) {
102cb93a386Sopenharmony_ci        return;
103cb93a386Sopenharmony_ci    }
104cb93a386Sopenharmony_ci    fWindowContext->resize(w, h);
105cb93a386Sopenharmony_ci    this->visitLayers([=](Layer* layer) { layer->onResize(w, h); });
106cb93a386Sopenharmony_ci}
107cb93a386Sopenharmony_ci
108cb93a386Sopenharmony_civoid Window::onActivate(bool isActive) {
109cb93a386Sopenharmony_ci    if (fWindowContext) {
110cb93a386Sopenharmony_ci        fWindowContext->activate(isActive);
111cb93a386Sopenharmony_ci    }
112cb93a386Sopenharmony_ci    fIsActive = isActive;
113cb93a386Sopenharmony_ci}
114cb93a386Sopenharmony_ci
115cb93a386Sopenharmony_ciint Window::width() const {
116cb93a386Sopenharmony_ci    if (!fWindowContext) {
117cb93a386Sopenharmony_ci        return 0;
118cb93a386Sopenharmony_ci    }
119cb93a386Sopenharmony_ci    return fWindowContext->width();
120cb93a386Sopenharmony_ci}
121cb93a386Sopenharmony_ci
122cb93a386Sopenharmony_ciint Window::height() const {
123cb93a386Sopenharmony_ci    if (!fWindowContext) {
124cb93a386Sopenharmony_ci        return 0;
125cb93a386Sopenharmony_ci    }
126cb93a386Sopenharmony_ci    return fWindowContext->height();
127cb93a386Sopenharmony_ci}
128cb93a386Sopenharmony_ci
129cb93a386Sopenharmony_civoid Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
130cb93a386Sopenharmony_ci    fRequestedDisplayParams = params;
131cb93a386Sopenharmony_ci    if (fWindowContext) {
132cb93a386Sopenharmony_ci        fWindowContext->setDisplayParams(fRequestedDisplayParams);
133cb93a386Sopenharmony_ci    }
134cb93a386Sopenharmony_ci}
135cb93a386Sopenharmony_ci
136cb93a386Sopenharmony_ciint Window::sampleCount() const {
137cb93a386Sopenharmony_ci    if (!fWindowContext) {
138cb93a386Sopenharmony_ci        return 0;
139cb93a386Sopenharmony_ci    }
140cb93a386Sopenharmony_ci    return fWindowContext->sampleCount();
141cb93a386Sopenharmony_ci}
142cb93a386Sopenharmony_ci
143cb93a386Sopenharmony_ciint Window::stencilBits() const {
144cb93a386Sopenharmony_ci    if (!fWindowContext) {
145cb93a386Sopenharmony_ci        return -1;
146cb93a386Sopenharmony_ci    }
147cb93a386Sopenharmony_ci    return fWindowContext->stencilBits();
148cb93a386Sopenharmony_ci}
149cb93a386Sopenharmony_ci
150cb93a386Sopenharmony_ciGrDirectContext* Window::directContext() const {
151cb93a386Sopenharmony_ci    if (!fWindowContext) {
152cb93a386Sopenharmony_ci        return nullptr;
153cb93a386Sopenharmony_ci    }
154cb93a386Sopenharmony_ci    return fWindowContext->directContext();
155cb93a386Sopenharmony_ci}
156cb93a386Sopenharmony_ci
157cb93a386Sopenharmony_civoid Window::inval() {
158cb93a386Sopenharmony_ci    if (!fWindowContext) {
159cb93a386Sopenharmony_ci        return;
160cb93a386Sopenharmony_ci    }
161cb93a386Sopenharmony_ci    if (!fIsContentInvalidated) {
162cb93a386Sopenharmony_ci        fIsContentInvalidated = true;
163cb93a386Sopenharmony_ci        onInval();
164cb93a386Sopenharmony_ci    }
165cb93a386Sopenharmony_ci}
166cb93a386Sopenharmony_ci
167cb93a386Sopenharmony_civoid Window::markInvalProcessed() {
168cb93a386Sopenharmony_ci    fIsContentInvalidated = false;
169cb93a386Sopenharmony_ci}
170cb93a386Sopenharmony_ci
171cb93a386Sopenharmony_ci}   // namespace sk_app
172