Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Effects manifest defaults #10837

Merged
merged 3 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/effects/presets/effectpreset.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "effects/presets/effectpreset.h"

#include <QHash>
#include <functional>

#include "effects/backends/effectsbackend.h"
#include "effects/effectslot.h"
#include "effects/presets/effectxmlelements.h"
Expand Down Expand Up @@ -99,5 +102,28 @@ const QDomElement EffectPreset::toXml(QDomDocument* doc) const {
return effectElement;
}

EffectPreset::~EffectPreset() {
void EffectPreset::updateParametersFrom(const EffectPreset& other) {
DEBUG_ASSERT(backendType() == other.backendType());

// we use a std::reference_wrapper as optimization so we don't copy and
// store the entire object when we need to copy later anyways
// TODO(XXX): Replace QHash with <std::flat_map>
QHash<QString, std::reference_wrapper<const EffectParameterPreset>> parameterPresetLookup;
// we build temporary hashtable to reduce the algorithmic complexity of the
// lookup later. A plain std::find_if (O(n)) would've resulted in O(n^n)
// parameter updating. The hashtable has O(1) lookup with O(n) updating,
// though with more constant overhead. Since 3rd-party EffectPresets could
// have hundreds of parameters, we can't afford O(n^2) lookups.
// TODO(XXX): measure overhead and possibly implement fallback to lower
// overhead O(n^2) solution for small presets.
for (const EffectParameterPreset& preset : other.m_effectParameterPresets) {
parameterPresetLookup.insert(preset.id(), std::ref(preset));
}

for (EffectParameterPreset& parameterToUpdate : m_effectParameterPresets) {
const auto parameterToCopyIt = parameterPresetLookup.constFind(parameterToUpdate.id());
if (parameterToCopyIt != parameterPresetLookup.constEnd()) {
parameterToUpdate = *parameterToCopyIt;
}
}
}
10 changes: 9 additions & 1 deletion src/effects/presets/effectpreset.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class EffectPreset {
EffectPreset(const QDomElement& element);
EffectPreset(const EffectSlotPointer pEffectSlot);
EffectPreset(const EffectManifestPointer pManifest);
~EffectPreset();

const QDomElement toXml(QDomDocument* doc) const;

Expand All @@ -39,6 +38,15 @@ class EffectPreset {
return m_effectParameterPresets;
}

/// updates all of the parameters of `this` with the parameters
/// of `preset`.
/// The operation is not symmetric:
/// Parameters which are present on `preset` but not on `this` will
/// not be added to `this`
/// Parameters present on `this` but not `preset` will keep their previous
/// settings
void updateParametersFrom(const EffectPreset& preset);

private:
QString m_id;
EffectBackendType m_backendType;
Expand Down
8 changes: 5 additions & 3 deletions src/effects/presets/effectpresetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ void EffectPresetManager::loadDefaultEffectPresets() {
file.close();
continue;
}
EffectPresetPointer pEffectPreset(new EffectPreset(doc.documentElement()));
if (!pEffectPreset->isEmpty()) {
auto presetFromFile = EffectPreset(doc.documentElement());
if (!presetFromFile.isEmpty()) {
EffectManifestPointer pManifest = m_pBackendManager->getManifest(
pEffectPreset->id(), pEffectPreset->backendType());
presetFromFile.id(), presetFromFile.backendType());
if (pManifest) {
auto pEffectPreset = EffectPresetPointer(new EffectPreset(pManifest));
pEffectPreset->updateParametersFrom(presetFromFile);
m_defaultPresets.insert(pManifest, pEffectPreset);
}
}
Expand Down