Skip to content

Commit

Permalink
Improvements to cached blur (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudara committed Jul 31, 2024
1 parent 9d704d2 commit 3f6e6c2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
37 changes: 24 additions & 13 deletions melatonin/cached_blur.cpp
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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!
Expand Down
16 changes: 11 additions & 5 deletions melatonin/cached_blur.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
}

0 comments on commit 3f6e6c2

Please sign in to comment.