New approach for Window (always valid; swap handles for real UI)

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2021-11-03 23:27:24 +00:00
parent 823ad6b53d
commit 9ff29396df
No known key found for this signature in database
GPG key ID: CDBAA37ABC74FBA0
5 changed files with 209 additions and 78 deletions

View file

@ -27,7 +27,6 @@
#else #else
#define DISTRHO_PLUGIN_HAS_UI 1 #define DISTRHO_PLUGIN_HAS_UI 1
#define DISTRHO_PLUGIN_WANT_DIRECT_ACCESS 1 #define DISTRHO_PLUGIN_WANT_DIRECT_ACCESS 1
#define DISTRHO_UI_USE_NANOVG 1
#define DISTRHO_UI_USER_RESIZABLE 1 #define DISTRHO_UI_USER_RESIZABLE 1
#endif #endif
#define DISTRHO_PLUGIN_NUM_INPUTS 2 #define DISTRHO_PLUGIN_NUM_INPUTS 2

View file

@ -379,6 +379,7 @@ public:
context->event = new rack::widget::EventState; context->event = new rack::widget::EventState;
context->scene = new rack::app::Scene; context->scene = new rack::app::Scene;
context->event->rootWidget = context->scene; context->event->rootWidget = context->scene;
context->window = new rack::window::Window;
context->patch->loadTemplate(); context->patch->loadTemplate();
context->scene->rackScroll->reset(); context->scene->rackScroll->reset();
@ -386,16 +387,6 @@ public:
#ifdef HAVE_LIBLO #ifdef HAVE_LIBLO
fInitializer->oscPlugin = this; fInitializer->oscPlugin = this;
#endif #endif
#if defined(__MOD_DEVICES__) && !defined(HEADLESS)
context->window = new rack::window::Window;
rack::window::WindowInit(context->window, this);
/*
context->scene->removeChild(context->scene->menuBar);
context->scene->menuBar = rack::app::createMenuBar(getWindow(), getApp().isStandalone());
context->scene->addChildBelow(context->scene->menuBar, context->scene->rackScroll);
*/
#endif
} }
~CardinalPlugin() override ~CardinalPlugin() override
@ -413,11 +404,6 @@ public:
{ {
const ScopedContext sc(this); const ScopedContext sc(this);
#if defined(__MOD_DEVICES__) && !defined(HEADLESS)
delete context->window;
context->window = nullptr;
#endif
delete context; delete context;
} }

View file

@ -27,7 +27,6 @@
#else #else
#define DISTRHO_PLUGIN_HAS_UI 1 #define DISTRHO_PLUGIN_HAS_UI 1
#define DISTRHO_PLUGIN_WANT_DIRECT_ACCESS 1 #define DISTRHO_PLUGIN_WANT_DIRECT_ACCESS 1
#define DISTRHO_UI_USE_NANOVG 1
#define DISTRHO_UI_USER_RESIZABLE 1 #define DISTRHO_UI_USER_RESIZABLE 1
#endif #endif
#define DISTRHO_PLUGIN_IS_SYNTH 1 #define DISTRHO_PLUGIN_IS_SYNTH 1

View file

@ -45,8 +45,8 @@ namespace app {
widget::Widget* createMenuBar(Window& window, bool isStandalone); widget::Widget* createMenuBar(Window& window, bool isStandalone);
} }
namespace window { namespace window {
void WindowInit(Window* window, DISTRHO_NAMESPACE::UI* ui); void WindowSetPluginUI(Window* window, DISTRHO_NAMESPACE::UI* ui);
void WindowMods(Window* window, int mods); void WindowSetMods(Window* window, int mods);
} }
} }
@ -80,7 +80,7 @@ class CardinalUI : public UI,
: context(ui->fContext) : context(ui->fContext)
{ {
rack::contextSet(context); rack::contextSet(context);
rack::window::WindowMods(context->window, mods); rack::window::WindowSetMods(context->window, mods);
WindowParametersRestore(context->window); WindowParametersRestore(context->window);
} }
@ -106,38 +106,37 @@ public:
if (scaleFactor != 1) if (scaleFactor != 1)
setSize(1228 * scaleFactor, 666 * scaleFactor); setSize(1228 * scaleFactor, 666 * scaleFactor);
rack::window::Window* const window = new rack::window::Window;
rack::window::WindowInit(window, this);
rack::contextSet(fContext); rack::contextSet(fContext);
fContext->scene->removeChild(fContext->scene->menuBar); rack::window::WindowSetPluginUI(fContext->window, this);
if (fContext->scene->menuBar != nullptr)
fContext->scene->removeChild(fContext->scene->menuBar);
fContext->scene->menuBar = rack::app::createMenuBar(getWindow(), getApp().isStandalone()); fContext->scene->menuBar = rack::app::createMenuBar(getWindow(), getApp().isStandalone());
fContext->scene->addChildBelow(fContext->scene->menuBar, fContext->scene->rackScroll); fContext->scene->addChildBelow(fContext->scene->menuBar, fContext->scene->rackScroll);
fContext->window = window; fContext->window->step();
rack::widget::Widget::ContextCreateEvent e;
fContext->scene->onContextCreate(e);
window->step();
rack::contextSet(nullptr); rack::contextSet(nullptr);
WindowParametersSetCallback(window, this); WindowParametersSetCallback(fContext->window, this);
} }
~CardinalUI() override ~CardinalUI() override
{ {
rack::contextSet(fContext); rack::contextSet(fContext);
delete fContext->window; rack::widget::Widget* const menuBar = fContext->scene->menuBar;
fContext->window = nullptr; fContext->scene->menuBar = nullptr;
fContext->scene->removeChild(menuBar);
rack::window::WindowSetPluginUI(fContext->window, nullptr);
rack::contextSet(nullptr); rack::contextSet(nullptr);
} }
void onNanoDisplay() override void onDisplay() override
{ {
const ScopedContext sc(this); const ScopedContext sc(this);
fContext->window->step(); fContext->window->step();
@ -481,8 +480,6 @@ protected:
default: key = ev.key; break; default: key = ev.key; break;
} }
rack::window::WindowMods(fContext->window, mods);
const ScopedContext sc(this, mods); const ScopedContext sc(this, mods);
return fContext->event->handleKey(fLastMousePos, key, ev.keycode, action, mods); return fContext->event->handleKey(fLastMousePos, key, ev.keycode, action, mods);
} }

View file

@ -44,8 +44,14 @@
#endif #endif
#include "DistrhoUI.hpp" #include "DistrhoUI.hpp"
#include "Application.hpp"
#include "../WindowParameters.hpp" #include "../WindowParameters.hpp"
#ifndef DGL_NO_SHARED_RESOURCES
# include "NanoVG.hpp"
# include "src/Resources.hpp"
#endif
namespace rack { namespace rack {
namespace window { namespace window {
@ -93,10 +99,27 @@ std::shared_ptr<Image> Image::load(const std::string& filename) {
} }
struct FontWithOriginalContext : Font {
int ohandle = -1;
std::string ofilename;
};
struct ImageWithOriginalContext : Image {
int ohandle = -1;
std::string ofilename;
};
struct Window::Internal { struct Window::Internal {
DISTRHO_NAMESPACE::UI* ui = nullptr; DISTRHO_NAMESPACE::UI* ui = nullptr;
DISTRHO_NAMESPACE::WindowParameters params; DISTRHO_NAMESPACE::WindowParameters params;
DISTRHO_NAMESPACE::WindowParametersCallback* callback = nullptr; DISTRHO_NAMESPACE::WindowParametersCallback* callback = nullptr;
DGL_NAMESPACE::Application hiddenApp;
DGL_NAMESPACE::Window hiddenWindow;
NVGcontext* r_vg = nullptr;
NVGcontext* r_fbVg = nullptr;
NVGcontext* o_vg = nullptr;
NVGcontext* o_fbVg = nullptr;
math::Vec size = minWindowSize; math::Vec size = minWindowSize;
std::string lastWindowTitle; std::string lastWindowTitle;
@ -108,66 +131,185 @@ struct Window::Internal {
double frameTime = 0.0; double frameTime = 0.0;
double lastFrameDuration = 0.0; double lastFrameDuration = 0.0;
std::map<std::string, std::shared_ptr<Font>> fontCache; std::map<std::string, std::shared_ptr<FontWithOriginalContext>> fontCache;
std::map<std::string, std::shared_ptr<Image>> imageCache; std::map<std::string, std::shared_ptr<ImageWithOriginalContext>> imageCache;
bool fbDirtyOnSubpixelChange = true; bool fbDirtyOnSubpixelChange = true;
Internal()
: hiddenApp(),
hiddenWindow(hiddenApp) { hiddenApp.idle(); }
}; };
#ifndef DGL_NO_SHARED_RESOURCES
static int loadFallbackFont(NVGcontext* const vg)
{
const int font = nvgFindFont(vg, NANOVG_DEJAVU_SANS_TTF);
if (font >= 0)
return font;
using namespace dpf_resources;
return nvgCreateFontMem(vg, NANOVG_DEJAVU_SANS_TTF,
(uchar*)dejavusans_ttf, dejavusans_ttf_size, 0);
}
#endif
Window::Window() { Window::Window() {
internal = new Internal; internal = new Internal;
}
void WindowInit(Window* const window, DISTRHO_NAMESPACE::UI* const ui) DGL_NAMESPACE::Window::ScopedGraphicsContext sgc(internal->hiddenWindow);
{
const GLubyte* vendor = glGetString(GL_VENDOR);
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
INFO("Renderer: %s %s", vendor, renderer);
INFO("OpenGL: %s", version);
window->internal->ui = ui; // Set up NanoVG
window->internal->size = rack::math::Vec(ui->getWidth(), ui->getHeight()); const int nvgFlags = NVG_ANTIALIAS;
window->vg = ui->getContext();
#ifdef NANOVG_GLES2 #ifdef NANOVG_GLES2
window->fbVg = nvgCreateSharedGLES2(window->vg, NVG_ANTIALIAS); vg = nvgCreateGLES2(nvgFlags);
fbVg = nvgCreateSharedGLES2(vg, nvgFlags);
#else #else
window->fbVg = nvgCreateSharedGL2(window->vg, NVG_ANTIALIAS); vg = nvgCreateGL2(nvgFlags);
fbVg = nvgCreateSharedGL2(vg, nvgFlags);
#endif #endif
// Load default Blendish font // Load default Blendish font
#ifndef DGL_NO_SHARED_RESOURCES #ifndef DGL_NO_SHARED_RESOURCES
ui->loadSharedResources(); uiFont = std::make_shared<Font>();
window->uiFont = std::make_shared<Font>(); uiFont->vg = vg;
window->uiFont->vg = window->vg; uiFont->handle = loadFallbackFont(vg);
window->uiFont->handle = nvgFindFont(window->vg, NANOVG_DEJAVU_SANS_TTF);
window->internal->fontCache["res/fonts/DejaVuSans.ttf"] = window->uiFont; std::shared_ptr<FontWithOriginalContext> uiFont2;
uiFont2 = std::make_shared<FontWithOriginalContext>();
uiFont2->vg = vg;
uiFont2->handle = loadFallbackFont(vg);
uiFont2->ofilename = asset::system("res/fonts/DejaVuSans.ttf");
internal->fontCache[uiFont2->ofilename] = uiFont2;
#else #else
window->loadFont(asset::system("res/fonts/DejaVuSans.ttf")); uiFont = loadFont(asset::system("res/fonts/DejaVuSans.ttf"));
#endif #endif
if (window->uiFont != nullptr) if (uiFont != nullptr)
bndSetFont(window->uiFont->handle); bndSetFont(uiFont->handle);
// Init settings
WindowParametersRestore(window);
} }
void WindowMods(Window* const window, const int mods) void WindowSetPluginUI(Window* const window, DISTRHO_NAMESPACE::UI* const ui)
{
if (ui != nullptr)
{
const GLubyte* vendor = glGetString(GL_VENDOR);
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
INFO("Renderer: %s %s", vendor, renderer);
INFO("OpenGL: %s", version);
window->internal->ui = ui;
window->internal->size = rack::math::Vec(ui->getWidth(), ui->getHeight());
// Set up NanoVG
const int nvgFlags = NVG_ANTIALIAS;
#ifdef NANOVG_GLES2
window->internal->r_vg = nvgCreateSharedGLES2(nvgFlags);
window->internal->r_fbVg = nvgCreateSharedGLES2(window->internal->r_vg, nvgFlags);
#else
window->internal->r_vg = nvgCreateGL2(nvgFlags);
window->internal->r_fbVg = nvgCreateSharedGL2(window->internal->r_vg, nvgFlags);
#endif
// swap contexts
window->internal->o_vg = window->vg;
window->internal->o_fbVg = window->fbVg;
window->vg = window->internal->r_vg;
window->fbVg = window->internal->r_fbVg;
// also for fonts and images
window->uiFont->vg = window->vg;
window->uiFont->handle = loadFallbackFont(window->vg);
for (auto& font : window->internal->fontCache)
{
font.second->vg = window->vg;
font.second->ohandle = font.second->handle;
font.second->handle = nvgCreateFont(window->vg,
font.second->ofilename.c_str(), font.second->ofilename.c_str());
}
for (auto& image : window->internal->imageCache)
{
image.second->vg = window->vg;
image.second->ohandle = image.second->handle;
image.second->handle = nvgCreateImage(window->vg, image.second->ofilename.c_str(),
NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
}
// Init settings
WindowParametersRestore(window);
widget::Widget::ContextCreateEvent e;
APP->scene->onContextCreate(e);
}
else
{
widget::Widget::ContextDestroyEvent e;
APP->scene->onContextDestroy(e);
// swap contexts
window->uiFont->vg = window->internal->o_vg;
window->vg = window->internal->o_vg;
window->fbVg = window->internal->o_fbVg;
window->internal->o_vg = nullptr;
window->internal->o_fbVg = nullptr;
// also for fonts and images
window->uiFont->vg = window->vg;
window->uiFont->handle = loadFallbackFont(window->vg);
for (auto& font : window->internal->fontCache)
{
font.second->vg = window->vg;
font.second->handle = font.second->ohandle;
font.second->ohandle = -1;
}
for (auto& image : window->internal->imageCache)
{
image.second->vg = window->vg;
image.second->handle = image.second->ohandle;
image.second->ohandle = -1;
}
// also for images
#if defined NANOVG_GLES2
nvgDeleteGLES2(window->internal->r_vg);
nvgDeleteGLES2(window->internal->r_fbVg);
#else
nvgDeleteGL2(window->internal->r_vg);
nvgDeleteGL2(window->internal->r_fbVg);
#endif
// window->internal->hiddenWindow.close();
window->internal->ui = nullptr;
window->internal->callback = nullptr;
}
}
void WindowSetMods(Window* const window, const int mods)
{ {
window->internal->mods = mods; window->internal->mods = mods;
} }
Window::~Window() { Window::~Window() {
if (APP->scene) { {
widget::Widget::ContextDestroyEvent e; DGL_NAMESPACE::Window::ScopedGraphicsContext sgc(internal->hiddenWindow);
APP->scene->onContextDestroy(e);
}
// Fonts and Images in the cache must be deleted before the NanoVG context is deleted // Fonts and Images in the cache must be deleted before the NanoVG context is deleted
internal->fontCache.clear(); internal->fontCache.clear();
internal->imageCache.clear(); internal->imageCache.clear();
#if defined NANOVG_GLES2
nvgDeleteGLES2(vg);
nvgDeleteGLES2(fbVg);
#else
nvgDeleteGL2(vg);
nvgDeleteGL2(fbVg);
#endif
}
delete internal; delete internal;
} }
@ -189,6 +331,8 @@ void Window::run() {
void Window::step() { void Window::step() {
DISTRHO_SAFE_ASSERT_RETURN(internal->ui != nullptr,);
double frameTime = system::getTime(); double frameTime = system::getTime();
double lastFrameTime = internal->frameTime; double lastFrameTime = internal->frameTime;
internal->frameTime = frameTime; internal->frameTime = frameTime;
@ -196,7 +340,7 @@ void Window::step() {
// DEBUG("%.2lf Hz", 1.0 / internal->lastFrameDuration); // DEBUG("%.2lf Hz", 1.0 / internal->lastFrameDuration);
// Make event handlers and step() have a clean NanoVG context // Make event handlers and step() have a clean NanoVG context
// nvgReset(vg); nvgReset(vg);
if (uiFont != nullptr) if (uiFont != nullptr)
bndSetFont(uiFont->handle); bndSetFont(uiFont->handle);
@ -238,6 +382,7 @@ void Window::step() {
// Render scene // Render scene
// Update and render // Update and render
nvgBeginFrame(vg, fbWidth, fbHeight, pixelRatio);
nvgScale(vg, pixelRatio, pixelRatio); nvgScale(vg, pixelRatio, pixelRatio);
// Draw scene // Draw scene
@ -249,6 +394,7 @@ void Window::step() {
glViewport(0, 0, fbWidth, fbHeight); glViewport(0, 0, fbWidth, fbHeight);
glClearColor(0.0, 0.0, 0.0, 1.0); glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgEndFrame(vg);
} }
internal->frame++; internal->frame++;
@ -264,6 +410,8 @@ void Window::screenshotModules(const std::string&, float) {
void Window::close() { void Window::close() {
DISTRHO_SAFE_ASSERT_RETURN(internal->ui != nullptr,);
internal->ui->getWindow().close(); internal->ui->getWindow().close();
} }
@ -322,14 +470,15 @@ std::shared_ptr<Font> Window::loadFont(const std::string& filename) {
return pair->second; return pair->second;
// Load font // Load font
std::shared_ptr<Font> font; std::shared_ptr<FontWithOriginalContext> font;
try { try {
font = std::make_shared<Font>(); font = std::make_shared<FontWithOriginalContext>();
font->ofilename = filename;
font->loadFile(filename, vg); font->loadFile(filename, vg);
} }
catch (Exception& e) { catch (Exception& e) {
WARN("%s", e.what()); WARN("%s", e.what());
font = NULL; font = nullptr;
} }
internal->fontCache[filename] = font; internal->fontCache[filename] = font;
return font; return font;
@ -342,14 +491,15 @@ std::shared_ptr<Image> Window::loadImage(const std::string& filename) {
return pair->second; return pair->second;
// Load image // Load image
std::shared_ptr<Image> image; std::shared_ptr<ImageWithOriginalContext> image;
try { try {
image = std::make_shared<Image>(); image = std::make_shared<ImageWithOriginalContext>();
image->ofilename = filename;
image->loadFile(filename, vg); image->loadFile(filename, vg);
} }
catch (Exception& e) { catch (Exception& e) {
WARN("%s", e.what()); WARN("%s", e.what());
image = NULL; image = nullptr;
} }
internal->imageCache[filename] = image; internal->imageCache[filename] = image;
return image; return image;