2016-04-06 13:08:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/sk_app/Window.h"
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "tools/sk_app/WindowContext.h"
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2016-05-04 20:49:13 +00:00
|
|
|
namespace sk_app {
|
|
|
|
|
2017-12-08 21:45:43 +00:00
|
|
|
Window::Window() {}
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2019-08-01 20:21:49 +00:00
|
|
|
Window::~Window() {}
|
|
|
|
|
|
|
|
void Window::detach() { fWindowContext = nullptr; }
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2017-12-19 16:15:16 +00:00
|
|
|
void Window::visitLayers(std::function<void(Layer*)> visitor) {
|
2017-12-08 21:45:43 +00:00
|
|
|
for (int i = 0; i < fLayers.count(); ++i) {
|
2017-12-19 16:15:16 +00:00
|
|
|
if (fLayers[i]->fActive) {
|
|
|
|
visitor(fLayers[i]);
|
|
|
|
}
|
2017-12-08 21:45:43 +00:00
|
|
|
}
|
2017-02-24 20:22:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:15:16 +00:00
|
|
|
bool Window::signalLayers(std::function<bool(Layer*)> visitor) {
|
2017-12-08 21:45:43 +00:00
|
|
|
for (int i = fLayers.count() - 1; i >= 0; --i) {
|
2017-12-19 16:15:16 +00:00
|
|
|
if (fLayers[i]->fActive && visitor(fLayers[i])) {
|
2017-12-08 21:45:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2016-04-06 19:08:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:15:16 +00:00
|
|
|
void Window::onBackendCreated() {
|
|
|
|
this->visitLayers([](Layer* layer) { layer->onBackendCreated(); });
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool Window::onChar(SkUnichar c, skui::ModifierKey modifiers) {
|
2017-12-19 16:15:16 +00:00
|
|
|
return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool Window::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
|
2017-12-19 16:15:16 +00:00
|
|
|
return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); });
|
2016-04-06 19:08:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool Window::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
|
2017-12-19 16:15:16 +00:00
|
|
|
return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); });
|
2016-04-06 19:08:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool Window::onMouseWheel(float delta, skui::ModifierKey modifiers) {
|
2017-12-19 16:15:16 +00:00
|
|
|
return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); });
|
Integrate the ImGui library with viewer
Code and docs are at: https://github.com/ocornut/imgui
ImGui is an open source immediate mode GUI library that's
lightweight and fairly simply to integrate. Widget functions
return their state, and the library emits vertex and index
data to render everything. It's got a huge set of built-in
widgets and really robust layout control.
For the initial integration, I had to fix up event handling
in the viewer's app framework (to get mouse wheel and more
keys, etc...).
The new viewer 'Debug' window is toggled with the space bar.
For this change, I've added one feature to that window: the
slide picker. It's got a list of all slides, with filtering
support, and the ability to click to switch slides.
I also included the ImGui 'Demo' window (toggled with 'g').
This is nicely laid out, and includes examples of pretty
much everything the library can do. It also serves as good
documentation - find something that looks like what you want,
and then go look at the corresponding code (all of it is in
imgui_demo.cpp).
I have other CLs with other features (like directly editing
the primaries of the working color space), but I wanted to
land this chunk first, then start adding more features.
Other than adding new debugging features, there are few
more outstanding work items:
1) Raster doesn't render the GUI correctly, due to non-
invertible pos -> UV matrices. Florin is working on that.
2) Touch inputs aren't being routed yet, so the GUI isn't
usable on Android yet. Might also be tough to work with,
given the size.
3) ImGui has clipboard integration (that's why it wants
the C, X, and V keys), but we need to wire it up to the
OS' clipboard functions.
4) Draw commands can carry a void* payload to support
drawing images (using whatever mechanism the engine has).
I'd like to set that up (probably using SkImage*), which
makes it really easy to add visualization of off-screen
images in GMs, etc...
BUG=skia:
Change-Id: Iac2a63e37228d33141cb55b7e4d60bf11b7e9ae1
Reviewed-on: https://skia-review.googlesource.com/7702
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2017-02-10 18:36:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool Window::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
|
2017-12-19 16:15:16 +00:00
|
|
|
return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); });
|
2016-05-17 19:44:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 20:53:39 +00:00
|
|
|
bool Window::onFling(skui::InputState state) {
|
|
|
|
return this->signalLayers([=](Layer* layer) { return layer->onFling(state); });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::onPinch(skui::InputState state, float scale, float x, float y) {
|
|
|
|
return this->signalLayers([=](Layer* layer) { return layer->onPinch(state, scale, x, y); });
|
|
|
|
}
|
|
|
|
|
2016-05-27 15:52:52 +00:00
|
|
|
void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
|
2017-12-19 16:15:16 +00:00
|
|
|
this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); });
|
2016-05-27 15:52:52 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 13:08:59 +00:00
|
|
|
void Window::onPaint() {
|
2017-02-24 20:22:53 +00:00
|
|
|
if (!fWindowContext) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-16 16:06:58 +00:00
|
|
|
if (!fIsActive) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-13 02:31:29 +00:00
|
|
|
sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
|
|
|
|
if (backbuffer == nullptr) {
|
|
|
|
printf("no backbuffer!?\n");
|
|
|
|
// TODO: try recreating testcontext
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:52:34 +00:00
|
|
|
markInvalProcessed();
|
2020-04-13 02:31:29 +00:00
|
|
|
|
|
|
|
// draw into the canvas of this surface
|
2019-03-21 18:43:00 +00:00
|
|
|
this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
|
2020-04-13 02:31:29 +00:00
|
|
|
this->visitLayers([=](Layer* layer) { layer->onPaint(backbuffer.get()); });
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2020-05-14 19:45:44 +00:00
|
|
|
backbuffer->flushAndSubmit();
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2020-04-13 02:31:29 +00:00
|
|
|
fWindowContext->swapBuffers();
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-27 15:50:12 +00:00
|
|
|
void Window::onResize(int w, int h) {
|
2017-02-24 20:22:53 +00:00
|
|
|
if (!fWindowContext) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-05 19:32:03 +00:00
|
|
|
fWindowContext->resize(w, h);
|
2018-08-09 19:06:19 +00:00
|
|
|
this->visitLayers([=](Layer* layer) { layer->onResize(w, h); });
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
2016-05-04 20:49:13 +00:00
|
|
|
|
2020-11-16 16:06:58 +00:00
|
|
|
void Window::onActivate(bool isActive) {
|
|
|
|
if (fWindowContext) {
|
|
|
|
fWindowContext->activate(isActive);
|
|
|
|
}
|
|
|
|
fIsActive = isActive;
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:23:34 +00:00
|
|
|
int Window::width() const {
|
2017-02-24 20:22:53 +00:00
|
|
|
if (!fWindowContext) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fWindowContext->width();
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:23:34 +00:00
|
|
|
int Window::height() const {
|
2017-02-24 20:22:53 +00:00
|
|
|
if (!fWindowContext) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fWindowContext->height();
|
|
|
|
}
|
|
|
|
|
2017-06-23 17:32:29 +00:00
|
|
|
void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
|
2017-02-24 23:04:47 +00:00
|
|
|
fRequestedDisplayParams = params;
|
|
|
|
if (fWindowContext) {
|
|
|
|
fWindowContext->setDisplayParams(fRequestedDisplayParams);
|
|
|
|
}
|
2016-05-06 20:28:57 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 23:04:47 +00:00
|
|
|
int Window::sampleCount() const {
|
|
|
|
if (!fWindowContext) {
|
2018-02-03 01:32:49 +00:00
|
|
|
return 0;
|
2017-02-24 23:04:47 +00:00
|
|
|
}
|
|
|
|
return fWindowContext->sampleCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Window::stencilBits() const {
|
|
|
|
if (!fWindowContext) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return fWindowContext->stencilBits();
|
2016-05-06 20:28:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 17:55:21 +00:00
|
|
|
GrDirectContext* Window::directContext() const {
|
2017-02-28 00:00:53 +00:00
|
|
|
if (!fWindowContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-07-10 17:55:21 +00:00
|
|
|
return fWindowContext->directContext();
|
2017-02-28 00:00:53 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:52:34 +00:00
|
|
|
void Window::inval() {
|
2017-02-24 20:22:53 +00:00
|
|
|
if (!fWindowContext) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-23 17:52:34 +00:00
|
|
|
if (!fIsContentInvalidated) {
|
|
|
|
fIsContentInvalidated = true;
|
|
|
|
onInval();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::markInvalProcessed() {
|
|
|
|
fIsContentInvalidated = false;
|
|
|
|
}
|
|
|
|
|
2016-05-04 20:49:13 +00:00
|
|
|
} // namespace sk_app
|