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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Window.h"
|
|
|
|
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkCanvas.h"
|
2016-06-17 16:29:14 +00:00
|
|
|
#include "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
|
|
|
|
|
|
|
void Window::detach() {
|
2016-05-05 19:32:03 +00:00
|
|
|
delete fWindowContext;
|
|
|
|
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(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::onChar(SkUnichar c, uint32_t modifiers) {
|
|
|
|
return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:08:51 +00:00
|
|
|
bool Window::onKey(Key key, InputState state, uint32_t 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
|
|
|
}
|
|
|
|
|
|
|
|
bool Window::onMouse(int x, int y, InputState state, uint32_t 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
|
|
|
}
|
|
|
|
|
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
|
|
|
bool Window::onMouseWheel(float delta, uint32_t 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
|
|
|
}
|
|
|
|
|
2016-06-06 15:48:47 +00:00
|
|
|
bool Window::onTouch(intptr_t owner, 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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2016-05-23 17:52:34 +00:00
|
|
|
markInvalProcessed();
|
2016-05-20 13:01:06 +00:00
|
|
|
sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
|
2016-04-06 13:08:59 +00:00
|
|
|
if (backbuffer) {
|
|
|
|
// draw into the canvas of this surface
|
|
|
|
SkCanvas* canvas = backbuffer->getCanvas();
|
|
|
|
|
2017-12-19 16:15:16 +00:00
|
|
|
this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
|
|
|
|
this->visitLayers([=](Layer* layer) { layer->onPaint(canvas); });
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
canvas->flush();
|
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
fWindowContext->swapBuffers();
|
2016-04-07 18:09:51 +00:00
|
|
|
} else {
|
2016-05-27 13:47:08 +00:00
|
|
|
printf("no backbuffer!?\n");
|
2016-04-07 18:09:51 +00:00
|
|
|
// try recreating testcontext
|
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);
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
2016-05-04 20:49:13 +00:00
|
|
|
|
2017-02-24 20:22:53 +00:00
|
|
|
int Window::width() {
|
|
|
|
if (!fWindowContext) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fWindowContext->width();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Window::height() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-28 00:00:53 +00:00
|
|
|
const GrContext* Window::getGrContext() const {
|
|
|
|
if (!fWindowContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return fWindowContext->getGrContext();
|
|
|
|
}
|
|
|
|
|
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
|