From 3f6e6c2e54ddd2df19efbf3607a5b7d871a77650 Mon Sep 17 00:00:00 2001 From: Sudara Date: Wed, 31 Jul 2024 22:53:41 +0200 Subject: [PATCH] Improvements to cached blur (#69) --- melatonin/cached_blur.cpp | 37 ++++++++++++++++++++++++------------- melatonin/cached_blur.h | 16 +++++++++++----- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/melatonin/cached_blur.cpp b/melatonin/cached_blur.cpp index 0b6e95c..4873871 100644 --- a/melatonin/cached_blur.cpp +++ b/melatonin/cached_blur.cpp @@ -1,6 +1,6 @@ #include "cached_blur.h" -#include "internal/rendered_single_channel_shadow.h" #include "internal/implementations.h" +#include "internal/rendered_single_channel_shadow.h" namespace melatonin { @@ -12,24 +12,35 @@ namespace melatonin void CachedBlur::update (const juce::Image& newSource) { - if (newSource != src) - { - jassert (newSource.isValid()); - src = newSource; - - // the first time the blur is created, a copy is needed - // so we are passing correct dimensions, etc to the blur algo - dst = src.createCopy(); - melatonin::blur::argb (src, dst, radius); - } + jassert (newSource.isValid()); + src = newSource; + + // the first time the blur is created, a copy is needed + // so we are passing correct dimensions, etc to the blur algo + dst = src.createCopy(); + blur::argb (src, dst, radius); } - juce::Image& CachedBlur::render (juce::Image& newSource) + juce::Image& CachedBlur::render (const juce::Image& newSource) { - update (newSource); + // TODO: This doesn't check if image contents have changed + // Only that a new image is being passed in. + // This is problematic for juce::ImageEffectFilter::applyEffect + // because the image passed in will always be the same image. + // In that case, you can directly call update + if (needsRedraw || newSource != src) + update (newSource); + return dst; } + void CachedBlur::setRadius (size_t newRadius) + { + radius = newRadius; + needsRedraw = true; + } + + juce::Image& CachedBlur::render() { // You either need to have called update or rendered with a src! diff --git a/melatonin/cached_blur.h b/melatonin/cached_blur.h index c432efc..c82505b 100644 --- a/melatonin/cached_blur.h +++ b/melatonin/cached_blur.h @@ -7,19 +7,25 @@ namespace melatonin public: explicit CachedBlur (size_t r); - // we are passing the source by value here + // we are passing the source by reference here // (but it's a value object of sorts since its reference counted) void update (const juce::Image& newSource); - juce::Image& render (juce::Image& newSource); + // Render and potentially update the image + juce::Image& render (const juce::Image& newSource); + + // Render the image from cache juce::Image& render(); + void setRadius (size_t newRadius); + private: // juce::Images are value objects, reference counted behind the scenes // We want to store a reference to the src so we can compare on render // And we actually are the owner of the dst size_t radius = 0; - juce::Image src; - juce::Image dst; + juce::Image src {}; + juce::Image dst {}; + bool needsRedraw = false; }; -} \ No newline at end of file +}