skia2/tools/viewer/sk_app/Window.cpp

174 lines
4.3 KiB
C++
Raw Normal View History

/*
* 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"
#include "WindowContext.h"
namespace sk_app {
static void default_backend_created_func(void* userData) {}
static bool default_char_func(SkUnichar c, uint32_t modifiers, void* userData) {
return false;
}
static bool default_key_func(Window::Key key, Window::InputState state, uint32_t modifiers,
void* userData) {
return false;
}
static bool default_mouse_func(int x, int y, Window::InputState state, uint32_t modifiers,
void* userData) {
return false;
}
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
static bool default_mouse_wheel_func(float delta, uint32_t modifiers, void* userData) {
return false;
}
static bool default_touch_func(intptr_t owner, Window::InputState state, float x, float y,
void* userData) {
return false;
}
static void default_ui_state_changed_func(
const SkString& stateName, const SkString& stateValue, void* userData) {}
static void default_paint_func(SkCanvas*, void* userData) {}
Window::Window() : fBackendCreatedFunc(default_backend_created_func)
, fCharFunc(default_char_func)
, fKeyFunc(default_key_func)
, fMouseFunc(default_mouse_func)
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
, fMouseWheelFunc(default_mouse_wheel_func)
, fTouchFunc(default_touch_func)
, fUIStateChangedFunc(default_ui_state_changed_func)
, fPaintFunc(default_paint_func) {
}
void Window::detach() {
delete fWindowContext;
fWindowContext = nullptr;
}
void Window::onBackendCreated() {
fBackendCreatedFunc(fBackendCreatedUserData);
}
bool Window::onChar(SkUnichar c, uint32_t modifiers) {
return fCharFunc(c, modifiers, fCharUserData);
}
bool Window::onKey(Key key, InputState state, uint32_t modifiers) {
return fKeyFunc(key, state, modifiers, fKeyUserData);
}
bool Window::onMouse(int x, int y, InputState state, uint32_t modifiers) {
return fMouseFunc(x, y, state, modifiers, fMouseUserData);
}
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) {
return fMouseWheelFunc(delta, modifiers, fMouseWheelUserData);
}
bool Window::onTouch(intptr_t owner, InputState state, float x, float y) {
return fTouchFunc(owner, state, x, y, fTouchUserData);
}
void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
return fUIStateChangedFunc(stateName, stateValue, fUIStateChangedUserData);
}
void Window::onPaint() {
if (!fWindowContext) {
return;
}
markInvalProcessed();
sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
if (backbuffer) {
// draw into the canvas of this surface
SkCanvas* canvas = backbuffer->getCanvas();
fPaintFunc(canvas, fPaintUserData);
canvas->flush();
fWindowContext->swapBuffers();
} else {
printf("no backbuffer!?\n");
// try recreating testcontext
}
}
void Window::onResize(int w, int h) {
if (!fWindowContext) {
return;
}
fWindowContext->resize(w, h);
}
int Window::width() {
if (!fWindowContext) {
return 0;
}
return fWindowContext->width();
}
int Window::height() {
if (!fWindowContext) {
return 0;
}
return fWindowContext->height();
}
void Window::setRequestedDisplayParams(const DisplayParams& params) {
fRequestedDisplayParams = params;
if (fWindowContext) {
fWindowContext->setDisplayParams(fRequestedDisplayParams);
}
}
int Window::sampleCount() const {
if (!fWindowContext) {
return -1;
}
return fWindowContext->sampleCount();
}
int Window::stencilBits() const {
if (!fWindowContext) {
return -1;
}
return fWindowContext->stencilBits();
}
const GrContext* Window::getGrContext() const {
if (!fWindowContext) {
return nullptr;
}
return fWindowContext->getGrContext();
}
void Window::inval() {
if (!fWindowContext) {
return;
}
if (!fIsContentInvalidated) {
fIsContentInvalidated = true;
onInval();
}
}
void Window::markInvalProcessed() {
fIsContentInvalidated = false;
}
} // namespace sk_app