Skip to content

Commit

Permalink
Server: Optimize local mode
Browse files Browse the repository at this point in the history
This change avoids flickering when opening a plugin UI by rendering it
at the target position initially to avoid moving it and by waiting for
editor creation in the worker thread.
  • Loading branch information
apohl79 committed May 5, 2022
1 parent 061f3e6 commit dbd013e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Plugin/Source/PluginEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,15 @@ int PluginEditor::getPluginIndex(const String& name) {
return -1;
}

void PluginEditor::focusOfChildComponentChanged(FocusChangeType /*cause*/) {
void PluginEditor::focusOfChildComponentChanged(FocusChangeType cause) {
traceScope();
bool focus = hasKeyboardFocus(true);
// logln("focus change: has focus is " << (int)focus << ", cause is " << cause);
if (focus) {
// reactivate the plugin screen
int active = m_processor.getActivePlugin();
if (active > -1) {
auto p = getLocalModePosition();
logln("focus change: cause is " << cause);
m_processor.editPlugin(active, p.x, p.y);
}
}
Expand Down
23 changes: 13 additions & 10 deletions Server/Source/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void App::restartServer(bool rescan) {
const KnownPluginList& App::getPluginList() { return m_server->getPluginList(); }

template <typename T>
void App::showEditorInternal(std::shared_ptr<Processor> proc, Thread::ThreadID tid, T func) {
void App::showEditorInternal(std::shared_ptr<Processor> proc, Thread::ThreadID tid, T func, int x, int y) {
traceScope();

if (tid == nullptr) {
Expand All @@ -364,7 +364,7 @@ void App::showEditorInternal(std::shared_ptr<Processor> proc, Thread::ThreadID t
}

helper.processor = proc;
helper.window = std::make_unique<ProcessorWindow>(proc, func);
helper.window = std::make_unique<ProcessorWindow>(proc, func, x, y);

#ifdef JUCE_MAC
if (getServer()->getSandboxMode() != Server::SANDBOX_PLUGIN ||
Expand All @@ -378,13 +378,13 @@ void App::showEditorInternal(std::shared_ptr<Processor> proc, Thread::ThreadID t
}

void App::showEditor(std::shared_ptr<Processor> proc, Thread::ThreadID tid,
ProcessorWindow::CaptureCallbackFFmpeg func) {
showEditorInternal(proc, tid, func);
ProcessorWindow::CaptureCallbackFFmpeg func, int x, int y) {
showEditorInternal(proc, tid, func, x, y);
}

void App::showEditor(std::shared_ptr<Processor> proc, Thread::ThreadID tid,
ProcessorWindow::CaptureCallbackNative func) {
showEditorInternal(proc, tid, func);
ProcessorWindow::CaptureCallbackNative func, int x, int y) {
showEditorInternal(proc, tid, func, x, y);
}

void App::hideEditor(Thread::ThreadID tid, bool updateMacOSDock) {
Expand Down Expand Up @@ -502,10 +502,13 @@ void App::restartEditor(Thread::ThreadID tid) {
if (it != m_windows.end()) {
if (it->second.processor != nullptr) {
logln("recreating processor window");
if (nullptr != it->second.callbackFFmpeg) {
it->second.window = std::make_unique<ProcessorWindow>(it->second.processor, it->second.callbackFFmpeg);
} else if (nullptr != it->second.callbackNative) {
it->second.window = std::make_unique<ProcessorWindow>(it->second.processor, it->second.callbackNative);
auto& helper = it->second;
if (nullptr != helper.callbackFFmpeg) {
helper.window = std::make_unique<ProcessorWindow>(helper.processor, helper.callbackFFmpeg,
helper.window->getX(), helper.window->getY());
} else if (nullptr != helper.callbackNative) {
helper.window = std::make_unique<ProcessorWindow>(helper.processor, helper.callbackNative,
helper.window->getX(), helper.window->getY());
}
}
} else {
Expand Down
8 changes: 5 additions & 3 deletions Server/Source/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ class App : public JUCEApplication, public MenuBarModel, public LogTag {
void restartServer(bool rescan = false);
std::shared_ptr<Server> getServer() { return m_server; }

void showEditor(std::shared_ptr<Processor> proc, Thread::ThreadID tid, ProcessorWindow::CaptureCallbackFFmpeg func);
void showEditor(std::shared_ptr<Processor> proc, Thread::ThreadID tid, ProcessorWindow::CaptureCallbackNative func);
void showEditor(std::shared_ptr<Processor> proc, Thread::ThreadID tid, ProcessorWindow::CaptureCallbackFFmpeg func,
int x = 0, int y = 0);
void showEditor(std::shared_ptr<Processor> proc, Thread::ThreadID tid, ProcessorWindow::CaptureCallbackNative func,
int x = 0, int y = 0);

void hideEditor(Thread::ThreadID tid = nullptr, bool updateMacOSDock = true);
void resetEditor(Thread::ThreadID tid);
Expand Down Expand Up @@ -113,7 +115,7 @@ class App : public JUCEApplication, public MenuBarModel, public LogTag {
uint32 m_exitCode = 0;

template <typename T>
void showEditorInternal(std::shared_ptr<Processor> proc, Thread::ThreadID tid, T func);
void showEditorInternal(std::shared_ptr<Processor> proc, Thread::ThreadID tid, T func, int x, int y);

ENABLE_ASYNC_FUNCTORS();
};
Expand Down
17 changes: 10 additions & 7 deletions Server/Source/ProcessorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,39 @@

namespace e47 {

ProcessorWindow::ProcessorWindow(std::shared_ptr<Processor> proc, CaptureCallbackNative func)
ProcessorWindow::ProcessorWindow(std::shared_ptr<Processor> proc, CaptureCallbackNative func, int x, int y)
: DocumentWindow(proc->getName(), Colours::lightgrey, DocumentWindow::closeButton),
LogTag("procwindow"),
m_processor(proc),
m_callbackNative(func),
m_callbackFFmpeg(nullptr) {
traceScope();
initAsyncFunctors();
setBounds(x, y, 100, 100);
logln("creating processor window for " << m_processor->getName() << " at " << x << "x" << y);
if (m_processor->hasEditor()) {
createEditor();
}
}

ProcessorWindow::ProcessorWindow(std::shared_ptr<Processor> proc, CaptureCallbackFFmpeg func)
ProcessorWindow::ProcessorWindow(std::shared_ptr<Processor> proc, CaptureCallbackFFmpeg func, int x, int y)
: DocumentWindow(proc->getName(), Colours::lightgrey, DocumentWindow::closeButton),
LogTag("procwindow"),
m_processor(proc),
m_callbackNative(nullptr),
m_callbackFFmpeg(func) {
traceScope();
initAsyncFunctors();
logln("creating processor window: " << m_processor->getName());
setBounds(x, y, 100, 100);
logln("creating processor window for " << m_processor->getName() << " at " << x << "x" << y);
if (m_processor->hasEditor()) {
createEditor();
}
}

ProcessorWindow::~ProcessorWindow() {
traceScope();
logln("destroying processor window: " << m_processor->getName());
logln("destroying processor window for " << m_processor->getName());
stopAsyncFunctors();
stopCapturing();
if (m_editor != nullptr) {
Expand Down Expand Up @@ -203,14 +206,14 @@ void ProcessorWindow::createEditor() {
bool success = true;

if (m_processor->isClient()) {
auto p = m_processor->getLastPosition();
m_processor->showEditor(p.x, p.y);
//auto p = m_processor->getLastPosition();
m_processor->showEditor(getX(), getY());
} else {
m_editor = m_processor->createEditorIfNeeded();
if (nullptr != m_editor) {
setContentNonOwned(m_editor, true);
if (getApp()->getServer()->getScreenLocalMode()) {
setTopLeftPosition(m_processor->getLastPosition());
setTopLeftPosition({getX(), getY()});
} else {
setTopLeftPosition(userRect.getTopLeft());
}
Expand Down
4 changes: 2 additions & 2 deletions Server/Source/ProcessorWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ProcessorWindow : public DocumentWindow, private Timer, public LogTag {
using CaptureCallbackNative = std::function<void(std::shared_ptr<Image> image, int width, int height)>;
using CaptureCallbackFFmpeg = ScreenRecorder::CaptureCallback;

ProcessorWindow(std::shared_ptr<Processor> proc, CaptureCallbackNative func);
ProcessorWindow(std::shared_ptr<Processor> proc, CaptureCallbackFFmpeg func);
ProcessorWindow(std::shared_ptr<Processor> proc, CaptureCallbackNative func, int x, int y);
ProcessorWindow(std::shared_ptr<Processor> proc, CaptureCallbackFFmpeg func, int x, int y);
~ProcessorWindow() override;

void closeButtonPressed() override;
Expand Down
15 changes: 12 additions & 3 deletions Server/Source/ScreenWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ void ScreenWorker::showEditor(Thread::ThreadID tid, std::shared_ptr<Processor> p

runOnMsgThreadAsync([this] {
traceScope();
logln("trying to hide an existing editor");
if (getApp()->getServer()->getScreenCapturingOff()) {
getApp()->hideEditor(m_currentTid, false);
} else {
Expand All @@ -196,8 +197,15 @@ void ScreenWorker::showEditor(Thread::ThreadID tid, std::shared_ptr<Processor> p
}
});

if (getApp()->getServer()->getScreenCapturingFFmpeg()) {
runOnMsgThreadAsync([this, proc] {
if (getApp()->getServer()->getScreenCapturingOff()) {
logln("showing editor with NO callback");
runOnMsgThreadSync([this, proc, x, y] {
traceScope();
getApp()->showEditor(proc, m_currentTid, [](const uint8_t*, int, int, int, double) {}, x, y);
});
} else if (getApp()->getServer()->getScreenCapturingFFmpeg()) {
logln("showing editor with ffmpeg callback");
runOnMsgThreadSync([this, proc] {
traceScope();
getApp()->showEditor(proc, m_currentTid, [this](const uint8_t* data, int size, int w, int h, double scale) {
// executed in the context of the screen recorder worker thread
Expand All @@ -222,7 +230,8 @@ void ScreenWorker::showEditor(Thread::ThreadID tid, std::shared_ptr<Processor> p
});
});
} else {
runOnMsgThreadAsync([this, proc] {
logln("showing editor with legacy callback");
runOnMsgThreadSync([this, proc] {
traceScope();
m_currentImageLock.lock();
m_currentImage.reset();
Expand Down

0 comments on commit dbd013e

Please sign in to comment.